WeakEventManager with lambda expression with event name and special event assemblers

I searched for an event subscription using a weak event pattern. With the .NET 4.5 platform, we have the sleek WeakEventManager class. Slightly subscribing to an event is as easy as

WeakEventManager<EventSource, SomeEventEventArgs>.AddHandler(source, "SomeEvent", source_SomeEvent); 

However, I am not a big fan of string input. I am trying to find a way to use a string name for a subscription. the only way I found to get the name of the event uses the lambda expression in the class that defines the event. In my scenario, I have a class that defines an event, so I can change it as I like. I tried to find a clean way to subscribe and unsubscribe to my event, and here's what I didn't like.

 public event EventHandler<EventArgs> LoggingOn; public event EventHandler<EventArgs> LoggingOn_Weak { add { var eventName = this.GetEventName(() => this.LoggingOn); WeakEventManager<CurrentUser, EventArgs>.AddHandler(this, eventName, value); } remove { var eventName = this.GetEventName(() => this.LoggingOn); WeakEventManager<CurrentUser, EventArgs>.RemoveHandler(this, eventName, value); } } // In a base class view model in my scenario private string GetEventName<T>(System.Linq.Expressions.Expression<Func<T>> expression) { return (expression.Body as System.Linq.Expressions.MemberExpression).Member.Name; } protected void OnLoggingOn(object sender, EventArgs e) { var handler = this.LoggingOn; if (handler != null) { handler(sender, e); } } 

Using custom event assemblers I managed to avoid clumsy (in my opinion) methods, such as LoggingOn_Subscribe (EventHandler) or adding name properties for each event. Unfortunately, this is not so intuitive that people who sign up for the event do it classically, but have no idea, except for the "_Weak" part of the name, which indicates that it is poorly subscribed.

As for my questions ..

1) I've never used weak events or custom event accessors before. The above code seems to work, however I would just like to make sure there is nothing technically with it. Is there something I am doing here to shoot the leg?

2) In terms of design, is this a terrible idea? Are there any serious design issues that I should consider? Is there a better alternative? Should I just suck it and subscribe from my subscriber using a string-typed event name?

Thoughts?

+4
source share
2 answers

With .NET 4.6, you can now use the nameof () expression:

 WeakEventManager<IMyGrid, MyEventArgs>.AddHandler(myGrid, nameof(IMyGrid.MouseDown), OnMouseDown); 
+1
source

What you can do is use the built-in System.ComponentModel.EventHandlerList. This class is a container for all object event handler delegates. The main advantage is that no storage is allocated for each object for each event, if in fact someone did not subscribe to the event.

The second advantage is that to use it you must provide a key for your event.

 class MyObject { protected EventHandlerList Events = new EventHandlerList(); public static Event1Key = new object(); public event Event1 { add { Events.AddHandler(Event1Key, value); } remove { Events.RemoveHandler(Event1Key, value); } } } 

Now you can create a variation of WeakEventManager that accepts keys, not row names. So the consumer could say

 WeakEventManager<EventSource, SomeEventEventArgs>.AddHandler(source, EventSource.Event1Key, source_SomeEvent); 
-1
source

Source: https://habr.com/ru/post/1442324/


All Articles