3

I am creating new instnace provider which resolve services through Unity. I am not sure how to add the configuration in web.config. Following is my service class.

public class Service : IService {

private IUnitOfWork _unitOfWork; 

private IMyRepository _myRepository; 

// Dependency Injection enabled constructors 

public Service(IUnitOfWork uow, IMyRepository myRepository) 
{ 
    _unitOfWork = uow; 
    _myRepository = myRepository; 
} 

public bool DoWork()
{
        return true;
}

}

SVI
  • 1,631
  • 3
  • 22
  • 30

1 Answers1

3

You should only use web.config if you need to be able to vary the services after compilation. That should not be regarded as the default scenario.

This means that in most cases it would be better to resort to Code as Configuration, like this:

container.RegisterType<Service>();
container.RegisterType<IUnitOfWork, MyUnitOfWork>();
container.RegisterType<IMyRepository, MyRepository>();

If you must use XML configuration you can do something similar. Unity's excellent documentation explains the details.

It would probably go something like this:

<container>
  <register type="Service" />
  <register type="IUnitOfWork" mapTo="MyUnitOfWork" />
  <register type="IMyRepository" mapTo="MyRepository" />
</container>
Community
  • 1
  • 1
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736