简体   繁体   English

对仓库模式的理解混乱

[英]confusion in understanding of repository pattern

Im new in MVC pattern but im involved in a project which i am asked to implement repository pattern and unit of work,tons of examples online with 100 different implementations thats also a pain,because there is no clear way,any way,here is what i am doing and i would like you to give me a reason why should i use this damn pattern: 我是MVC模式的新手,但我参与了一个项目,要求我实施存储库模式和工作单元,在线提供100种不同实现的大量示例,这也很痛苦,因为没有明确的方法,任何方法,这是什么我正在做,我想请您给我一个理由,为什么我要使用这种该死的模式:

i have many controllers i instantiate the the model,and use it in my controller: 我有很多控制器,我实例化了模型,并在控制器中使用它:

public CentralEntities DB = new CentralEntities(); 

i use it in my controller for example like this: 我在控制器中使用它,例如:

var turbineid = (from s in DB.MasterDatas
                 where s.name == turbinename
                 select new TrBineId
                 {
                     turbineID = s.m_turbine_id
                 }).Single();

TrBineId is my viewModel,any way the number of controllers are increasing and also in each controller i have many different LINQ,should i start with generic repository? TrBineId是我的viewModel,无论如何控制器的数量都在增加,而且在每个控制器中我都有许多不同的LINQ,我应该从通用存储库开始吗?

The damn reason to use repository pattern lies in implementing your solution with a clear separation of concerns and leverage domain I/O in a way that can be reused across your codebase. 使用存储库模式该死原因在于实现解决方案时应将关注点明确分开,并以可在整个代码库中重用的方式利用域I / O。

You should start to revisit OOP and you'll need to double-check repository pattern . 您应该开始重新访问OOP,并且需要仔细检查存储库模式

At the end of the day, the need for certain patterns is already there, but it'll arise in your mind once you put the pieces in order. 归根结底,对某些模式的需求已经存在,但是一旦您整理好顺序,它就会在您的脑海中浮现。

I would start following some tutorial around the net about repository pattern to implement a proof-of-concept project and realize how it works. 我将开始在网上跟随一些有关存储库模式的教程,以实施概念验证项目并实现其工作方式。

Finally, Inversion of Control and Dependency Injection are concepts you'll need to manage to integrate your repositories and other patterns in your project to increase code composability and testability. 最后, 控制反转依赖注入是您需要管理的概念,以在您的项目中集成存储库和其他模式以提高代码的可组合性和可测试性。

DISCLAMER : The following links are from my website about software architecture. 免责声明 :以下链接来自我的网站,有关软件体系结构。 You might want to check them as a possible reference implementation of repository pattern : 您可能需要检查它们作为存储库模式的可能参考实现:

The repository pattern allows you to use the concept of Single responsibility principle, which mean (as an overview) one class = one role. 存储库模式允许您使用单一职责原则的概念,这意味着(作为概述)一个类=一个角色。

Your controler class is here for managing the request (Get, Post) and send back a response (FileResult, ActionResult...). 您的控制程序类用于管理请求(获取,发布)并发送回响应(FileResult,ActionResult ...)。

For the Data access (DAL, DAO) you will usually create a class per model entity (ClientRepository, CommandRepository), and you will create your methods for getting them (GetClients(), GetOneClientById(int id)...) within this class. 对于数据访问(DAL,DAO),通常将为每个模型实体 (ClientRepository,CommandRepository)创建一个类,并在该类中创建用于获取它们的方法(GetClients(),GetOneClientById(int id)...)。 。

Edit for clarification after Matías comment: Matías评论后编辑以澄清问题:

This class will be called in your controller through his interface that you will also implement (with IOC or not). 此类将通过您还将通过其实现的接口在您的控制器中调用(无论是否使用IOC)。 You will then create a class instance of ClientRepository in your controller, but assigned to a reference of the interface type (IClientRepository). 然后,您将在控制器中创建ClientRepository的类实例,但将其分配给接口类型(IClientRepository)的引用。

**End Edit ** **结束编辑**

So we can imagin for Client Entity: 因此,我们可以想象客户实体:

ClientController(All route for clients data)
IClientRepository (interface)
ClientRepository (Class which implement IClientRepository )

Then, in your controller you will call the repository like 然后,在您的控制器中,您将像

IClientRepository clientRepo = new ClientRepository();

And then use the methods: 然后使用以下方法:

ICollection<Client> clients = clientRepo.YourMethod(int param1);

Advantages: 好处:

First of all, your code will be more clear and maintainable. 首先,您的代码将更加清晰和可维护。 Your DAO (DAL in .net) will keep the data access layer and you could use the GetAllClients method many time and you will not repeat yourself (DRY). 您的DAO(.net中的DAL)将保留数据访问层,并且您可以多次使用GetAllClients方法,并且不会重复自己(DRY)。

You can also apply easily some param on the method (like order, limit for pagination etc...) 您还可以轻松地在方法上应用一些参数(例如顺序,分页限制等)。

It will be testable as well, I don't think that calling database in the controller can give you reasonable unit test results. 它也将是可测试的,我认为在控制器中调用数据库不会给您合理的单元测试结果。 You will also catch excption with a more "elegant way". 您还将以更加“优雅的方式”捕捉到异常。

Small tip : When you will have more experience in the repo pattern, you could have a look at the Inversion of Control pattern 小提示:如果您将对回购模式有更多的经验,可以看看控制反转模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM