@@ -40,16 +40,14 @@ public class View: IView
40
40
/// This <c>IView</c> implementation is a Multiton,
41
41
/// so you should not call the constructor
42
42
/// directly, but instead call the static Multiton
43
- /// Factory method <c>View.getInstance(multitonKey, () => new View(multitonKey ))</c>
43
+ /// Factory method <c>View.getInstance(multitonKey, key => new View(key ))</c>
44
44
/// </para>
45
45
/// </remarks>
46
46
/// <param name="key">Key of view</param>
47
- /// <exception cref="System.Exception">Thrown if instance for this Multiton key has already been constructed</exception>
48
47
public View ( string key )
49
48
{
50
- if ( instanceMap . TryGetValue ( key , out Lazy < IView > _ ) && multitonKey != null ) throw new Exception ( MULTITON_MSG ) ;
51
49
multitonKey = key ;
52
- instanceMap . TryAdd ( key , new Lazy < IView > ( ( ) => this ) ) ;
50
+ InstanceMap . TryAdd ( key , new Lazy < IView > ( this ) ) ;
53
51
mediatorMap = new ConcurrentDictionary < string , IMediator > ( ) ;
54
52
observerMap = new ConcurrentDictionary < string , IList < IObserver > > ( ) ;
55
53
InitializeView ( ) ;
@@ -74,11 +72,11 @@ protected virtual void InitializeView()
74
72
/// <c>View</c> Multiton Factory method.
75
73
/// </summary>
76
74
/// <param name="key">Key of view</param>
77
- /// <param name="viewFunc ">the <c>FuncDelegate</c> of the <c>IView</c></param>
75
+ /// <param name="func ">the <c>FuncDelegate</c> of the <c>IView</c></param>
78
76
/// <returns>the instance for this Multiton key </returns>
79
- public static IView GetInstance ( string key , Func < IView > viewFunc )
77
+ public static IView GetInstance ( string key , Func < string , IView > func )
80
78
{
81
- return instanceMap . GetOrAdd ( key , new Lazy < IView > ( viewFunc ) ) . Value ;
79
+ return InstanceMap . GetOrAdd ( key , new Lazy < IView > ( ( ) => func ( key ) ) ) . Value ;
82
80
}
83
81
84
82
/// <summary>
@@ -89,7 +87,7 @@ public static IView GetInstance(string key, Func<IView> viewFunc)
89
87
/// <param name="observer">the <c>IObserver</c> to register</param>
90
88
public virtual void RegisterObserver ( string notificationName , IObserver observer )
91
89
{
92
- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
90
+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
93
91
{
94
92
observers . Add ( observer ) ;
95
93
}
@@ -113,12 +111,12 @@ public virtual void RegisterObserver(string notificationName, IObserver observer
113
111
public virtual void NotifyObservers ( INotification notification )
114
112
{
115
113
// Get a reference to the observers list for this notification name
116
- if ( observerMap . TryGetValue ( notification . Name , out IList < IObserver > observers_ref ) )
114
+ if ( observerMap . TryGetValue ( notification . Name , out var observersRef ) )
117
115
{
118
116
// Copy observers from reference array to working array,
119
117
// since the reference array may change during the notification loop
120
- var observers = new List < IObserver > ( observers_ref ) ;
121
- foreach ( IObserver observer in observers )
118
+ var observers = new List < IObserver > ( observersRef ) ;
119
+ foreach ( var observer in observers )
122
120
{
123
121
observer . NotifyObserver ( notification ) ;
124
122
}
@@ -132,9 +130,9 @@ public virtual void NotifyObservers(INotification notification)
132
130
/// <param name="notifyContext">remove the observer with this object as its notifyContext</param>
133
131
public virtual void RemoveObserver ( string notificationName , object notifyContext )
134
132
{
135
- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
133
+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
136
134
{
137
- for ( int i = 0 ; i < observers . Count ; i ++ )
135
+ for ( var i = 0 ; i < observers . Count ; i ++ )
138
136
{
139
137
if ( observers [ i ] . CompareNotifyContext ( notifyContext ) )
140
138
{
@@ -146,7 +144,7 @@ public virtual void RemoveObserver(string notificationName, object notifyContext
146
144
// Also, when a Notification's Observer list length falls to
147
145
// zero, delete the notification key from the observer map
148
146
if ( observers . Count == 0 )
149
- observerMap . TryRemove ( notificationName , out IList < IObserver > _ ) ;
147
+ observerMap . TryRemove ( notificationName , out _ ) ;
150
148
}
151
149
}
152
150
@@ -174,14 +172,14 @@ public virtual void RegisterMediator(IMediator mediator)
174
172
{
175
173
mediator . InitializeNotifier ( multitonKey ) ;
176
174
177
- string [ ] interests = mediator . ListNotificationInterests ( ) ;
175
+ var interests = mediator . ListNotificationInterests ( ) ;
178
176
179
177
if ( interests . Length > 0 )
180
178
{
181
179
IObserver observer = new Observer ( mediator . HandleNotification , mediator ) ;
182
- for ( int i = 0 ; i < interests . Length ; i ++ )
180
+ foreach ( var interest in interests )
183
181
{
184
- RegisterObserver ( interests [ i ] , observer ) ;
182
+ RegisterObserver ( interest , observer ) ;
185
183
}
186
184
}
187
185
// alert the mediator that it has been registered
@@ -196,7 +194,7 @@ public virtual void RegisterMediator(IMediator mediator)
196
194
/// <returns>the <c>IMediator</c> instance previously registered with the given <c>mediatorName</c>.</returns>
197
195
public virtual IMediator RetrieveMediator ( string mediatorName )
198
196
{
199
- return mediatorMap . TryGetValue ( mediatorName , out IMediator mediator ) ? mediator : null ;
197
+ return mediatorMap . TryGetValue ( mediatorName , out var mediator ) ? mediator : null ;
200
198
}
201
199
202
200
/// <summary>
@@ -206,12 +204,12 @@ public virtual IMediator RetrieveMediator(string mediatorName)
206
204
/// <returns>the <c>IMediator</c> that was removed from the <c>View</c></returns>
207
205
public virtual IMediator RemoveMediator ( string mediatorName )
208
206
{
209
- if ( mediatorMap . TryRemove ( mediatorName , out IMediator mediator ) )
207
+ if ( mediatorMap . TryRemove ( mediatorName , out var mediator ) )
210
208
{
211
- string [ ] interests = mediator . ListNotificationInterests ( ) ;
212
- for ( int i = 0 ; i < interests . Length ; i ++ )
209
+ var interests = mediator . ListNotificationInterests ( ) ;
210
+ foreach ( var interest in interests )
213
211
{
214
- RemoveObserver ( interests [ i ] , mediator ) ;
212
+ RemoveObserver ( interest , mediator ) ;
215
213
}
216
214
mediator . OnRemove ( ) ;
217
215
}
@@ -234,22 +232,20 @@ public virtual bool HasMediator(string mediatorName)
234
232
/// <param name="key">multitonKey of IView instance to remove</param>
235
233
public static void RemoveView ( string key )
236
234
{
237
- instanceMap . TryRemove ( key , out Lazy < IView > _ ) ;
235
+ InstanceMap . TryRemove ( key , out _ ) ;
238
236
}
239
237
240
238
/// <summary>The Multiton Key for this Core</summary>
241
- protected string multitonKey ;
239
+ protected readonly string multitonKey ;
242
240
243
241
/// <summary>Mapping of Mediator names to Mediator instances</summary>
244
- protected ConcurrentDictionary < string , IMediator > mediatorMap ;
242
+ protected readonly ConcurrentDictionary < string , IMediator > mediatorMap ;
245
243
246
244
/// <summary>Mapping of Notification names to Observer lists</summary>
247
- protected ConcurrentDictionary < string , IList < IObserver > > observerMap ;
245
+ protected readonly ConcurrentDictionary < string , IList < IObserver > > observerMap ;
248
246
249
247
/// <summary>The Multiton View instanceMap.</summary>
250
- protected static ConcurrentDictionary < string , Lazy < IView > > instanceMap = new ConcurrentDictionary < string , Lazy < IView > > ( ) ;
248
+ protected static readonly ConcurrentDictionary < string , Lazy < IView > > InstanceMap = new ConcurrentDictionary < string , Lazy < IView > > ( ) ;
251
249
252
- /// <summary>Message Constants</summary>
253
- protected const string MULTITON_MSG = "View instance for this Multiton key already constructed!" ;
254
250
}
255
251
}
0 commit comments