I am in the process is designing a website in ASP.NET MVC and am perhaps a little confused as to the exact nature of a repository.
我正在设计一个在ASP中的网站。NET MVC和我可能对存储库的确切性质有点困惑。
Following the NerdDinner example, my site should have one repository which serves up the entities as I need them. However, I have also heard that you should have different repositorys that deal with specific sets of related entities....?
遵循NerdDinner示例,我的站点应该有一个存储库,根据需要为实体提供服务。然而,我还听说你应该有不同的存储库,处理相关的特定组实体.... ?
In the case of my site, there will be a number of entities (around 15 tables) yet the majority are all related. Is it ok / advisable to have one repository that contains all the methods that I'll need for pulling / updating / deleting etc or should I split them down?
在我的站点中,将有许多实体(大约15个表),但是大多数实体都是相关的。是否可以/建议拥有一个存储库,其中包含所有我需要的用于提取/更新/删除等的方法,还是应该将它们拆分?
8 个解决方案
#1
2
I create a repository for each data object.
我为每个数据对象创建一个存储库。
For example, a simple library database could contain the following repositories:
例如,一个简单的库数据库可以包含以下存储库:
- AuthorRepository
- AuthorRepository
- BookRepository
- BookRepository
- PublisherRepository
- PublisherRepository
#2
8
I use a generic repository which is plenty for many entities.
我使用一个通用的存储库,它对于许多实体来说都是足够的。
For a more complex one, I simply extend this with what's needed. The best of both worlds really.
对于更复杂的问题,我只是用需要的东西扩展它。这真是两全其美。
#3
8
In domain driven design, there's a rule that repositories are per aggregate root. You can read more about it here.
在域驱动设计中,有一条规则,即存储库是每个聚合根。你可以在这里读到更多。
The more I read, the more I think that NerdDinner is too often seen as a collection of good practices, while it's absolutely not (see here for a discussion of, particularly, NerdDinner repository). That's why people often blame other MS examples like Oxite (and here:
我读得越多,就越觉得NerdDinner常常被看作是一组好的实践,而它绝对不是(请参阅这里讨论NerdDinner储存库)。这就是为什么人们经常指责像Oxite这样的MS例子。
Developers will flock to it, praise it, and blindly accept it as gospel because it comes from Microsoft (it's already well on its way). Sadly, any developer which adopts its spirit will be left with an unmaintainble, untestable and unreadable mess
开发人员会蜂拥而至,称赞它,并盲目地接受它,因为它来自微软(它已经在路上了)。遗憾的是,任何采用其精神的开发人员都将陷入不可维护、不可测试和不可读的混乱
).
)。
#4
5
If you use a generic repository which accepts types then I don't see any reason to use more than one.
如果您使用一个接受类型的通用存储库,那么我认为没有任何理由使用多个类型。
we use an interface like this:
我们使用这样的界面:
public interface IRepository
{
void Save<ENTITY>(ENTITY entity)
where ENTITY : Entity;
void Delete<ENTITY>(ENTITY entity)
where ENTITY : Entity;
ENTITY Load<ENTITY>(int id)
where ENTITY : Entity;
IQueryable<ENTITY> Query<ENTITY>()
where ENTITY : Entity;
IList<ENTITY> GetAll<ENTITY>()
where ENTITY : Entity;
IQueryable<ENTITY> Query<ENTITY>(IDomainQuery<ENTITY> whereQuery)
where ENTITY : Entity;
ENTITY Get<ENTITY>(int id) where ENTITY : Entity;
IList<ENTITY> GetObjectsForIds<ENTITY>(string ids) where ENTITY : Entity;
void Flush();
}
then use in code like this:
然后像这样在代码中使用:
var returnedObjects = repository.GetAll<ObjectClass>();
var singleObject = repository.Get<ObjectClass>(id);
#5
2
I think perhaps the verbiage of what is a repository might be confusing you. To me a repository is the data storage (ie; MS SQL Database) of where your data is being stored into.
Following the Repository Pattern I recommend setting up a single respository for each datastore. Most of my projects I use MS SQL so I create a Repository for that DB (I like using Subsonic for my DAL/ORM and it also implements the Repositry pattern and the ActiveRecord pattern) then I create Factories for each table. This lets me wrap up the Subsonic ActiveREcord classes and gives me abstraction.
我想可能是对什么是存储库的描述让你感到困惑。对我来说,存储库就是数据存储(即;MS SQL数据库),您的数据存储在什么地方。按照存储库模式,我建议为每个数据存储设置一个单独的响应。我的大多数项目都使用MS SQL,因此我为DB创建了一个存储库(我喜欢使用Subsonic作为我的DAL/ORM,它还实现了重新定位模式和ActiveRecord模式),然后为每个表创建工厂。这让我可以总结亚音速ActiveREcord类并给出抽象。
Hope thats helpfull, perhaps...
希望这经验,也许……
#6
2
You should not create Repositories per each table. As queen3 said, you should create Repository per aggregate root. Like, if Products can have a Category, Category Repository should be a nested class of Products. Follow the domain logic relationship than domain objects.
不应该为每个表创建存储库。正如queen3所说,您应该为每个聚合根创建存储库。类似地,如果产品可以有一个类别,那么类别存储库应该是一个嵌套的产品类。遵循域逻辑关系而不是域对象。
#7
1
Queen3 is right, you can follow that Aggregate Root theory. I basically group my repository not thinking in Entities but how they group logically in the application I'm building.
Queen3是对的,你可以遵循聚合根理论。我基本上是将我的存储库分组,而不是考虑实体,而是考虑它们如何在我正在构建的应用程序中进行逻辑分组。
For example:
例如:
CustomersRepository
OrdersRepository
...
In CustomerRepository I would put methods for GetCustomers, GetCustomer, AddCustomer, DeleteCustomer, AddCustomerContact, DeleteCustomerContact.
在CustomerRepository中,我将为GetCustomers、GetCustomer、AddCustomer、DeleteCustomer、AddCustomerContact、DeleteCustomerContact设置方法。
In OrdersRepository I would put methods for GetOrders, GetOrder, AddOrder, CancelOrder, CloneOrder, AddOrderDetail, DeleteOrderDetail and so on.
在OrdersRepository中,我将放置GetOrders、GetOrder、AddOrder、CancelOrder、CloneOrder、AddOrderDetail、DeleteOrderDetail等方法。
#8
0
I tend to use a repository per related group of entitites. i.e orderrepository might have:
我倾向于对每一组相关的实体使用一个存储库。我。e orderrepository可能有:
Order, and OrderDetail.
秩序,OrderDetail。
and would have another for, say, Customer, CustomerProfile, etc.
顾客会有另一个,比如,顾客,个人资料等等。
This keeps the repository classes neat.
这使存储库类保持整洁。
Davy
戴维
#1
2
I create a repository for each data object.
我为每个数据对象创建一个存储库。
For example, a simple library database could contain the following repositories:
例如,一个简单的库数据库可以包含以下存储库:
- AuthorRepository
- AuthorRepository
- BookRepository
- BookRepository
- PublisherRepository
- PublisherRepository
#2
8
I use a generic repository which is plenty for many entities.
我使用一个通用的存储库,它对于许多实体来说都是足够的。
For a more complex one, I simply extend this with what's needed. The best of both worlds really.
对于更复杂的问题,我只是用需要的东西扩展它。这真是两全其美。
#3
8
In domain driven design, there's a rule that repositories are per aggregate root. You can read more about it here.
在域驱动设计中,有一条规则,即存储库是每个聚合根。你可以在这里读到更多。
The more I read, the more I think that NerdDinner is too often seen as a collection of good practices, while it's absolutely not (see here for a discussion of, particularly, NerdDinner repository). That's why people often blame other MS examples like Oxite (and here:
我读得越多,就越觉得NerdDinner常常被看作是一组好的实践,而它绝对不是(请参阅这里讨论NerdDinner储存库)。这就是为什么人们经常指责像Oxite这样的MS例子。
Developers will flock to it, praise it, and blindly accept it as gospel because it comes from Microsoft (it's already well on its way). Sadly, any developer which adopts its spirit will be left with an unmaintainble, untestable and unreadable mess
开发人员会蜂拥而至,称赞它,并盲目地接受它,因为它来自微软(它已经在路上了)。遗憾的是,任何采用其精神的开发人员都将陷入不可维护、不可测试和不可读的混乱
).
)。
#4
5
If you use a generic repository which accepts types then I don't see any reason to use more than one.
如果您使用一个接受类型的通用存储库,那么我认为没有任何理由使用多个类型。
we use an interface like this:
我们使用这样的界面:
public interface IRepository
{
void Save<ENTITY>(ENTITY entity)
where ENTITY : Entity;
void Delete<ENTITY>(ENTITY entity)
where ENTITY : Entity;
ENTITY Load<ENTITY>(int id)
where ENTITY : Entity;
IQueryable<ENTITY> Query<ENTITY>()
where ENTITY : Entity;
IList<ENTITY> GetAll<ENTITY>()
where ENTITY : Entity;
IQueryable<ENTITY> Query<ENTITY>(IDomainQuery<ENTITY> whereQuery)
where ENTITY : Entity;
ENTITY Get<ENTITY>(int id) where ENTITY : Entity;
IList<ENTITY> GetObjectsForIds<ENTITY>(string ids) where ENTITY : Entity;
void Flush();
}
then use in code like this:
然后像这样在代码中使用:
var returnedObjects = repository.GetAll<ObjectClass>();
var singleObject = repository.Get<ObjectClass>(id);
#5
2
I think perhaps the verbiage of what is a repository might be confusing you. To me a repository is the data storage (ie; MS SQL Database) of where your data is being stored into.
Following the Repository Pattern I recommend setting up a single respository for each datastore. Most of my projects I use MS SQL so I create a Repository for that DB (I like using Subsonic for my DAL/ORM and it also implements the Repositry pattern and the ActiveRecord pattern) then I create Factories for each table. This lets me wrap up the Subsonic ActiveREcord classes and gives me abstraction.
我想可能是对什么是存储库的描述让你感到困惑。对我来说,存储库就是数据存储(即;MS SQL数据库),您的数据存储在什么地方。按照存储库模式,我建议为每个数据存储设置一个单独的响应。我的大多数项目都使用MS SQL,因此我为DB创建了一个存储库(我喜欢使用Subsonic作为我的DAL/ORM,它还实现了重新定位模式和ActiveRecord模式),然后为每个表创建工厂。这让我可以总结亚音速ActiveREcord类并给出抽象。
Hope thats helpfull, perhaps...
希望这经验,也许……
#6
2
You should not create Repositories per each table. As queen3 said, you should create Repository per aggregate root. Like, if Products can have a Category, Category Repository should be a nested class of Products. Follow the domain logic relationship than domain objects.
不应该为每个表创建存储库。正如queen3所说,您应该为每个聚合根创建存储库。类似地,如果产品可以有一个类别,那么类别存储库应该是一个嵌套的产品类。遵循域逻辑关系而不是域对象。
#7
1
Queen3 is right, you can follow that Aggregate Root theory. I basically group my repository not thinking in Entities but how they group logically in the application I'm building.
Queen3是对的,你可以遵循聚合根理论。我基本上是将我的存储库分组,而不是考虑实体,而是考虑它们如何在我正在构建的应用程序中进行逻辑分组。
For example:
例如:
CustomersRepository
OrdersRepository
...
In CustomerRepository I would put methods for GetCustomers, GetCustomer, AddCustomer, DeleteCustomer, AddCustomerContact, DeleteCustomerContact.
在CustomerRepository中,我将为GetCustomers、GetCustomer、AddCustomer、DeleteCustomer、AddCustomerContact、DeleteCustomerContact设置方法。
In OrdersRepository I would put methods for GetOrders, GetOrder, AddOrder, CancelOrder, CloneOrder, AddOrderDetail, DeleteOrderDetail and so on.
在OrdersRepository中,我将放置GetOrders、GetOrder、AddOrder、CancelOrder、CloneOrder、AddOrderDetail、DeleteOrderDetail等方法。
#8
0
I tend to use a repository per related group of entitites. i.e orderrepository might have:
我倾向于对每一组相关的实体使用一个存储库。我。e orderrepository可能有:
Order, and OrderDetail.
秩序,OrderDetail。
and would have another for, say, Customer, CustomerProfile, etc.
顾客会有另一个,比如,顾客,个人资料等等。
This keeps the repository classes neat.
这使存储库类保持整洁。
Davy
戴维