Unity Framework IoC with default constructor

限于喜欢 提交于 2019-12-04 00:38:16

问题


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?


回答1:


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>



回答2:


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>()


来源:https://stackoverflow.com/questions/3714357/unity-framework-ioc-with-default-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!