0

In C# and constructor dependency injection what is the difference between the first two constructors. Specifically what does the :this in the first constructor signify. Is it just shorthand for the second constructor or something else?

    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;
    }
adam78
  • 9,668
  • 24
  • 96
  • 207
  • https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors – Matt Hogan-Jones Nov 03 '17 at 14:54
  • https://stackoverflow.com/questions/1814953/c-sharp-constructor-chaining-how-to-do-it – Nkosi Nov 03 '17 at 14:56
  • @L.Guthardt the first one calls the 3rd one. – Nkosi Nov 03 '17 at 15:00
  • @Nkosi so inorder to use my dependency injection constructor should i be using constructor number two and get rid of constructor number one so that I'm using the same context? – adam78 Nov 03 '17 at 15:08
  • 1
    @adam78 This appears to be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the ultimate goal you are trying to achieve? 'Cause I would remove first two constructor and let the DI container inject the explicit dependencies into constructor 3 – Nkosi Nov 03 '17 at 15:10
  • @Nkosi I want to use dependency injection using constructor but I need to use the same application db context for the repositories. Note I dont have a DI container. – adam78 Nov 03 '17 at 15:12
  • @adam78 Then create the instances externally and Pure DI them into the dependent class. – Nkosi Nov 03 '17 at 15:17
  • @Nkosi I'm trying to ask how I can do it with the above example and no DI container. Which constructor should I be using the first or second? – adam78 Nov 03 '17 at 15:19
  • @adam78 use second constructor based on the desired functionality – Nkosi Nov 03 '17 at 15:19
  • @Nkosi. Thankyou. – adam78 Nov 03 '17 at 15:20
  • 1
    Having multiple constructors is [an anti-pattern](https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99). – Steven Nov 03 '17 at 15:41
  • @Steven my programming is not up to your high standard but thanks for the tip. However in all honesty in my many years of programming no one ever bothers to look under the hood regardless of whatever pattern you use as long as the applications chugs along. – adam78 Nov 05 '17 at 19:33

1 Answers1

1

You should not create the first 2 constructors because the dependency injection container will handle how to create the repository and the service.

The this keyword is used in scenarios like

Public Person(string name){}

public Person(string name, string lastname) :this(name)
{ 
    // calls first constructor and then..
    // do something with lastname
} 
Patrick Allwood
  • 1,822
  • 17
  • 21
Igal23
  • 122
  • 3