简体   繁体   中英

When would I use a delegate in asp.net?

I'm always looking for a way to use all the tools I can and to stretch myself just beyond where I am at. But as much as I have read about delegates, I can never find a place to use them (like Interfaces, Generics, and a lot of stuff, but I digress.) I was hoping someone could show me when and how they used a delegate in web programming for asp.net c#(2.0 and above).

Thank you and if this wrong for Stack Overflow, please just let me know.

bdukes is right about events. But you're not limited to just using delegates with events.

Study the classic Observer Pattern for more examples on using delegates. Some text on the pattern points toward an event model, but from a raw learning perspective, you don't have to use events.

One thing to remember: A delegate is just another type that can be used & passed around similar to your primitive types such as an "int". And just like "int", a delegate has it's own special characteristics that you can act on in your coding when you consume the delegate type.

To get a really great handle on the subject and on some of it's more advanced and detailed aspects, get Joe Duffy's book, .NET Framework 2.0 .

Well, whenever you handle an event, you're using a delegate.

To answer your second question first, I think this is a great question for StackOverflow!

On the first, one example would be sorting. The Sort() method on List takes a delegate to do the sorting, as does the Find() method. I'm not a huge fan of sorting in the database, so I like to use Sort() on my result sets. After all, the order of a list is much more of a UI issue (typically) than a business rule issue.

Edit: I've added my reasons for sorting outside the DB to the appropriate question here .

Edit: The comparison function used in the sort routine is a delegate. Therefore, if you sort a List using the.Sort(Comparison(T)) method the Comparison(T) method you pass to the sort function is a delegate. See the.Sort(Comparison(T)) documentation .

Another quick example off the top of my head would be unit testing with Rhino Mocks. A lot of the things you can do with Rhino Mocks utilize delegates and lambda expressions.

You can use delegates whenever you know you will want to take some action, but the details of that action will depend on circumstances.

Among other things, we use delegates for:

  • Sorting and filtering, especially if the user can choose between different sorting/filtering criteria
  • Simplifying code. For example, a longish process where the beginning and end are always the same, but a small middle bit varies. Instead of having a hard-to-read if block in the middle, I have one method for the whole process, and pass in a delegate (Action) for the middle bit.
  • I have a very useful ToString method in my presentation layer which converts a collection of anything into a comma-separated list. The method parameters are an IEnumerable and a Func delegate for turning each T in the collection into a string. It works equally well for stringing together Users by their FirstName or for listing Projects by their ID.

There isn't anything special to asp.net related to delegates (besides considerations when using async stuff, which is a whole different question), so I will point you to other questions instead:

Delegate Usage: Business Applications

Where do I use delegates?

Another example would be to publish events for user controls.

Eg.

// In your user control
public delegate void evtSomething(SomeData oYourData);
public event evtSomething OnSomething;

// In the page using your user control
ucYourUserControl.OnSomething += ucYourUserControl_OnSomething;

// Then implement the function
protected void ucYourUserControl_OnSelect(SomeData oYourData)
{
   ...
}

Recently i used the delegates for "delegating" the checking of the permissions.

public Func CheckPermission;

This way, the CheckPermission function can be shared by various controls or classes, say it in a static class or a utilities class, and still be managed centralized, avoiding also Interface explossion; just a thought

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM