Delegate Usage : Business Applications(委托用法:业务应用程序)
问题描述
鉴于大多数"开发人员都是业务应用程序开发人员,我们最喜欢的编程语言的功能在我们正在使用它们的上下文中使用.
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.
注意:答案应与 .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?
更新:Jarrod Dixon 有帮助的链接到 MSDN 文档 关于委托使用,我必须承认我的 favorite Design Patterns Book 根本没有提出委托,所以除了用于 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.
这是我最近使用代表的一个上下文(格式和名称已更改以用于演示目的:)
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.
这篇关于委托用法:业务应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:委托用法:业务应用程序
基础教程推荐
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01