Jay Nathan's Weblog
Tuesday, June 08, 2004
 
.NET Delegates Simplified
First of all, think of delegates as a way to decouple objects from one another. When the President of the United States gets invited to a dinner with the NRA, he may not have time in his schedule to attend, but since this organization is important to him, he will send someone in his stead (a delegate) to attend on his behalf.

Now think about an ASP.NET appliction. The Page_Load(object sender, EventArgs e) functionof the ASP.NET Page class prescribes to a predefined method signature that is defined by the System.EventHandler delegate. The System.Web.UI.Page class does not implement the logic of the Page_Load event, you do that when the Page raises the Load event. So now, all the page class has to do is to notify everyone who is listening that the page has loaded via the Load event, and everyone can implement custom logic to react to the fact that the Load event has occured.

It may be simpler to look at how you might implement a delegate of your own. Lets say that you are creating a reusable login control that developers can drop on a page and use for them selves. You have implemented the logic to go out and check a security credentials data store, and you simply want to notify whoever is listening to the events on that control that the login either succeeded or failed.

The first thing you do is declare a custom delegate:


// define new delegate for my login event
public delegate void LoginEventHandler(object sender, bool loginSucceeded);


Now use that custom delegate when creating your login web control like this:

	
public class LoginControl : System.Web.UI.WebControls.WebControl
{
      // define loging event with new delegate type
      public event LoginEventHandler Login;

      private void Page_Load(object sender, System.EventArgs e)
      {

      }

      // create a handler method to raise the event when it occurs
      protected void OnLogin(bool loginSucceeded)
      {
            if(this.Login != null)
            this.Login(this, loginSucceeded);
      }

      // private method that contains my credential checking logic
      private bool DoLogon(string username, string password)
      {
            if(username == "jnathan" && password == "margaritaville")
                  return true;
            else
                  return false;
      }
}


Now, if I drop this control onto my page, I would subscribe to this event with the following line of code in the Page_Init event:

LoginControl.Login += new LoginEventHandler(this.LoginControl_Login);

You create the LoginControl_Login function in the code behind of the page that uses your Login control and respond to the event as you need to from there.

Check out the basis of the event model in .NET by researching the Observer design pattern [GOF].

Comments:
Jay,
It is good post and connection to the Observer pattern, I made the following write you awhile ago http://ipattern.com/simpleblog/PermLink.aspx?entryid=13.

Let me know what you think, Maxim

[www.ipattern.com do you?]
 
Thanks for the follow-up, Max. The above posting is a thorough explanation of delegates in the .NET framework to supplement this post.
 
pkkz bjrc siog [url=http://sbac.org/ralphlaureneaby.aspx]polo ralph lauren[/url] pkzlie mrlksx [url=http://www.longchampoutletsttaschen.info/]longchamp le pliage[/url] aicvww [url=http://www.deralphlaurensaleoutlet.info/]ralph lauren sale[/url] uvjblr gjibrj [url=http://www.monclerjackennoutlets.info/]www.monclerjackennoutlets.info[/url] tanfqw [url=http://www.longchampoutletsttaschen.info/]longchamp le pliage[/url] vqfjcy [url=http://sbac.org/de/monclereaby.aspx]moncler outlet[/url] gksuth mdoxhki
 
Post a Comment

<< Home

Powered by Blogger