如何为 DAL 构建理解 lambda 的谓词或条件构建器?

发布于 2024-08-14 19:00:30 字数 548 浏览 2 评论 0原文

我正在研究数据访问层设计,我们还没有最终确定我们将使用什么 ORM。 我倾向于 NHibernate + FluentMappings + Nhibernate.Linq,但根据项目时间表,我们甚至可以等待 EF4。我想替换诸如 :

IList<Customer> FindById(int id);

IList<Customer> FindByName(string fullName);

IList<Customer> FindByCriteria(Func<Customer, bool> criteria);

类的方法,甚至

IList<T> FindByCriteria(Func<T, bool> criteria)

这样的想法是根据需求(来自 UI 或 Business )动态链接或构建标准到存储库或 DAO 对象。欢迎任何代码示例、链接、博客文章、提示。

提前致谢!

I am working on a Data Access Layer Design, we have not finalized what ORM we are going to use as of yet.
I am leaning towards NHibernate + FluentMappings + Nhibernate.Linq but depending on project timelines we could even wait for EF4. I want to replace methods like :

IList<Customer> FindById(int id);

IList<Customer> FindByName(string fullName);

to

IList<Customer> FindByCriteria(Func<Customer, bool> criteria);

or even

IList<T> FindByCriteria(Func<T, bool> criteria)

so the idea is to chain or build a criteria based on the requirement (from UI or Business ) dynamically to a repository or DAO object. Any code samples, links , blog posts , hints are welcome.

Thanks in advance!

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

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

发布评论

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

评论(3

酒绊 2024-08-21 19:00:30

如果您只想使用 lambda,则必须处理 System.LINQ.Expression 类。
它允许您访问 lambda 代表的代码。

您可能也会对 IQueryable 周围的类型感兴趣。

If you want to work only with lambdas you will have to deal with the System.LINQ.Expression class.
It allows you to access the code the lambda represents.

The types around IQueryable may also be interesting for you.

ζ澈沫 2024-08-21 19:00:30

链接到以下系列的第一篇文章。不是 100% 正确,但一般概念适用。文章中使用的ORM是LLBLGen Pro,但应该适用于任何成熟的ORM框架:

动态搜索对象第 1 部分 - 简介

Link to the first post of a series below. Not 100% on point, but the general concept applies. The ORM used in the article is LLBLGen Pro, but should be applicable to any mature ORM framework:

Dynamic Search Objects Part 1–Introduction

冷︶言冷语的世界 2024-08-21 19:00:30

如果您正在编写 ORM,那么您应该问这个问题。

如果您正在编写一个基于 ORM 构建的应用程序,它应该已经能够表达通用查询(例如 LINQ 表达式)。您的应用程序的任务是将通用查询功能利用到特定于系统的查询中,例如“按名称查找客户”。

通用查询是直接位于特定于应用程序的查询之下的抽象级别。存储库本身不应包含通用功能。

请在此处查看我的答案以获取更多信息和代码示例:

为每个对象创建通用存储库与特定存储库的优点?

If you are writing an ORM, then you should be asking this question.

If you are writing an application built on top of an ORM, it should already have the capability to express generic queries (such as LINQ expressions). Your task for your application is to harness that generic query capability into system-specific queries like "find customer by name".

Generic queries are a level of abstraction directly below application-specific queries. Repositories should not themselves contain generic capabilities.

See my answer here for more info and a code sample:

Advantage of creating a generic repository vs. specific repository for each object?

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