C# 存储库和工厂模式之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1104275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference between a repository and factory pattern
提问by Blankman
Can you please outline the differences between the Repository pattern and the Factory pattern?
您能否概述存储库模式和工厂模式之间的区别?
回答by Jeff Sternal
The Factory patternis for creating objects, while the Repository patterndescribes a general method of encapsulating CRUD operations against a data source.
回答by WorthiGe
The repository pattern deals with creating a set of data access services, ie. CRUD methods. The factory pattern on the other hand deals with how an object is created. A factory class will have a method that returns a new instance of a class.
存储库模式处理创建一组数据访问服务,即。CRUD 方法。另一方面,工厂模式处理如何创建对象。工厂类将具有返回类的新实例的方法。
The two are independent of each other; however, you will often see the factory pattern used alongside with the repository pattern in order to create a new instance of a entity.
两者相互独立;但是,您经常会看到工厂模式与存储库模式一起使用以创建实体的新实例。
回答by Herman Van Der Blom
I think the difference is the usage of both.
我认为区别在于两者的用法。
If you want to create objects and fill those objects with database data you will use a Repository. You can have a CustomerRepository
with all related methods in it for retrieving and manipulation of Customers
.
如果您想创建对象并用数据库数据填充这些对象,您将使用存储库。您可以使用CustomerRepository
包含所有相关方法的a来检索和操作Customers
.
If you want to create Objects and fill those objects with data you use a Factory. An example could be a ColorFactory
. You can fill that one with a lot of possible RGB colors that you will use later on.
如果要创建对象并用数据填充这些对象,请使用工厂。一个例子可能是一个ColorFactory
. 您可以使用多种可能的 RGB 颜色填充该颜色,稍后将使用这些颜色。
If you look at something like "saving" data you could also use different words like: store, save and persist. All three are used for a different purpose while they may do the same :-)
如果您查看诸如“保存”数据之类的内容,您还可以使用不同的词,例如:存储、保存和持久化。这三个都用于不同的目的,但它们可能会做相同的事情:-)