开发者

Unity: Constructor injection with mutiple constructors

开发者 https://www.devze.com 2023-03-19 19:03 出处:网络
I\'m trying to inject an EF ObjectContext using Unity constructor injection. My attemp is to register the type in the bootsprapper like this:

I'm trying to inject an EF ObjectContext using Unity constructor injection. My attemp is to register the type in the bootsprapper like this:

protected override void ConfigureContainer()
{
    base.ConfigureContainer();
    Container.RegisterType<ObjectContext, MyObjectContext>(new InjectionConstructor());
}

EF creates mutiple constructors which look like these

public MyObjectContext() : base("name=MyObjectContext", "MyObjectContext")
public MyObjectContext(string connectionString) : base(connectionString, "MyObjectContext")
public MyObjectContext(EntityConnection connect开发者_开发知识库ion) : base(connection, "MyObjectContext")

When debugging my code Unity throws a ResolutionFailedException telling me "The type MyObjectContext has multiple constructors of length 1. Unable to disambiguate." at the time when a new class that has the following constructor is resolved.

public MainViewModel(UnityContainer container, MyObjectContext entities)

As far as I know using RegisterType with new InjectionConstructor() as argument ensures that the default parameterless constructor is called (thats what I want). Why can't Unity resolve the type as expected? Do I miss anything?

Best Regards

Jay


Your registration looks right. You sure the registration code is getting called? Try putting a breakpoint on it.

(Off topic: why are you passing your container to your viewmodel?)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号