0

I'm interested how to implement my constructor in services. I'm a bit new to .NET so don't get me wrong if question is too trivial.

This are my current constructors but I would like to fully understand (since it looks like its working, I took code from somewhere)

   // initialize UnitOfWork
    private IUnitOfWork _unitOfWork;

    public TownService()
        : this(new UnitOfWork())
    {
    }

    public TownService(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

What does : this means and which one is called first? Also I saw there is constructor with : base but I think thats the one thats calling superclass first.

Do I need to call new UnitOfWork() ? Isn't UnitOfWork supposed to be factory (shared) instance? Or it is factory and new UnitOfWork is not creating new but taking initialized one from memory?

Thanks.

Želja Huber
  • 337
  • 5
  • 14
  • 1
    : this(new UnitOfWork()) means that it calls the constructor with the IUnitOfWork interface parameter. Which one is called first, is determind by the context. Maybe take a read into Dependency Injection first for this matter. – Edwin van Vliet Jun 11 '14 at 08:15
  • Thanks for pointing that out, just found out that I have some DI book :D – Želja Huber Jun 11 '14 at 08:18

1 Answers1

2

Constructors in C#

Given your code

public TownService()
    : this(new UnitOfWork())
{
  //1
}

public TownService(IUnitOfWork unitOfWork)
{
    //2
    _unitOfWork = unitOfWork;
}

Invoking new TownService() will

  • Call the parameterless-constructor
  • Instantiate a new UnitOfWork and call the overload TownService(IUnitOfWork unitOfWork), so //2 is executed. This happens because of the this(...) call.
  • Then execute the parameterless constructor's body, i.e. //1

You need to get an instance of IUnitOfWork from somewhere, but calling the parameterless constructor which in turn will instantiate a new UnitOfWork probably isn't what you want - it doesn't really buy you much flexibility.

Using a factory

If you want to use a factory, you would go something like

ITownService s = new TownService(myFactory.Get<IUnitOfWork>());

thus avoiding the parameterless constructor.

Using IoC

If you want to use an IoC container, you would probably go something like this to configure your container

myContainer.Register<IUnitOfWork, UnitOfWork>(); //May need to provide database transaction or whatever
myContainer.Register<ITownService, TownService>();

You would also need to tell ASP.NET MVC to use the container when creating controllers. You do this by creating a ControllerFactory that is aware of your IoC container. Most IoC containers come with such a factory and instructions on how to use it.

Once that is done, you would be able to declare your controllers like

public class MyController
{
    public MyController(ITownService townService) 
    {
      /*...*/
    }
}

and have ASP.NET MVC and your IoC container do the rest.

Rune
  • 8,340
  • 3
  • 34
  • 47
  • Actually I have both first and last blocks of code you just wrote... And using ninject I have this: private static void RegisterServices(IKernel kernel) { kernel.Bind().To(); kernel.Bind().To(); kernel.Bind().To(); kernel.Bind().To(); kernel.Bind().To(); } – Želja Huber Jun 11 '14 at 08:46
  • Is that enough then or I should add/remove some code? – Želja Huber Jun 11 '14 at 08:50
  • Well, does it work? You probably have to tell ASP.NET MVC about Ninject if you haven't done so already. Just google ninject and mvc. – Rune Jun 11 '14 at 08:58
  • Looks like it does although I think (as you said) that public TownService() : this(new UnitOfWork()) { //1 } is code I dont need... Was just interested in those constructors to fully understand them. Will //2 ever be called without that? – Želja Huber Jun 11 '14 at 09:01