I can't find a way to inject parameters in this scenario:
class Top
{
private ISome some;
public Top()
{
some = CreateSome(localRuntimeVariable);
}
//I need to pass "some" instance as a InjectionParameter to Child constructor
[Dependency]
public Child Child {get;set;}
}
class Child
{
//I need to inject ISome but it can only be constructed in Top
public Child(ISome some, Foo foo)
{
}
}
public class Usage
{
private v开发者_如何学Pythonoid Top GetTop(Foo foo)
{
return unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
//I expect: Top.Constuctor called and 'some' is assigned;
// Top.Child property beeing resolved: Child.Constructor called
// 'foo' instance to be taken from unity.Resolve<Top>(new DependencyOverride<Foo>(foo));
// 'some' instance to be taken from Top.some, but how to tell unity to inject it?
}
}
class Top
{
public Top( Foo foo, IUnityContainer container )
{
some = CreateSome(localRuntimeVariable);
Child = container.Resolve<Child>(new ParameterOverride("some" some),
new ParameterOverride("Foo", foo));
}
public Child Child {get;private set;}
}
class Child
{
public Child(ISome some, Foo foo)
{
}
}
Now you can resolve an instance of top using unity.Resolve<Top>(new ParameterOverride("Foo", foo))
The class Usage
isn't needed. GetTop(Foo foo)
is just syntactic sugar for unity.Resolve<Top>(new DependencyOverride<Foo>(foo))
精彩评论