0
C#とコンストラクタ依存性注入では、最初の2つのコンストラクタの違いは何ですか?特に、最初のコンストラクタの:this
は何を表していますか? 2番目のコンストラクタや他の何かの省略形ですか?c# - オブジェクト指向プログラミングファクトリメソッドコンストラクタ
private readonly IRepositoryOne _repositoryOne;
private readonly IRepositoryTwo _repositoryTwo;
private readonly IService _service;
private readonly ApplicationDbContext _context;
public MyContructor()
: this(new RepositoryOne(new ApplicationDbContext()),
new RepositoryTwo(new ApplicationDbContext())
new Service())
{
}
public MyContructor()
{
_context = new ApplicationDbContext();
_repositoryOne = new RepositoryOne(_context);
_repositoryTwo = new RepositoryTwo(_context);
_service = new Service();
}
public MyContructor(IRepositoryOne repositoryOne,
IRepositoryTwo repositoryTwo,
IService service)
{
_repositoryOne = repositoryOne;
_repositoryTwo = repositoryTwo;
_service = service;
}
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors –
のようなシナリオで使用されていますhttps://stackoverflow.com/questions/1814953/c-sharp-constructor-chaining-how-to-do-it – Nkosi
@ L.Guthardt最初の人は3番目の人を呼び出します。 – Nkosi