@@ -40,16 +40,14 @@ public class View: IView
4040 /// This <c>IView</c> implementation is a Multiton,
4141 /// so you should not call the constructor
4242 /// 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>
4444 /// </para>
4545 /// </remarks>
4646 /// <param name="key">Key of view</param>
47- /// <exception cref="System.Exception">Thrown if instance for this Multiton key has already been constructed</exception>
4847 public View ( string key )
4948 {
50- if ( instanceMap . TryGetValue ( key , out Lazy < IView > _ ) && multitonKey != null ) throw new Exception ( MULTITON_MSG ) ;
5149 multitonKey = key ;
52- instanceMap . TryAdd ( key , new Lazy < IView > ( ( ) => this ) ) ;
50+ InstanceMap . TryAdd ( key , new Lazy < IView > ( this ) ) ;
5351 mediatorMap = new ConcurrentDictionary < string , IMediator > ( ) ;
5452 observerMap = new ConcurrentDictionary < string , IList < IObserver > > ( ) ;
5553 InitializeView ( ) ;
@@ -74,11 +72,11 @@ protected virtual void InitializeView()
7472 /// <c>View</c> Multiton Factory method.
7573 /// </summary>
7674 /// <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>
7876 /// <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 )
8078 {
81- return instanceMap . GetOrAdd ( key , new Lazy < IView > ( viewFunc ) ) . Value ;
79+ return InstanceMap . GetOrAdd ( key , new Lazy < IView > ( ( ) => func ( key ) ) ) . Value ;
8280 }
8381
8482 /// <summary>
@@ -89,7 +87,7 @@ public static IView GetInstance(string key, Func<IView> viewFunc)
8987 /// <param name="observer">the <c>IObserver</c> to register</param>
9088 public virtual void RegisterObserver ( string notificationName , IObserver observer )
9189 {
92- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
90+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
9391 {
9492 observers . Add ( observer ) ;
9593 }
@@ -113,12 +111,12 @@ public virtual void RegisterObserver(string notificationName, IObserver observer
113111 public virtual void NotifyObservers ( INotification notification )
114112 {
115113 // 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 ) )
117115 {
118116 // Copy observers from reference array to working array,
119117 // 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 )
122120 {
123121 observer . NotifyObserver ( notification ) ;
124122 }
@@ -132,9 +130,9 @@ public virtual void NotifyObservers(INotification notification)
132130 /// <param name="notifyContext">remove the observer with this object as its notifyContext</param>
133131 public virtual void RemoveObserver ( string notificationName , object notifyContext )
134132 {
135- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
133+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
136134 {
137- for ( int i = 0 ; i < observers . Count ; i ++ )
135+ for ( var i = 0 ; i < observers . Count ; i ++ )
138136 {
139137 if ( observers [ i ] . CompareNotifyContext ( notifyContext ) )
140138 {
@@ -146,7 +144,7 @@ public virtual void RemoveObserver(string notificationName, object notifyContext
146144 // Also, when a Notification's Observer list length falls to
147145 // zero, delete the notification key from the observer map
148146 if ( observers . Count == 0 )
149- observerMap . TryRemove ( notificationName , out IList < IObserver > _ ) ;
147+ observerMap . TryRemove ( notificationName , out _ ) ;
150148 }
151149 }
152150
@@ -174,14 +172,14 @@ public virtual void RegisterMediator(IMediator mediator)
174172 {
175173 mediator . InitializeNotifier ( multitonKey ) ;
176174
177- string [ ] interests = mediator . ListNotificationInterests ( ) ;
175+ var interests = mediator . ListNotificationInterests ( ) ;
178176
179177 if ( interests . Length > 0 )
180178 {
181179 IObserver observer = new Observer ( mediator . HandleNotification , mediator ) ;
182- for ( int i = 0 ; i < interests . Length ; i ++ )
180+ foreach ( var interest in interests )
183181 {
184- RegisterObserver ( interests [ i ] , observer ) ;
182+ RegisterObserver ( interest , observer ) ;
185183 }
186184 }
187185 // alert the mediator that it has been registered
@@ -196,7 +194,7 @@ public virtual void RegisterMediator(IMediator mediator)
196194 /// <returns>the <c>IMediator</c> instance previously registered with the given <c>mediatorName</c>.</returns>
197195 public virtual IMediator RetrieveMediator ( string mediatorName )
198196 {
199- return mediatorMap . TryGetValue ( mediatorName , out IMediator mediator ) ? mediator : null ;
197+ return mediatorMap . TryGetValue ( mediatorName , out var mediator ) ? mediator : null ;
200198 }
201199
202200 /// <summary>
@@ -206,12 +204,12 @@ public virtual IMediator RetrieveMediator(string mediatorName)
206204 /// <returns>the <c>IMediator</c> that was removed from the <c>View</c></returns>
207205 public virtual IMediator RemoveMediator ( string mediatorName )
208206 {
209- if ( mediatorMap . TryRemove ( mediatorName , out IMediator mediator ) )
207+ if ( mediatorMap . TryRemove ( mediatorName , out var mediator ) )
210208 {
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 )
213211 {
214- RemoveObserver ( interests [ i ] , mediator ) ;
212+ RemoveObserver ( interest , mediator ) ;
215213 }
216214 mediator . OnRemove ( ) ;
217215 }
@@ -234,22 +232,20 @@ public virtual bool HasMediator(string mediatorName)
234232 /// <param name="key">multitonKey of IView instance to remove</param>
235233 public static void RemoveView ( string key )
236234 {
237- instanceMap . TryRemove ( key , out Lazy < IView > _ ) ;
235+ InstanceMap . TryRemove ( key , out _ ) ;
238236 }
239237
240238 /// <summary>The Multiton Key for this Core</summary>
241- protected string multitonKey ;
239+ protected readonly string multitonKey ;
242240
243241 /// <summary>Mapping of Mediator names to Mediator instances</summary>
244- protected ConcurrentDictionary < string , IMediator > mediatorMap ;
242+ protected readonly ConcurrentDictionary < string , IMediator > mediatorMap ;
245243
246244 /// <summary>Mapping of Notification names to Observer lists</summary>
247- protected ConcurrentDictionary < string , IList < IObserver > > observerMap ;
245+ protected readonly ConcurrentDictionary < string , IList < IObserver > > observerMap ;
248246
249247 /// <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 > > ( ) ;
251249
252- /// <summary>Message Constants</summary>
253- protected const string MULTITON_MSG = "View instance for this Multiton key already constructed!" ;
254250 }
255251}
0 commit comments