11//
22// PureMVC C# Standard
33//
4- // Copyright(c) 2017 Saad Shams <saad.shams@puremvc.org>
4+ // Copyright(c) 2020 Saad Shams <saad.shams@puremvc.org>
55// Your reuse is governed by the Creative Commons Attribution 3.0 License
66//
77
@@ -46,7 +46,7 @@ public class View: IView
4646 /// <exception cref="System.Exception">Thrown if Singleton instance has already been constructed</exception>
4747 public View ( )
4848 {
49- if ( instance != null ) throw new Exception ( Singleton_MSG ) ;
49+ if ( instance != null ) throw new Exception ( SingletonMsg ) ;
5050 instance = this ;
5151 mediatorMap = new ConcurrentDictionary < string , IMediator > ( ) ;
5252 observerMap = new ConcurrentDictionary < string , IList < IObserver > > ( ) ;
@@ -71,13 +71,13 @@ protected virtual void InitializeView()
7171 /// <summary>
7272 /// <c>View</c> Singleton Factory method.
7373 /// </summary>
74- /// <param name="viewFunc ">the <c>FuncDelegate</c> of the <c>IView</c></param>
74+ /// <param name="factory ">the <c>FuncDelegate</c> of the <c>IView</c></param>
7575 /// <returns>the instance for this Singleton key </returns>
76- public static IView GetInstance ( Func < IView > viewFunc )
76+ public static IView GetInstance ( Func < IView > factory )
7777 {
7878 if ( instance == null )
7979 {
80- instance = viewFunc ( ) ;
80+ instance = factory ( ) ;
8181 }
8282 return instance ;
8383 }
@@ -90,7 +90,7 @@ public static IView GetInstance(Func<IView> viewFunc)
9090 /// <param name="observer">the <c>IObserver</c> to register</param>
9191 public virtual void RegisterObserver ( string notificationName , IObserver observer )
9292 {
93- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
93+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
9494 {
9595 observers . Add ( observer ) ;
9696 }
@@ -114,14 +114,14 @@ public virtual void RegisterObserver(string notificationName, IObserver observer
114114 public virtual void NotifyObservers ( INotification notification )
115115 {
116116 // Get a reference to the observers list for this notification name
117- if ( observerMap . TryGetValue ( notification . Name , out IList < IObserver > observers_ref ) )
117+ if ( observerMap . TryGetValue ( notification . Name , out var observersRef ) )
118118 {
119119 // Copy observers from reference array to working array,
120120 // since the reference array may change during the notification loop
121- var observers = new List < IObserver > ( observers_ref ) ;
121+ var observers = new List < IObserver > ( observersRef ) ;
122122
123123 // Notify Observers from the working array
124- foreach ( IObserver observer in observers )
124+ foreach ( var observer in observers )
125125 {
126126 observer . NotifyObserver ( notification ) ;
127127 }
@@ -136,10 +136,10 @@ public virtual void NotifyObservers(INotification notification)
136136 public virtual void RemoveObserver ( string notificationName , object notifyContext )
137137 {
138138 // the observer list for the notification under inspection
139- if ( observerMap . TryGetValue ( notificationName , out IList < IObserver > observers ) )
139+ if ( observerMap . TryGetValue ( notificationName , out var observers ) )
140140 {
141141 // find the observer for the notifyContext
142- for ( int i = 0 ; i < observers . Count ; i ++ )
142+ for ( var i = 0 ; i < observers . Count ; i ++ )
143143 {
144144 if ( observers [ i ] . CompareNotifyContext ( notifyContext ) )
145145 {
@@ -153,7 +153,7 @@ public virtual void RemoveObserver(string notificationName, object notifyContext
153153 // Also, when a Notification's Observer list length falls to
154154 // zero, delete the notification key from the observer map
155155 if ( observers . Count == 0 )
156- observerMap . TryRemove ( notificationName , out IList < IObserver > _ ) ;
156+ observerMap . TryRemove ( notificationName , out _ ) ;
157157 }
158158 }
159159
@@ -182,18 +182,18 @@ public virtual void RegisterMediator(IMediator mediator)
182182 if ( mediatorMap . TryAdd ( mediator . MediatorName , mediator ) )
183183 {
184184 // Get Notification interests, if any.
185- string [ ] interests = mediator . ListNotificationInterests ( ) ;
185+ var interests = mediator . ListNotificationInterests ( ) ;
186186
187187 // Register Mediator as an observer for each notification of interests
188188 if ( interests . Length > 0 )
189189 {
190- // Create Observer referencing this mediator's handlNotification method
190+ // Create Observer referencing this mediator's handleNotification method
191191 IObserver observer = new Observer ( mediator . HandleNotification , mediator ) ;
192192
193193 // Register Mediator as Observer for its list of Notification interests
194- for ( int i = 0 ; i < interests . Length ; i ++ )
194+ foreach ( var interest in interests )
195195 {
196- RegisterObserver ( interests [ i ] , observer ) ;
196+ RegisterObserver ( interest , observer ) ;
197197 }
198198 }
199199 // alert the mediator that it has been registered
@@ -208,7 +208,7 @@ public virtual void RegisterMediator(IMediator mediator)
208208 /// <returns>the <c>IMediator</c> instance previously registered with the given <c>mediatorName</c>.</returns>
209209 public virtual IMediator RetrieveMediator ( string mediatorName )
210210 {
211- return mediatorMap . TryGetValue ( mediatorName , out IMediator mediator ) ? mediator : null ;
211+ return mediatorMap . TryGetValue ( mediatorName , out var mediator ) ? mediator : null ;
212212 }
213213
214214 /// <summary>
@@ -219,15 +219,15 @@ public virtual IMediator RetrieveMediator(string mediatorName)
219219 public virtual IMediator RemoveMediator ( string mediatorName )
220220 {
221221 // Retrieve the named mediator
222- if ( mediatorMap . TryRemove ( mediatorName , out IMediator mediator ) )
222+ if ( mediatorMap . TryRemove ( mediatorName , out var mediator ) )
223223 {
224224 // for every notification this mediator is interested in...
225- string [ ] interests = mediator . ListNotificationInterests ( ) ;
226- for ( int i = 0 ; i < interests . Length ; i ++ )
225+ var interests = mediator . ListNotificationInterests ( ) ;
226+ foreach ( var interest in interests )
227227 {
228228 // remove the observer linking the mediator
229- // to the notification interest
230- RemoveObserver ( interests [ i ] , mediator ) ;
229+ // to the notification interest
230+ RemoveObserver ( interest , mediator ) ;
231231 }
232232
233233 // remove the mediator from the map
@@ -256,6 +256,6 @@ public virtual bool HasMediator(string mediatorName)
256256 protected static IView instance ;
257257
258258 /// <summary>Message Constants</summary>
259- protected const string Singleton_MSG = "View Singleton already constructed!" ;
259+ protected const string SingletonMsg = "View Singleton already constructed!" ;
260260 }
261261}
0 commit comments