views:

117

answers:

2

I'm trying to inject a dependency to my MVC controllers like this

private static void RegisterContainer(IUnityContainer container)
{            
    container
        .RegisterType<IUserService, UserService>()
        .RegisterType<IFacebookService, FacebookService>();
}

The UserService class has a constructor like this...

public UserService(): this(new UserRepository(), new FacebookService())
{
    //this a parameterless constructor... why doesnt it get picked up by unity?
}

public UserService(IUserRepository repository, IFacebookService facebook_service)
{
    Repository=repository;
    this.FacebookService=facebook_service;
}

The exception I am getting is the following...

The current type, Repositories.IUserRepository, is an interface and cannot be constructed. Are you missing a type mapping?

It looks like it's trying to inject a constructor into the service, but the default would suffice? Why is it not mapping to the parameterless constructor?

A: 

I can't speak to Unity specifically, but IoC containers will generally try to use the most specific constructor they can find because it's a constructor.

If there's a constructor that takes two dependencies for injection, then presumably they are required for using the object; the default constructor will have to do something to fulfill them if the container calls it. The container's job is to fulfill dependencies, so why would it leave it up to the class to do that if it were not instructed to leave it up to the class?

To your specific question, according to your code:

private static void RegisterContainer(IUnityContainer container)
{            
    container
        .RegisterType<IUserService, UserService>()
        .RegisterType<IFacebookService, FacebookService>();
}

IUserRepository is not registered. Add a line like

.RegisterType<IUserRepository, UserRepository>()
arootbeer
it's not true that IoC will find the most specific constructor , if you don't specify the constructor parameters while registering a type , it will automatically call default constructor.
saurabh
I don't know which containers you've used, but I almost never specify constructors in my mappings. I know for sure that StructureMap will prefer a more fulfilling constructor to the default constructor - I'm using it in my current project, and that's how I unit test some of the objects that have to have a default constructor (WCF Services, for example). I'm pretty sure that Castle Windsor will also prefer a more specific constructor.
arootbeer
@saurabh In my experience with Unity, when no parameters are specified the most specific constructor is used.
Kirk Broadhurst
In Unity , new can specify Injection Parameters.Which will be injected while construction so if you have a class which is having a parameter in a ctor, you can specify while registering types.
saurabh
+1  A: 

The Unity default convention (which is pretty clearly spelled out in the documentation) is to choose the constructor with the most parameters. You can't just make a blanket statement that "it's not true that IoC will find the most specific constructor , if you don't specify the constructor parameters while registering a type , it will automatically call default constructor." Each container implementation can and does have different defaults.

In Unity's case, like I said, it will choose the constructor with the most parameters. If there are two that have the most parameters, then it'll be ambiguous and throw. If you want something different, you must configure the container to do that.

Your choices are:

Put the [InjectionConstructor] attribute on the constructor you want called (not recommended, but quick and easy).

Using the API:

container.RegisterType<UserService>(new InjectionConstructor());  

Using XML config:

<container>
  <register type="UserService">
    <constructor />
  </register>
</container>
Chris Tavares