如何在mvc中实现依赖注入? [英] how to implement dependency injection in mvc?

查看:167
本文介绍了如何在mvc中实现依赖注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现依赖项注入,但是我知道如何实现类的接口和存储库,所以我不知道该怎么办。
这是我的示例:

I'm trying to implement dependency injection but i know how to implement the interface and repository of classes then i don't know what shall i do. This my sample:

 public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}

这是我的界面:

public interface IUser
{
   IEnumerable<User> GetUsers();
   void AddUser(User user);
   void EditUser(User user);
   void DeleteUser(int id);
   User UserGetById(int id);
   void Save();
}

这是我的存储库:

  public class UserRepsitory:IUser
{
    private _Context _context;
    public UserRepsitory(_Context _context)
    {
        this._context = _context;
    }
    public IEnumerable<User> GetUsers()
    {
       return  _context.User.ToList();
    }
    public void AddUser(User user)
    {
        _context.User.Add(user);
    }
    public void EditUser(User user)
    {

        _context.Entry(user).State = System.Data.Entity.EntityState.Modified;
    }
    public User UserGetById(int id)
    {
       return _context.User.Find(id);
    }
    public void Save()
    {
        _context.SaveChanges();
    }
    public void DeleteUser(int id)
    {
        var Search = _context.User.Find(id);
        _context.User.Remove(Search);

    }
}

而控制器中的方法之一:

And one of method in controller:

  private IUser userRepsitory;

    public UsersController()
    {
        this.userRepsitory = new UserRepsitory(new _Context());
    }

    public UsersController(IUser UserRepository)
    {
        this.userRepsitory = UserRepository;
    } 

    public ActionResult Index()
    {
        return View(userRepsitory.GetUsers());
    }

下一步是什么?

推荐答案

第一件事是,摆脱默认构造函数,在这里我们很难对 UserRepository 的初始化进行编码!

The first thing is, get rid of the default constructor where we are hard coding the initialization of UserRepository ! We will do that in the dependency injection way.

public UsersController : Controller
{ 
    private readonly IUser userRepsitory;
    public UsersController(IUser UserRepository)
    {
        this.userRepsitory = UserRepository;
    } 

    public ActionResult Index()
    {
        return View(userRepsitory.GetUsers());
    }
}

现在我们需要一些信息来告诉MVC框架哪个版本代码运行时,应使用/ implementation IUser 。您可以使用任何依赖项注入框架来做到这一点。例如,如果您使用的是MVC 6,则可以使用内置的依赖项注入框架来实现。因此,转到Startup类,并在您的 ConfigureServices 方法中,您可以将接口映射到具体的实现。

Now we need something to tell the MVC framework which version/implementation of IUser should be used when the code runs. you can use any dependency injection frameworks to do that. For example, If you are in MVC 6, you can use the inbuilt dependency injection framework to do that. So go to your Startup class and in your ConfigureServices method, you can map an interface to a concrete implementation.

public class Startup
{
  public void ConfigureServices(IServiceCollection services)
  {
     services.AddTransient<IUser, UserRepository>();
  }
}

如果您使用的是早期版本的MVC,则您可以考虑使用任何可用的依赖项注入框架,例如Unity,Ninject等。

If you are in a previous version of MVC, you may consider using any of the dependency injection frameworks available like Unity, Ninject etc.

几乎相同,您将接口映射到具体实现

It is pretty much same, you map an interface to a concrete implementation

Ninject

private static void RegisterServices(IKernel kernel)
{
  kernel.Bind<IUser>().To<UserRepository>();
}

您无需将映射放在cs文件中。您可以在配置文件中进行定义。例如,当您使用Unity时,您可以在配置文件中(网络配置或用于统一配置的外部配置文件)执行类似的操作

You do not need to put the mapping in a cs file. You can define that in a config file. For example, when you use Unity you can do something like this in your config file (web config or an external config file for unity configuration)

Unity

<alias alias="IUser" type="YourNamespace.IUser, YourAssemblyName" />
<register type="IUser" mapTo="YourNamespace.UseRepository, YourAssemblyName">

这篇关于如何在mvc中实现依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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