Skip to content

Commit

Permalink
Some clean-up and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Nijhof authored and Mark Nijhof committed Dec 20, 2009
1 parent 2992fa7 commit f85a251
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Expand Up @@ -11,8 +11,7 @@ public class ClientSearchFormPresenter : Presenter<IClientSearchFormView>, IClie
private readonly IClientDetailsPresenter _clientDetailsPresenter; private readonly IClientDetailsPresenter _clientDetailsPresenter;
private readonly IReportingRepository _reportingRepository; private readonly IReportingRepository _reportingRepository;


public ClientSearchFormPresenter(IClientSearchFormView clientSearchFormView, IClientDetailsPresenter clientDetailsPresenter, IPopupPresenter popupPresenter, IReportingRepository reportingRepository) public ClientSearchFormPresenter(IClientSearchFormView clientSearchFormView, IClientDetailsPresenter clientDetailsPresenter, IPopupPresenter popupPresenter, IReportingRepository reportingRepository) : base(clientSearchFormView)
: base(clientSearchFormView)
{ {
_clientSearchFormView = clientSearchFormView; _clientSearchFormView = clientSearchFormView;
_popupPresenter = popupPresenter; _popupPresenter = popupPresenter;
Expand Down
Expand Up @@ -24,30 +24,39 @@ private void HookUpViewEvents(TView view)
var viewEvents = GetViewEvents(view, viewDefinedEvents); var viewEvents = GetViewEvents(view, viewDefinedEvents);
var presenterEventHandlers = GetPresenterEventHandlers(viewDefinedEvents, this); 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); WireUpTheEventAndEventHandler(view, eventInfo, methodInfo);
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);
} }
} }


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) private static IDictionary<string, MethodInfo> GetPresenterEventHandlers<TPresenter>(ICollection<string> actionProperties, TPresenter presenter)
{ {
IDictionary<string, MethodInfo> presenterEventHandlers = new Dictionary<string, MethodInfo>(); return presenter
presenter
.GetType() .GetType()
.GetMethods(BindingFlags.Instance | BindingFlags.Public) .GetMethods(BindingFlags.Instance | BindingFlags.Public)
.Where(x => Contains(actionProperties, x)) .Where(x => Contains(actionProperties, x))
.ToList() .ToList()
.ForEach(x => presenterEventHandlers.Add(new KeyValuePair<string, MethodInfo>(x.Name, x))); .Select(x => new KeyValuePair<string, MethodInfo>(x.Name, x))
return presenterEventHandlers; .ToDictionary(x => x.Key, x => x.Value);
} }


private static List<string> GetViewDefinedEvents() private static List<string> GetViewDefinedEvents()
Expand All @@ -57,14 +66,13 @@ private static List<string> GetViewDefinedEvents()


private static IDictionary<string, EventInfo> GetViewEvents(TView view, ICollection<string> actionProperties) private static IDictionary<string, EventInfo> GetViewEvents(TView view, ICollection<string> actionProperties)
{ {
IDictionary<string, EventInfo> viewEvents = new Dictionary<string, EventInfo>(); return view
view
.GetType() .GetType()
.GetEvents() .GetEvents()
.Where(x => Contains(actionProperties, x)) .Where(x => Contains(actionProperties, x))
.ToList() .ToList()
.ForEach(x => viewEvents.Add(new KeyValuePair<string, EventInfo>(x.Name, x))); .Select(x => new KeyValuePair<string, EventInfo>(x.Name, x))
return viewEvents; .ToDictionary(x => x.Key, x => x.Value);
} }


private static bool Contains(ICollection<string> actionProperties, EventInfo x) private static bool Contains(ICollection<string> actionProperties, EventInfo x)
Expand Down

0 comments on commit f85a251

Please sign in to comment.