什么是存储库模式的最佳实践 - 每桌回购? [英] What is best practise for repository pattern - repo per table?
问题描述
Repository模式似乎运作良好与几大主表的初始项目时。
The repository pattern seems to work well when working with an initial project with several large main tables.
然而,随着项目的发展似乎有点不灵活。假设你有很多是挂完主表子表,你需要为每个表的存储库?
However as the project grows it seems a little inflexible. Say you have lots of child tables that hang off the main table, do you need a repository for each table?
例如:
CustomerAddress记录具有下列子表:
CustomerAddress Record has following child tables:
- >县
- >国家
- > CustomerType
-> CustomerType
在用户界面上,3下拉列表需要被显示,但它变得有点乏味写作存储库每一个选择的下拉菜单中的数据上表的。
On the UI, 3 dropdown lists need to be displayed, but it gets a bit tedious writing a repository for each of the above tables which selects the data for the dropdowns.
有没有这个做什么?
举个例子说,你有一个主CustomerAddress库我的猜测是聚合根,这从基地回购接口继承的主要CRUD操作。
As an example say you have a main CustomerAddress repository which I guess is the 'aggregate root' which inherits the main CRUD operations from the base repo interface.
以前我有短板缺的聚合根直去为这些类型的表的情况下。
Previously I have short-cutted the aggregate root and gone straight to the context for these kinds of tables.
例如
public Customer GetCustomerById(int id)
{
return Get(id);
}
public IEnumerable<Country> GetCountries()
{
return _ctx.DataContext.Countries.ToList();
}
等等...
etc...
但有时不的感觉的权利,因为国家并没有对客户的一部分,但我觉得我需要把它钉到的东西,而不必为每个表创建回购不计其数。每桌回购肯定看起来不正确对我来说不是。
But sometimes it doesn't feel right, as countries aren't part of the customer, but I feel like I need to tack it onto something without having to create zillions of repos for each table. A repo per table definately doesn't seem right to me either.
推荐答案
我在这里回答我的问题,因为当建议是当然是有用的,我觉得我有一个更好的解决方案。
虽然我没有phsyically创建每个底层存储库和每个表,因为我有界面(获取,添加,删除)的通用存储库基类的,我还是得:
I'm answering my own question here because while the suggestions are certainly useful, I feel I have a better solution. While I don't have to phsyically create the underlying repository for each and every table as I have a generic repository base class with interface (Get, Add, Remove), I still have to:
1)写访问任何特定的方法(通常这些查询)
1) write the interface to access any specialised methods (generally these are queries)
2)写那些实现了接口
2) write those implementations
我并不想这样做,当所有我想要检索的或国家的名单一些简单的类型填充下拉列表。想想,如果你有10引用类型表所需的努力。
I don't necessarily want to do this when all I want to retrieve is a list of countries or some simple type for populating a dropdown. Think of effort required if you have 10 reference type tables.
我决定做的是创建一个名为SimpleRepo与ISimpleRepo接口,暴露了1-2的方法的新类。虽然我通常不喜欢揭露的IQueryable接口出回购I / F类的,我也不在这里,我想提供的灵活性介意。我可以简单地揭露了查询()方法,该方法提供了灵活性挂钩。 。我可能需要这个专业的顺序,或者过滤
What I decided to do was create a new class called SimpleRepo with ISimpleRepo interface which exposes 1-2 methods. While I don't normally like to expose the IQueryable interface out of the repo i/f class, I don't mind here as I want the provided flexibility. I can simply expose a 'Query()' method which provides the flexibility hook. I might need this for specialising the ordering, or filtering.
当一个服务需要利用一些简单的数据,该ISimple<的; T>接口传入,其中T是该表/类
Whenever a service needs to make use of some simple data, the ISimple< T > interface is passed in, where T is the table/class.
我现在避免需要创建一个接口/类为这些简单的数据的块。
思考的人?
I now avoid the need to create an interface/class for these simple pieces of data. Thoughts anyone?
这篇关于什么是存储库模式的最佳实践 - 每桌回购?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!