2016-10-08 19 views
1

我想了解DI如何在新的ASP Core中工作。通过教程,我使它适用于控制器,但无法使其适用于模型。例如,我有一个AuthController,并且我向它注入了数据库上下文,但是现在,由于我有更多的控制器,共享相同的模型,即Authentication,我希望将上下文注入模型本身。下面是一些代码片段我:模型中的ASP.NET CORE DI

Startup.cs

public void ConfigureServices(IServiceCollection services) 
{ 
    ... 
    services.AddDbContext<GameContext>(options => options.UseSqlServer(@"Data Source=DESKTOP-USER\SQLEXPRESS;Initial Catalog=Db7;Integrated Security=True;Connect Timeout=30;")); 
} 

,这里是我如何使用它的控制器:

[Route("api/[controller]")] 
public class AuthController : Controller 
{ 
    public GameContext db; 
    public AuthController(GameContext context) 
    { 
     db = context; 
    } 

    [HttpPost] 
    [Route("login")] 
    public LoginResponseModel Login([FromBody] LoginModel user) //public Models.VM.LoginModel Login([FromBody] Models.VM.LoginModel user) 
    { 
     //query user 
     var detectedUser = db.Users.FirstOrDefault(u => u.Email == user.Email && u.Password == HelperClass.Md5(user.Password); 

如果我删除的情况下部分从控制器,和将它移动到模型中,由于构造函数需要参数(我们将自动注入该参数?),我将无法再将其重新安装,

public class Authentication 
{ 
    public GameContext db; 

    public Authentication(GameContext context) 
    { 
     db = context; 
    } 
    ... 

如何从模型到达数据库?

编辑:

这是我的验证类会是什么样子(构造函数可基于该解决方案会有所不同):

public class Authentication 
{ 
    public GameContext db; 

    public Authentication(GameContext context) 
    { 
     db = context; 
    } 

    public LoginResponseModel Login(LoginModel user) 
    { 
     //query user 
     var detectedUser = db.Users.FirstOrDefault(u => u.Email == user.Email && u.Password == HelperClass.Md5(user.Password)); 

这里就是我想用此模型控制器:

[Route("api/[controller]")] 
public class AuthController : Controller 
{ 

    public AuthController(GameContext context) 
    { 
    } 

    // POST api/login 
    [HttpPost] 
    [Route("login")] 
    public LoginResponseModel Login([FromBody] LoginModel user) //public Models.VM.LoginModel Login([FromBody] Models.VM.LoginModel user) 
    { 
     Authentication auth = new Authentication(); //throws error since no parameter passed 

     return auth.Login(user); 
    } 
+0

你打算如何实例化模型?试图了解你打算如何使用模型。你的解释不清楚。你是否用模型替换了控制器构造函数中的上下文? – Nkosi

+0

添加了更多的信息 –

+1

好吧,我想我现在明白了。起草答案 – Nkosi

回答

2

您基本上为其他控制器提供可重用服务。

首先创建所需功能的抽象。

public interface IAuthenticationService { 
    LoginResponseModel Login(LoginModel user); 
} 

,并有实现这个接口

public class Authentication : IAuthenticationService { 
    private readonly GameContext db; 

    public Authentication(GameContext context) { 
     db = context; 
    } 

    public LoginResponseModel Login(LoginModel user) { 
     //query user 
     var detectedUser = db.Users.FirstOrDefault(u => u.Email == user.Email && u.Password == HelperClass.Md5(user.Password)); 
     //...other code that creates and returns an instance of LoginResponseModel 
    } 
} 

与在地方,你需要注册与服务集合的接口继承,使得DI框架是知道什么注入,只要自己认为该界面。

来源:Asp.Net Core Fundamentals: Dependency Injection

public void ConfigureServices(IServiceCollection services) { 
    ... 
    services.AddDbContext<GameContext>(options => options.UseSqlServer(@"Data Source=DESKTOP-USER\SQLEXPRESS;Initial Catalog=Db7;Integrated Security=True;Connect Timeout=30;")); 
    // Add application services. 
    services.AddTransient<IAuthenticationService, Authentication>(); 
} 

,现在任何控制器需要访问该服务可以把它注入到它的构造

[Route("api/[controller]")] 
public class AuthController : Controller { 
    private readonly IAuthenticationService auth; 

    public AuthController(IAuthenticationService auth) { 
     this.auth = auth; 
    } 

    // POST api/login 
    [HttpPost] 
    [Route("login")] 
    public LoginResponseModel Login([FromBody] LoginModel user) { 
     return auth.Login(user); 
    } 
} 

的DI框架将负责创建的所有繁重的任务和注入服务。您现在不必自己创建实例。

+0

感谢您的解决方案,工作得很好! +1为清楚和很好的解释。一个问题,可能是愚蠢的,但看不到原因:为什么需要将IAuthenticationService注册为服务? –

+1

这样,DI框架就可以知道每当它看到该接口时要注入什么。在这里查看文档https://docs.asp.net/en/latest/fundamentals/dependency-injection.html – Nkosi