title: WeakEventManager with event name lambda expression and custom event accessors
body: <p>I have been looking in to subscribing to an event using a weak event pattern. With the .NET 4.5 framework, we have a slick looking <a href="http://msdn.microsoft.com/en-us/library/hh199438.aspx" rel="nofollow noreferrer">WeakEventManager</a> class. Weakly subscribing to an event is as simple as</p>
  
  <pre><code>WeakEventManager&lt;EventSource, SomeEventEventArgs&gt;.AddHandler(source, "SomeEvent", source_SomeEvent);
  </code></pre>
  
  <p>I'm not a big fan of 'stringly-typed' code however. I have been trying to find a way around using the string name of the event to subscribe to. The <a href="https://stackoverflow.com/questions/2392152/retrieving-event-name-from-lambda-expression">only way</a> I have found to obtain the name of the event is using a lambda expression in the class that defines the event. In my scenario, I own the class defining the event so I can change it however I like. I have been trying to find a clean way to subscribe and unsubscribe to my event and here is what I disliked the least.</p>
  
  <pre><code>public event EventHandler&lt;EventArgs&gt; LoggingOn;
  public event EventHandler&lt;EventArgs&gt; LoggingOn_Weak
  {
      add
      {
          var eventName = this.GetEventName(() =&gt; this.LoggingOn);
          WeakEventManager&lt;CurrentUser, EventArgs&gt;.AddHandler(this, eventName, value);
      }
  
      remove
      {
          var eventName = this.GetEventName(() =&gt; this.LoggingOn);
          WeakEventManager&lt;CurrentUser, EventArgs&gt;.RemoveHandler(this, eventName, value);
      }
  }
  
  // In a base class view model in my scenario
  private string GetEventName&lt;T&gt;(System.Linq.Expressions.Expression&lt;Func&lt;T&gt;&gt; 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);
      }
  }
  </code></pre>
  
  <p>Using <a href="http://msdn.microsoft.com/en-us/library/bb882534.aspx" rel="nofollow noreferrer">custom event accessors</a> I was able to avoid clunky (in my opinion) methods like LoggingOn_Subscribe(EventHandler) or adding name properties for each event. Unfortunately it is not so intuitive in that people subscribing to the event are doing so in the classic manner but have no idea other than the "_Weak" part of the name that indicates it is being subscribed to weakly.</p>
  
  <p>As for my questions..</p>
  
  <p>1) I have never used weak events or custom event accessors before. The code above appears to work, however, I would just like to make sure there is nothing technically wrong with it. Is there anything I'm doing here to shoot myself in the foot?</p>
  
  <p>2) From a design perspective, is this a terrible idea? Are there any major design concerns I should consider? Is there better alternative? Should i just suck it up and subscribe from my subscriber using a stringly-typed event name?</p>
  
  <p>Thoughts?</p>