简体   繁体   English

带有存储库的Asp.net MVC的替代方案

[英]Alternative to Asp.net MVC with repositories

There seem to be many guides pushing the use of Entity Framework with the Repository and Unit of Work patterns when developing ASP.Net MVC projects. 在开发ASP.Net MVC项目时,似乎有很多指南推动将Entity Framework与存储库和工作单元模式一起使用。

A quick Google yields countless discussion on the Pro and Cons of this approach and while those who oppose the use of these patterns with EF are vocal I have yet to see anyone detail a good alternative. 快速的Google对这种方法的优点和缺点进行了无数讨论,尽管那些反对将这些模式与EF结合使用的人很直言不讳,但我还没有看到任何人详细介绍一个好的选择。

Short of directly accessing EF in Controller classes what's another approach to the Data Access layer? 除了在Controller类中直接访问EF之外,还有什么方法可以用于数据访问层?

I used repositories a lot in the past; 我过去经常使用存储库。 I thought they were the best thing since sliced bread. 我以为他们是切成薄片以来最好的东西。

However, over time and with gaining experience, I stopped using the repository pattern. 但是,随着时间的流逝,随着经验的积累,我停止使用存储库模式。 My repo classes inevitably ended up being clogged with numerous methods, many of which almost mimicked stored procs e..g 我的repo类不可避免地最终被许多方法所阻塞,其中许多方法几乎都模仿了存储过程,例如

customerRepo.GetCustomerWithFooBar();

sp_GetCustomerWithFooBar

I didn't deliberately look for an alternative per se, but I ended up using one of two ways. 我并不是故意寻找替代品,但最终还是使用了以下两种方法之一。

  1. Entity Framework Context I simply coded against the context object itself; 实体框架上下文我只是针对上下文对象本身进行了编码。 running LINQ queries as well as the usual SaveChanges() type of stuff. 运行LINQ查询以及通常的SaveChanges()类型的东西。

That looked messy, but it reduced the level of abstraction. 看起来很乱,但是降低了抽象水平。 But, as my project and code got bigger, this method wasn't that that maintainable. 但是,随着我的项目和代码变得越来越大,这种方法并不是那么可维护。 SO I tried... 所以我尝试了...

  1. CQS (Command Query Separation) The idea is to have classes the do commands and other classes that perform query's. CQS(命令查询分离)的想法是使类具有do命令和其他执行查询的类。 eg 例如

    public class SaveCustomerCommand(){} 公共类SaveCustomerCommand(){}

    public class ListCustomerQuery(){} 公共类ListCustomerQuery(){}

That way, I was able to nicely abstract my Entity Framework code into two categories - those classes that changed the data, and those which simply read it. 这样,我就可以将Entity Framework代码很好地抽象为两类:更改数据的类和简单读取数据的类。 Unfortunately, I never finished the code where I experimented with this approach, but you could consider writing a CommandDispatcher class that takes in commands/query's, new's up UnitOfWork object (however you implemented that) and control the command.Execute() and query.Execute(). 不幸的是,我从未尝试过使用该方法的代码,但是可以考虑编写一个CommandDispatcher类,该类接受命令/查询,新的up UnitOfWork对象(无论如何实现)并控制command.Execute()和查询。执行()。

There are articles out there which could help you get up and running with method 2. 那里有文章可以帮助您入门并使用方法2。

http://alertcoding.wordpress.com/2013/03/17/command-and-query-based-entity-framework-architecture/ http://alertcoding.wordpress.com/2013/03/17/command-and-query-based-entity-framework-architecture/

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

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