Business Objects Collection 中具有 lambda 标准的通用方法

发布于 2024-07-19 00:28:30 字数 370 浏览 5 评论 0原文

我的应用程序中的每个业务对象都有抽象类 (BO) 还有父(通用)集合来存储所有 BO(名为 BOC - 业务对象集合)

我想要存档的是在 BOC 内创建的通用方法,类似于:

public  T GetBusinessObject (lambda_criteria)

所以我可以调用类似于以下的方法:

Employee emp = BOC.GetBusinessObject( Employee.ID=123 )

并且方法应返回满足指定 lambda 标准的 BO

有如何存档的提示吗?

I have abstract class for each Business Object in my app (BO)
Also have parent (generic) collection to store all BOs (named BOC - Business Object Collection)

What I would like to archive is created universal method iniside BOC similar to:

public  T GetBusinessObject (lambda_criteria)

So I could call this method similar to:

Employee emp = BOC.GetBusinessObject( Employee.ID=123 )

And method shall return BO meet to specified lambda criteria

Any tips how to archive that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

在巴黎塔顶看东京樱花 2024-07-26 00:28:30

假设您的 BOC 在内部继承或使用通用列表,这并不太难:

T GetBusinesObject<T>( Func<T, bool> predicate )
{
    return this.FirstOrDefault( predicate );
}

Assuming that your BOC inherits from or uses a generic list internally it's not too hard:

T GetBusinesObject<T>( Func<T, bool> predicate )
{
    return this.FirstOrDefault( predicate );
}
空心↖ 2024-07-26 00:28:30

类似的东西

T GetBusinessObject<T>(Predicate<T> constraint) where T : BO

,然后

GetBusinessObject<Employee>( e => e.ID = 123 )

可能会被称为类似的东西?

Something like

T GetBusinessObject<T>(Predicate<T> constraint) where T : BO

and then called like

GetBusinessObject<Employee>( e => e.ID = 123 )

perhaps?

擦肩而过的背影 2024-07-26 00:28:30

如果这是一个通用集合,那么调用者应该已经(通过 LINQ)能够使用:

BOC<Employee> boc = ...
// checks exactly 1 match, throws if no match
var emp = boc.Single(e => e.ID = 123);
// checks at most 1 match, null if no match
var emp = boc.SingleOrDefault(e => e.ID = 123);
// checks at least one match, throws if no match
var emp = boc.First(e => e.ID = 123);
// returns first match, null if no match
var emp = boc.FirstOrDefault(e => e.ID = 123);

那可以吗?

If this is a generic collection, then the caller should already (via LINQ) be able to use:

BOC<Employee> boc = ...
// checks exactly 1 match, throws if no match
var emp = boc.Single(e => e.ID = 123);
// checks at most 1 match, null if no match
var emp = boc.SingleOrDefault(e => e.ID = 123);
// checks at least one match, throws if no match
var emp = boc.First(e => e.ID = 123);
// returns first match, null if no match
var emp = boc.FirstOrDefault(e => e.ID = 123);

that do?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文