Permalink
Browse files

Some clean-up and improvements

  • Loading branch information...
1 parent 2992fa7 commit f85a25181b4fa382bd3afbbbbcb08da891cc8e45 Mark Nijhof committed Dec 20, 2009
@@ -11,8 +11,7 @@ public class ClientSearchFormPresenter : Presenter<IClientSearchFormView>, IClie
private readonly IClientDetailsPresenter _clientDetailsPresenter;
private readonly IReportingRepository _reportingRepository;
- public ClientSearchFormPresenter(IClientSearchFormView clientSearchFormView, IClientDetailsPresenter clientDetailsPresenter, IPopupPresenter popupPresenter, IReportingRepository reportingRepository)
- : base(clientSearchFormView)
+ public ClientSearchFormPresenter(IClientSearchFormView clientSearchFormView, IClientDetailsPresenter clientDetailsPresenter, IPopupPresenter popupPresenter, IReportingRepository reportingRepository) : base(clientSearchFormView)
{
_clientSearchFormView = clientSearchFormView;
_popupPresenter = popupPresenter;
@@ -24,30 +24,39 @@ private void HookUpViewEvents(TView view)
var viewEvents = GetViewEvents(view, viewDefinedEvents);
var presenterEventHandlers = GetPresenterEventHandlers(viewDefinedEvents, this);
- foreach (var actionProperty in viewDefinedEvents)
+ foreach (var viewDefinedEvent in viewDefinedEvents)
{
- var eventInfo = viewEvents[actionProperty];
+ var eventInfo = viewEvents[viewDefinedEvent];
+ var methodInfo = GetTheEventHandler(viewDefinedEvent, presenterEventHandlers, eventInfo);
- var substring = actionProperty.Substring(2);
- if (!presenterEventHandlers.ContainsKey(substring))
- throw new Exception(string.Format("\n\nThere is no event handler for event '{0}' on presenter '{1}' expected '{2}'\n\n", eventInfo.Name, GetType().FullName, substring));
-
- var methodInfo = presenterEventHandlers[substring];
- var newDelegate = Delegate.CreateDelegate(typeof(EventAction), this, methodInfo);
- eventInfo.AddEventHandler(view, newDelegate);
+ WireUpTheEventAndEventHandler(view, eventInfo, methodInfo);
}
}
+ private MethodInfo GetTheEventHandler(string viewDefinedEvent, IDictionary<string, MethodInfo> presenterEventHandlers, EventInfo eventInfo)
+ {
+ var substring = viewDefinedEvent.Substring(2);
+ if (!presenterEventHandlers.ContainsKey(substring))
+ throw new Exception(string.Format("\n\nThere is no event handler for event '{0}' on presenter '{1}' expected '{2}'\n\n", eventInfo.Name, GetType().FullName, substring));
+
+ return presenterEventHandlers[substring];
+ }
+
+ private void WireUpTheEventAndEventHandler(TView view, EventInfo eventInfo, MethodInfo methodInfo)
+ {
+ var newDelegate = Delegate.CreateDelegate(typeof(EventAction), this, methodInfo);
+ eventInfo.AddEventHandler(view, newDelegate);
+ }
+
private static IDictionary<string, MethodInfo> GetPresenterEventHandlers<TPresenter>(ICollection<string> actionProperties, TPresenter presenter)
{
- IDictionary<string, MethodInfo> presenterEventHandlers = new Dictionary<string, MethodInfo>();
- presenter
+ return presenter
.GetType()
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(x => Contains(actionProperties, x))
.ToList()
- .ForEach(x => presenterEventHandlers.Add(new KeyValuePair<string, MethodInfo>(x.Name, x)));
- return presenterEventHandlers;
+ .Select(x => new KeyValuePair<string, MethodInfo>(x.Name, x))
+ .ToDictionary(x => x.Key, x => x.Value);
}
private static List<string> GetViewDefinedEvents()
@@ -57,14 +66,13 @@ private static List<string> GetViewDefinedEvents()
private static IDictionary<string, EventInfo> GetViewEvents(TView view, ICollection<string> actionProperties)
{
- IDictionary<string, EventInfo> viewEvents = new Dictionary<string, EventInfo>();
- view
+ return view
.GetType()
.GetEvents()
.Where(x => Contains(actionProperties, x))
.ToList()
- .ForEach(x => viewEvents.Add(new KeyValuePair<string, EventInfo>(x.Name, x)));
- return viewEvents;
+ .Select(x => new KeyValuePair<string, EventInfo>(x.Name, x))
+ .ToDictionary(x => x.Key, x => x.Value);
}
private static bool Contains(ICollection<string> actionProperties, EventInfo x)

0 comments on commit f85a251

Please sign in to comment.