Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate Usage : Business Applications

Background

Given that 'most' developers are Business application developers, the features of our favorite programming languages are used in the context of what we're doing with them.

As a C# / ASP.NET Application developer, I tend to only use delegates when dealing with UI events. In fact (and this is part of my inexperience showing), I don't even know a good context other than events to use delegates in! This is quite scary; but I'm gathering that there are other developers in the same boat.

NB: Answers should pertain to .NET 2.0. .NET 3.0 takes delegates to a different level entirely, and that'll likely be a separate question.

Question:

Besides events, how useful are delegates, and in what Business Application contexts are they most useful?

Update: Jarrod Dixon helpfully linked to the MSDN documentation regarding delegate usage, and I must admit that my favorite Design Patterns Book didn't bring up delegates at all, so I haven't really seen them in use other than for UI events. To expand this question (just a little bit!), What examples can you give for business applications (or really, any application having to deal with a relate-able problem) that would make it easier to digest the MSDN documentation on the subject?

like image 747
George Stocker Avatar asked Mar 10 '09 03:03

George Stocker


3 Answers

I think this question reflects the many ways to skin a cat. I find delegates (and lambdas) nearly as fundamental as a "for" loop.

Here's one context in which I used delegates recently (formatting and names changed for presentation purposes:)

protected T[] SortLines<T>(Func<T> createLine, IEnumerable<T> unsorted)
where T : LineType
{
    Func<IEnumerable<T>, IEnumerable<T>> sorter = (lines => lines);

    switch (settings.OrderSort)
    {
        case OrderSort.ByA: 
            sorter = (lines => lines.OrderBy(x => x.A)); break;
        case OrderSort.ByB:
            sorter = (lines => lines.OrderBy(x => x.B)); break;

        // and so on... a couple cases have several levels of ordering
    }

    bool requiresSplit = // a complicated condition
    if (requiresSplit)
    {
        var positives = unsorted.Where(x => x.Qty >= 0);
        var negatives = unsorted.Where(x => x.Qty <  0);

        return sorter(negatives).Concat(
               new T[] { createLine.Invoke() }).Concat(
               sorter(positives)).ToArray();
    }
    else
        return sorter(unsorted).ToArray();
}

So this sorts a group of items based on some criteria, and then it either returns the whole list sorted, or it breaks it in two, sorts both halves separately, and puts a separator in between them. Good luck doing this elegantly if you can't express the concept of "a way to sort something", which is what the delegate is for.

EDIT: I guess Concat and OrderBy are 3.0-specific, but this is still the basic idea.

like image 118
mqp Avatar answered Sep 30 '22 09:09

mqp


Other than GUI...

  1. event dispatching; some of my business apps are quite complicated, talk to hardware devices, and rely on event queues to keep everything in synch. Delegates are used by these apps for event dispatching.
  2. business rules; some of my business apps have a partial soft-coding ability, where certain events trigger certain rules that are kept in a database. Delegates (in a Dictionary) are used to execute the rules on the client-side. (Plug-ins could be supported, but current are not needed).
  3. general secondary threads (using the SafeThread class, of course!)
like image 33
Steven A. Lowe Avatar answered Sep 30 '22 09:09

Steven A. Lowe


To my knowledge, a .NET delegate is essentially an implementation of a single-method interface, without all the class declaration hoopla. I wish we had them in Java, personally. Think of a comparator class:

class MyComparator<Circle> extends Comparator<Circle> {
    public int compare(Circle a, Circle b) {
        return a.radius - b.radius;
    }
}

Anyplace this pattern is useful, a delegate could be useful instead.

I hope I'm right, but go ahead and vote me down if I'm wrong; it's been too long since I saw any C# :)

like image 25
easeout Avatar answered Sep 30 '22 10:09

easeout