linq to nhibernate 在查询中重用业务逻辑
我正在开发一个项目,我们使用 Fluent nhibernate 并在我们的存储库上执行实体查询。通常我们会这样编写查询:
(from person in repository.Query<Person>()
where person.Age > 18
where person.Age < 50
select person).Single();
显然我们这里有一些逻辑,我们希望能够将其封装在更合理的地方。一个理想的解决方案是这样做:
(from person in repository.Query<Person>()
where personIsTheRightAge(person)
select person).Single();
bool personIsTheRightAge(Person person)
{
return person.Age > 18 && person.Age < 50;
}
但是 nhibernate 不知道如何处理这个问题。
我们可以为 IQueryable< 提供扩展方法。人>但如果我查询具有驾驶员 Person 的 Car 实体并且我需要重用相同的逻辑,那么这将不起作用。
我只是想知道是否有人对如何以易于在项目中重复使用的方式解决这个问题有一些好主意。
预先感谢您的任何帮助。
I'm working on a project where we are using fluent nhibernate and perform queries on our repository for entities. Often we write queries like this:
(from person in repository.Query<Person>()
where person.Age > 18
where person.Age < 50
select person).Single();
Obviously we have some logic here and we would like to be able to encapsulate it somewhere more sensible. An ideal solution would be to do this:
(from person in repository.Query<Person>()
where personIsTheRightAge(person)
select person).Single();
bool personIsTheRightAge(Person person)
{
return person.Age > 18 && person.Age < 50;
}
But nhibernate doesn't know how to deal with this.
We could provide extension methods to IQueryable< Person> but that won't work if I'm querying a Car entity that has a driver Person and I need to reuse that same logic.
I'm just wondering if anyone has some nice ideas about how to solve this problem in a way that is easy to repeatedly use across a project.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 DDD 规范 之类的东西来封装“正确的年龄”逻辑:
并且
您可能还会发现它很有趣看看存储库是如何在 DDD 中实现。
You may use something like DDD Specification to encapsulate 'right age' logic:
and
You might also find it interesting to look at how repositories are implemented in DDD.