Skip to content

Commit 8be3301

Browse files
committed
update
1 parent 1267be3 commit 8be3301

36 files changed

+298
-208
lines changed

PureMVC/Core/Controller.cs

+15-19
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,13 @@ public class Controller: IController
5353
/// <remarks>This <c>IController</c> implementation is a Multiton,
5454
/// so you should not call the constructor
5555
/// directly, but instead call the static Multiton
56-
/// Factory method <c>Controller.getInstance(multitonKey, () => new Controller(multitonKey))</c>
56+
/// Factory method <c>Controller.getInstance(multitonKey, key => new Controller(key))</c>
5757
/// </remarks>
5858
/// <param name="key">Key of controller</param>
59-
/// <exception cref="System.Exception">Thrown if instance for this Multiton key has already been constructed</exception>
6059
public Controller(string key)
6160
{
62-
if (instanceMap.TryGetValue(key, out Lazy<IController> _) && multitonKey != null) throw new Exception(MULTITON_MSG);
6361
multitonKey = key;
64-
instanceMap.TryAdd(multitonKey, new Lazy<IController>(() => this));
62+
InstanceMap.TryAdd(multitonKey, new Lazy<IController>(this));
6563
commandMap = new ConcurrentDictionary<string, Func<ICommand>>();
6664
InitializeController();
6765
}
@@ -88,18 +86,18 @@ public Controller(string key)
8886
/// </remarks>
8987
protected virtual void InitializeController()
9088
{
91-
view = View.GetInstance(multitonKey, () => new View(multitonKey));
89+
view = View.GetInstance(multitonKey, key => new View(key));
9290
}
9391

9492
/// <summary>
9593
/// <c>Controller</c> Multiton Factory method.
9694
/// </summary>
9795
/// <param name="key">Key of controller</param>
98-
/// <param name="controllerFunc">the <c>FuncDelegate</c> of the <c>IController</c></param>
96+
/// <param name="func">the <c>FuncDelegate</c> of the <c>IController</c></param>
9997
/// <returns>the Multiton instance of <c>Controller</c></returns>
100-
public static IController GetInstance(string key, Func<IController> controllerFunc)
98+
public static IController GetInstance(string key, Func<string, IController> func)
10199
{
102-
return instanceMap.GetOrAdd(key, (k) => new Lazy<IController>(controllerFunc)).Value;
100+
return InstanceMap.GetOrAdd(key, new Lazy<IController>(() => func(key))).Value;
103101
}
104102

