使用DI在每个mvc请求上加载存储库实例 [英] using DI to load repository instance on each mvc request

查看:123
本文介绍了使用DI在每个mvc请求上加载存储库实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了这篇文章他们正在使用依赖注入来在每个mvc请求上加载存储库实例。



我不知道我是否正确理解,但我目前在我的mvc应用程序中使用。 UserRepository 实现 IUserRepository 界面。这个接口被注入到控制器构造函数中。

  public class UserController:Controller 
{
private IUserRepository repository;
public UserController(IUserRepository rep)
{repository = rep; }

public UserController():this(new UserRepository()){}
}

但我没有看到任何好处使用这个界面( IUserRepository )我可以使用 UserRepository 没有界面。显然,有人更聪明地认为是正确的方法(我发现它在apress mvc4书),我会请一个人详细说明为什么这是更好的方法,而不是使用存储库没有界面。


$ b考虑到这一点,我会请任何人分享具体的示例或链接,了解如何实现这种方法(使用依赖注入来加载每个mvc请求上的仓库实例)。

解决方案

DI背后的主要思想是强制您看到大图,而不是具体的实现。



您的控制器需要获取用户,但不应该关心具体实现(您的存储库是否从数据库,Web服务,xml文件等获取用户)或者它使用Linq2Sql,EntityFramework,Dapper或其他东西在引擎盖下)。



您的控制器依赖于可以以构造函数,属性或方法注入的代码,但它并不关心具体的实现。



DI消除了控制器和存储库之间的紧密耦合,允许您通过嘲弄存储库来编写单元测试,您可以轻松地更改存储库的具体实现(例如使用PetaPoco而不是EntityFramework),而不用接触其余的代码。



你还应该看看SOLID的原理: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)


I read on this post that they are using dependency injection to load repository instance on each mvc request.

I'm not sure if I understand correctly but I currently using in my mvc app. UserRepository which implements IUserRepository interface. This interface is injected in controller constructor

public class UserController : Controller
{
   private IUserRepository repository;
   public UserController(IUserRepository rep)
   { repository = rep; }

   public UserController() : this(new UserRepository()) {}
}

but I don't see any benefit using this interface (IUserRepository) I could use UserRepository without interface. Obviously someone smarter is figured that is right approach (I've found it on apress mvc4 book) and I would kindly ask someone to elaborate why is this better approach instead of using repository without interface.

Having this in mind I would ask anyone to share concrete examples or links on how to implement this approach (using dependency injection to load repository instance on each mvc request).

解决方案

The main idea behind DI is to force you to see the big picture instead of concrete implementations.

Your controller needs to get the user, but it shouldn't care about concrete implementation (does your repository fetch the user from the database, web service, xml file, etc. or does it use Linq2Sql, EntityFramework, Dapper or something else under the hood).

Your controller is dependent on that piece of code which can be injected in constructor, property or method, but it doesn't really care about concrete implementation.

DI removes the tight coupling between your controller and repository, allows you to write unit tests by mocking the repository, and you can easily change the concrete implementation of your repository (eg. use PetaPoco instead of EntityFramework) without touching the rest of the code.

You should also take a look at the SOLID principles: http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

这篇关于使用DI在每个mvc请求上加载存储库实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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