ASP.NET MVC使用Repository模式 [英] ASP.NET MVC using the Repository Pattern

查看:600
本文介绍了ASP.NET MVC使用Repository模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前即时通讯使用的EF和直接在我所有的行动,利用其DataContext的,但自从我开始阅读关于松耦合,而且那不是最好的方式去测试性即时通讯思想。我试着去了解所有亲的和反对的我才开始重构所有我目前的code。

Currently im using EF and using its datacontext directly in all of my actions, but since i started reading about loose coupling and testability im thinking that thats not the best way to go. Im trying to understand all the pro's and con's before i start refactor all my current code.

问题1:
考虑到每一个实体需要它自己的仓库,因而具有设置它自己的连接到数据源(让我们假设使用EF数据库),难道不,让很多开销,如果我需要在一个页面上,从5个不同的实体数据?

Problem 1: Considering that every entity needs its own repository, and thus has to setup its own connection to a datasource (lets assume a database using EF), wouldnt that give alot of overhead if i need data from 5 different entities on a single page?

问题2:
什么即时通讯在其中我在网上找到的是,大多数人(甚至人们喜欢shanselman)使用的是由两种或LINQ产生EF实体类实现了存储库模式的所有例子藏汉看到,不这场失利与存储库模式的目的关于松耦合?另一方面,什么是替代方案中,组合使用POCO类与例如AutoMapper? (这让我害怕一点点)

Problem 2: What im seeing aswell in all the examples which i found online is that most people (even people like shanselman) implement the repository pattern using the entity classes which are generated by either LINQ or EF, doesn't this defeat the purpose of repository pattern with regards to loose coupling? On the other hand, what is the alternative, using POCO classes in combination with for example AutoMapper? (this scares me a little)

林希望,有几个人能摆脱这方面的一些光,由于IM有点此刻混淆,如果存储库模式是一个网站的正确选择。

Im hoping that a few people can shed some light on this, because im a bit confused at the moment if the repository pattern is the right choice for a website.

推荐答案

您可以阅读这本书。还有就是使用存储库模式和LINQ的一个很好的例子。结果
也有这篇文章<一个href=\"http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx?wa=wsignin1.0\"相对=nofollow>使用工作模式库和单位与实体框架4.0 。

You can read this book. There is a good example of the using Repository pattern and LINQ.
Also there is this article Using Repository and Unit of Work patterns with Entity Framework 4.0.

这篇关于ASP.NET MVC使用Repository模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