2016-10-08 11 views
1

新しいASPコアでDIがどのように機能するかを理解しようとしています。チュートリアルでは、私はコントローラのためにそれを作ったが、モデルのためにそれを働かせることはできない。たとえば、私はAuthControllerを持っていますが、データベースコンテキストを注入しましたが、コントローラが増えて同じモデル(Authentication)を共有しているので、モデル自体にコンテキストを注入したいと思います。私は、コントローラからのコンテキスト部分を削除した場合ASP.NET CORE DIのモデル

[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); 

、および: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;")); 
} 

から

そして、ここでは、私は、コントローラからそれを使用する方法は次のとおりです。ここで私がしたコードの一部の作品ですそれをモデルに移すと、コンストラクタが引数を必要とするので自動的に挿入することはできません(自動的に挿入されるでしょう)

public class Authentication 
{ 
    public GameContext db; 

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

モデルからデータベースにアクセスするにはどうすればよいですか?

EDIT:

これは私の認証クラスは、(コンストラクターがソリューションに基づいて異なる場合があります)のようにどのように見えるかです:

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

あなたは基本的に他のコントローラ用の再利用可能なサービスを作ります。

まず、必要な機能の抽象化を作成します。 DIフレームワークは、それが見るたび注入するものを認識しているように、

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 
    } 
} 

から継承しますそのインタフェース。

出典: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>(); 
} 

、今サービスへのアクセスを必要とする任意のコントローラは、それはDIフレームワークは、作成のすべての重労働を処理するそのコンストラクタ

[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); 
    } 
} 

に注入することができ、およびサービスを注入する。インスタンスを自分で作成する必要はありません。

+0

ありがとう、非常にうまく動作します! +1は、明確で素敵な説明です。 1つの質問は、ダムかもしれないが、理由はまだ分かりません。なぜIAuthenticationServiceをサービスとして登録する必要がありますか? –

+1

DIフレームワークは、そのインターフェイスを見るたびに何を注入するのかを認識します。ドキュメントはhttps://docs.asp.net/en/latest/fundamentals/dependency-injection.htmlで確認してください。 – Nkosi

関連する問題