委托用途:商业应用用途、商业

2023-09-03 02:06:50 作者:Get lost (迷路)

由于最开发商是商务应用程序开发人员,我们最喜欢的编程语言的特点是什么,我们正在做与他们的情况下被使用。

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.

作为一个C#/ ASP.NET应用程序开发者,我往往只使用委托时UI事件处理。 !其实(这是我的经验不足显示的部分),我的甚至不知道一个好的环境以外的事件使用委托在的这是相当可怕的;但我收集有其他开发商在同一条船上。

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 :答案应该涉及到.NET 2.0。 .NET 3.0需要委托给不同的级别完全消失,而那将可能是一个单独的问题。

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.

另外的事件,是多么有用的代表,以及在什么商业应用环境是他们最有用?

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

更新:贾罗德迪克森的有益链接 MSDN文档的关于委托的用法,我必须承认,我的最喜欢的设计模式一书并没有带来了代表所有,所以我还没有真正看到他们在使用中比UI事件等。为了扩大这个问题(只是一点点!),你可以给什么例子为业务应用程序(或者说真的,任何应用程序不得不面对一个涉及-能问题),这将使其更容易消化的关于这一主题的MSDN文档?

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?

推荐答案

我认为这个问题反映了很多方法对皮肤一只猫。我觉得代表(和lambda表达式)几乎为根本,以for循环。

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

下面是在我最近使用过的一位代表环境(格式和名字更改为presentation目的:)

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();
}

因此​​,这排序的一组基于一些标准的物品,然后将其或者返回整个列表排序,或者它打破它一分为二,分别排序两半,并提出一个分离器在它们之间。祝你好运这样优雅,如果你不能前preSS的方式进行排序的东西,这是该委托是什么。这个概念

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.

编辑:我想Concat的和排序依据是3.0特有的,但是这仍然是基本的想法

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