通用存储库。需要建议
我正在考虑走哪条路,但无法决定哪条路更好。也许你可以给我一个杀手级的想法:)
我的问题:我有一个基于 NHibernate 构建的通用存储库。它的接口非常简单,比如Get(object id)、Save、Delete等。有时这个接口工作正常,但有时却不行。例如,我的项目中的几个实体都有一个名为 Code 的属性(但不是全部)。我希望不仅能够通过主键获取实体,而且还能够通过此属性获取实体。另一种情况是当我需要过滤某些带有日期的实体,或提供特定于各个实体的其他过滤标准时。目前我正在考虑以下解决方案:
- Linq - 我可以从存储库中为每种类型返回一个 IQueryable 对象,然后使用 linq 表达式过滤查询。我不喜欢这个想法,因为如果我这样做,我就看不到存储库背后的原因。
- 特定于实体的存储库 - 在这里,我可以在通用存储库之上构建特定于实体的实体。它将具有 Get(DateTime dateFrom, DateTime dateTo) 等方法。缺点是我必须为每个实体对象创建一个存储库类。好的一面是,从数据库中持久化/删除对象图会更容易,因为它知道实体的依赖关系。
- 请告诉我:)
I'm thinking which way to go and can't decide which one is better. Maybe you can give me the killer idea :)
My problem: I have a generic repository built on top of NHibernate. It's interface is very simple, such as Get(object id), Save, Delete etc. Sometime this interface works fine, but sometime it doesn't. For example, several entities in my project has a property named Code (but not all). I want to be able to get an entity not only by primary key, but also with this property. Another situation is when I need to filter some entities with dates, or provide other filtering criteria which are specific to individual entities. Currently I'm thinking of the following solutions:
- Linq - I can return an IQueryable object from Repository for each type, then filter the query using linq expressions. I don't like this idea, because if I go this way, I don't see the reason behind Repository.
- Entity specific repository - Here I can build an entity specific on top of the generic repository. It will have methods such as Get(DateTime dateFrom, DateTime dateTo) etc. The drawback is that I'll have to create a repository class for each entity object. The good side is that it will be much easyer to persist/delete an object graph from database, because it knows entity's dependencies.
- You tell me please :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在对通用存储库投入了大量精力之后,我更喜欢特定于实体的存储库。
我的这个答案(到目前为止有 36 票)详细阐述了这一立场,并包含一种您可能可以使用的方法:
为每个对象创建通用存储库与特定存储库相比有何优势?
I prefer entity-specific repositories, after putting a lot of effort into generic repositories.
This answer of mine (36 votes so far) elaborates on this stance and contains an approach you might be able to use:
Advantage of creating a generic repository vs. specific repository for each object?
鉴于您已经有一个通用存储库接口(我假设),我将使用特定于实体的存储库,但通过扩展通用存储库来实现它。这应该使代码更容易被发现,并允许您继续遵守存储库模式。这将允许您添加通过代码获取和通过传递日期范围获取的方法。
Given that you already have a generic repository interface (I assume) I would go with the Entity specific repository, but implement it by extending your generic repository. This should make the code more discoverable and allow you to continue to adhere to your repository pattern. This will allow you to add methods for getting by Code and for getting by passing in a date range.
我主张使用聚合存储库而不是特定于实体的存储库。例如,如果您有实体,那么
将对所有这些实体的数据访问组合到一个存储库中而不是三个单独的存储库中是有意义的。
I advocate using aggregate repositories rather than entity specific repositories. For example, if you have entities
it makes sense to combine the data access to all these in a single repository instead of three separate ones.
您可以使用命名查询来获取基于非键列的行,或者使用 Hibernate 查询语言 (hql) 进行任何类型的过滤,这与 sql 语句非常相似。
这可能会有所帮助。
You can use named queries to get rows based on column that is not a key or do any kind of filtering using Hibernate Query Language (hql) which is pretty similar to sql statements.
This might help.
我使用的东西看起来像这样:注意,它用于实体框架,但模型/设计可以在我猜的任何地方使用。
我从一些关于使用 IQueryable 的争论中找到了它,整个优点是,你可以轻松模拟 IRepository。
有关完整详细信息,请参阅此答案。
I use something that looks like this: Note, its for Entity Framework but the model/design could be used anywhere I guess.
I picked it up from a number of questions that debate on using IQueryable, the whole advantage is, you can mock IRepository easily.
See this answer for full details.