views:

1654

answers:

8

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.

+5  A: 

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

bdukes
+1  A: 

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:

http://stackoverflow.com/questions/628803/delegate-usage-business-applications

http://stackoverflow.com/questions/31497/where-do-i-use-delegates

eglasius
+2  A: 

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.

WaldenL
Just out of curiousity: What's wrong about sorting in the database?
Adrian Grigore
There is absolutely nothing wrong in sorting in the DB, in fact with large amount of information you won't do without doing so
eglasius
@Adrian: I don't subscribe to this view, but those who do bring up three points: 1) Normally the db carries the heavy load, while clients have extra cpu. If you can move sorting to the client then you can improve scalability. [continued]
Joel Coehoorn
2) In n-Tier apps, including most web sites, sorting belongs logically on the presentation tier3) Sorting on the database means doing a completely new query/retrieval process every time the user re-orders a column in a grid. Keeping your sorts local can save that work.
Joel Coehoorn
That only works for small amount of information Joel. I might be wrong, but I would be shocked if SO sorted the questions outside the DB
eglasius
Some others reasons I thought of:-- Sorting on the database isn't good for chaining your API. For example, if one stored procedure get some data by calling another, where both the caller and the callee do a sort, that's wasted work.
Joel Coehoorn
I mean, that applies if you are retrieving the full result set, but whenever you can't pull the whole data because it is too big, you need to sort at the DB.
eglasius
@Freddy: true. But for larger sets you should implement paging, and paging != sorting. Also: remember that I don't subscribe to that viewpoint. I'm just listing their arguments.
Joel Coehoorn
Sorted on the DB != of doing on a sp, I use linq2sql a lot, and based on how you structure it you can avoid that issue completely.
eglasius
@Joel, I agree, don't take me wrong, I just don't want anyone misguided on this :) let me rephrase, if you will be using large amount of info, then you will probably be paging, in which case you need to do the sort at the DB :) i.e. on SO - paging for top voted questions
eglasius
There's no universal answer, but I added my answer to the right question in SO, I updated my answer here to reflect link.
WaldenL
Thanks. I'm not sure what you mean though. I understand not sorting at the DB with ORDER BY but not sure how this ties in with delegate. Do you have an example link?
johnny
+4  A: 

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.

Boydski
A: 

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)
{
   ...
}
Kelsey
+2  A: 

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.

Bryan Rowe
A: 

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

Jhonny D. Cano -Leftware-
A: 

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.
Helen Toomik