105103
/// <summary>
@@ -109,9 +107,9 @@ public static IController GetInstance(string key, Func<IController> controllerFu
109107
/// <param name="notification">note an <c>INotification</c></param>
110108
public virtual void ExecuteCommand(INotification notification)
111109
{
112-
if (commandMap.TryGetValue(notification.Name, out Func<ICommand> commandFunc))
110+
if (commandMap.TryGetValue(notification.Name, out var commandFunc))
113111
{
114-
ICommand commandInstance = commandFunc();
112+
var commandInstance = commandFunc();
115113
commandInstance.InitializeNotifier(multitonKey);
116114
commandInstance.Execute(notification);
117115
}
@@ -136,7 +134,7 @@ public virtual void ExecuteCommand(INotification notification)
136134
/// <param name="commandFunc">the <c>Func Delegate</c> of the <c>ICommand</c></param>
137135
public virtual void RegisterCommand(string notificationName, Func<ICommand> commandFunc)
138136
{
139-
if (commandMap.TryGetValue(notificationName, out Func<ICommand> _) == false)
137+
if (commandMap.TryGetValue(notificationName, out _) == false)
140138
{
141139
view.RegisterObserver(notificationName, new Observer(ExecuteCommand, this));
142140
}
@@ -149,7 +147,7 @@ public virtual void RegisterCommand(string notificationName, Func<ICommand> comm
149147
/// <param name="notificationName">the name of the <c>INotification</c> to remove the <c>ICommand</c> mapping for</param>
150148
public virtual void RemoveCommand(string notificationName)
151149
{
152-
if (commandMap.TryRemove(notificationName, out Func<ICommand> _))
150+
if (commandMap.TryRemove(notificationName, out _))
153151
{
154152
view.RemoveObserver(notificationName, this);
155153
}
@@ -171,22 +169,20 @@ public virtual bool HasCommand(string notificationName)
171169
/// <param name="key">multitonKey of IController instance to remove</param>
172170
public static void RemoveController(string key)
173171
{
174-
instanceMap.TryRemove(key, out Lazy<IController> _);
172+
InstanceMap.TryRemove(key, out _);
175173
}
176174

177175
/// <summary>Local reference to View</summary>
178176
protected IView view;
179177

180178
/// <summary>The Multiton Key for this Core</summary>
181-
protected string multitonKey;
179+
protected readonly string multitonKey;
182180

183181
/// <summary>Mapping of Notification names to Command Class references</summary>
184-
protected ConcurrentDictionary<string, Func<ICommand>> commandMap;
182+
protected readonly ConcurrentDictionary<string, Func<ICommand>> commandMap;
185183

186184
/// <summary>The Multiton Controller instanceMap.</summary>
187-
protected static ConcurrentDictionary<string, Lazy<IController>> instanceMap = new ConcurrentDictionary<string, Lazy<IController>>();
188-
189-
/// <summary>Message Constants</summary>
190-
protected const string MULTITON_MSG = "Controller instance for this Multiton key already constructed!";
185+
protected static readonly ConcurrentDictionary<string, Lazy<IController>> InstanceMap = new ConcurrentDictionary<string, Lazy<IController>>();
186+
191187
}
192188
}

PureMVC/Core/Model.cs

+10-15
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ public class Model: IModel
4040
/// This <c>IModel</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>Model.getInstance(multitonKey, () => new Model(multitonKey))</c>
43+
/// Factory method <c>Model.getInstance(multitonKey, key => new Model(key))</c>
4444
/// </para>
4545
/// </remarks>
4646
/// <param name="key">Key of model</param>
47-
/// <exception cref="System.Exception">Thrown if instance for this Multiton key has already been constructed</exception>
4847
public Model(string key)
4948
{
50-
if (instanceMap.ContainsKey(key) && multitonKey != null) throw new Exception(MULTITON_MSG);
5149
multitonKey = key;
52-
instanceMap.TryAdd(key, new Lazy<IModel>(() => this));
50+
InstanceMap.TryAdd(key, new Lazy<IModel>(this));
5351
proxyMap = new ConcurrentDictionary<string, IProxy>();
5452
InitializeModel();
5553
}
@@ -73,11 +71,11 @@ protected virtual void InitializeModel()
7371
/// <c>Model</c> Multiton Factory method.
7472
/// </summary>
7573
/// <param name="key">Key of model</param>
76-
/// <param name="modelFunc">the <c>FuncDelegate</c> of the <c>IModel</c></param>
74+
/// <param name="func">the <c>FuncDelegate</c> of the <c>IModel</c></param>
7775
/// <returns>the instance for this Multiton key </returns>
78-
public static IModel GetInstance(string key, Func<IModel> modelFunc)
76+
public static IModel GetInstance(string key, Func<string, IModel> func)
7977
{
80-
return instanceMap.GetOrAdd(key, new Lazy<IModel>(modelFunc)).Value;
78+
return InstanceMap.GetOrAdd(key, new Lazy<IModel>(() => func(key))).Value;
8179
}
8280

8381
/// <summary>
@@ -98,7 +96,7 @@ public virtual void RegisterProxy(IProxy proxy)
9896
/// <returns>the <c>IProxy</c> instance previously registered with the given <c>proxyName</c>.</returns>
9997
public virtual IProxy RetrieveProxy(string proxyName)
10098
{
101-
return proxyMap.TryGetValue(proxyName, out IProxy proxy) ? proxy : null;
99+
return proxyMap.TryGetValue(proxyName, out var proxy) ? proxy : null;
102100
}
103101

104102
/// <summary>
@@ -108,7 +106,7 @@ public virtual IProxy RetrieveProxy(string proxyName)
108106
/// <returns>the <c>IProxy</c> that was removed from the <c>Model</c></returns>
109107
public virtual IProxy RemoveProxy(string proxyName)
110108
{
111-
if (proxyMap.TryRemove(proxyName, out IProxy proxy))
109+
if (proxyMap.TryRemove(proxyName, out var proxy))
112110
{
113111
proxy.OnRemove();
114112
}
@@ -131,19 +129,16 @@ public virtual bool HasProxy(string proxyName)
131129
/// <param name="key">multitonKey of IModel instance to remove</param>
132130
public static void RemoveModel(string key)
133131
{
134-
instanceMap.TryRemove(key, out Lazy<IModel> _);
132+
InstanceMap.TryRemove(key, out _);
135133
}
136134

137135
/// <summary>The Multiton Key for this Core</summary>
138-
protected string multitonKey;
136+
protected readonly string multitonKey;
139137

140138
/// <summary>Mapping of proxyNames to IProxy instances</summary>
141139
protected readonly ConcurrentDictionary<string, IProxy> proxyMap;
142140

143141
/// <summary>The Multiton Model instanceMap.</summary>
144-
protected static readonly ConcurrentDictionary<string, Lazy<IModel>> instanceMap = new ConcurrentDictionary<string, Lazy<IModel>>();
145-
146-
/// <summary>Message Constants</summary>
147-
protected const string MULTITON_MSG = "Model instance for this Multiton key already constructed!";
142+
protected static readonly ConcurrentDictionary<string, Lazy<IModel>> InstanceMap = new ConcurrentDictionary<string, Lazy<IModel>>();
148143
}
149144
}

PureMVC/Core/View.cs

+25-29
Original file line numberDiff line numberDiff line change
@@ -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
}

PureMVC/Interfaces/ICommand.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public interface ICommand: INotifier
1616
/// <summary>
1717
/// Execute the <c>ICommand</c>'s logic to handle a given <c>INotification</c>.
1818
/// </summary>
19-
/// <param name="Notification">an <c>INotification</c> to handle.</param>
20-
void Execute(INotification Notification);
19+
/// <param name="notification">an <c>INotification</c> to handle.</param>
20+
void Execute(INotification notification);
2121
}
2222
}

PureMVC/Interfaces/IObserver.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public interface IObserver
7171
void NotifyObserver(INotification notification);
7272

7373
/// <summary>
74-
/// Compare the given object to the notificaiton context object.
74+
/// Compare the given object to the notification context object.
7575
/// </summary>
7676
/// <param name="obj">the object to compare.</param>
7777
/// <returns>indicating if the notification context and the object are the same.</returns>

PureMVC/Patterns/Command/MacroCommand.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace PureMVC.Patterns.Command
3838
/// <seealso cref="PureMVC.Core.Controller"/>
3939
/// <seealso cref="PureMVC.Patterns.Observer.Notification"/>
4040
/// <seealso cref="PureMVC.Patterns.Command.SimpleCommand"/>
41-
public class MacroCommand : Notifier, ICommand, INotifier
41+
public class MacroCommand : Notifier, ICommand
4242
{
4343
/// <summary>
4444
/// Constructor.
@@ -113,20 +113,20 @@ protected void AddSubCommand(Func<ICommand> commandFunc)
113113
/// order.
114114
/// </para>
115115
/// </remarks>
116-
/// <param name="notification">the <c>INotification</c> object to be passsed to each <i>SubCommand</i>.</param>
116+
/// <param name="notification">the <c>INotification</c> object to be passed to each <i>SubCommand</i>.</param>
117117
public virtual void Execute(INotification notification)
118118
{
119119
while(subcommands.Count > 0)
120120
{
121-
Func<ICommand> commandFunc = subcommands[0];
122-
ICommand commandInstance = commandFunc();
121+
var commandFunc = subcommands[0];
122+
var commandInstance = commandFunc();
123123
commandInstance.InitializeNotifier(MultitonKey);
124124
commandInstance.Execute(notification);
125125
subcommands.RemoveAt(0);
126126
}
127127
}
128128

129129
/// <summary>List of subcommands</summary>
130-
public IList<Func<ICommand>> subcommands;
130+
public readonly IList<Func<ICommand>> subcommands;
131131
}
132132
}

0 commit comments

Comments
 (0)