2017-11-03 4 views
0

C# 및 생성자 종속성 주입에서 처음 두 생성자의 차이점은 무엇입니까? 특히 첫 번째 생성자의 :this은 무엇을 의미합니까? 두 번째 생성자 또는 다른 것에 대한 축약입니까?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; 
    } 
+0

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors –

+0

같은 시나리오에서 사용된다 https://stackoverflow.com/questions/1814953/c-sharp-constructor-chaining-how-to-do-it – Nkosi

+0

@ L.Guthardt 첫 번째 호출은 세 번째 호출입니다. – Nkosi

답변

1

종속성 주입 컨테이너가 저장소와 서비스를 만드는 방법을 처리하기 때문에 첫 번째 2 생성자를 생성하면 안됩니다.

this 키워드는

Public Person(string name){} 

public Person(string name, string lastname) :this(name) 
{ 
    // calls first constructor and then.. 
    // do something with lastname 
}