代理使用:业务应用程序 [英] Delegate Usage : Business Applications
问题描述
鉴于大多数开发人员是Business应用程序开发人员,我们最喜欢的编程语言的功能在我们正在使用的上下文中使用。
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.
除了事件,代理有多有用,以及Business Application上下文中哪些最有用?
Besides events, how useful are delegates, and in what Business Application contexts are they most useful?
更新: Jarrod Dixon 帮助链接到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?
推荐答案
我认为这个问题反映了皮肤一种猫的很多方法。我发现代表(和lambdas)几乎与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.
这里有一个我最近使用代理的上下文(格式化和名称更改为演示目的: )
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.
编辑:我猜Concat和OrderBy是3.0特定的,但这仍然是基本思想。
I guess Concat and OrderBy are 3.0-specific, but this is still the basic idea.
这篇关于代理使用:业务应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!