views:

49

answers:

2

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;
}

}

+1  A: 

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>
Mark Seemann
No need to register the Service type.
Chris Tavares
Right; it's necessary with some other containers, but not Unity :)
Mark Seemann
A: 

Mark, where do i set these in my WCF service?

container.RegisterType();

In my MVC project, I had set these in my global.asax file.

SVI