Reputation: 41
I have one interface: IFoo Two classes implementing that interface: FooOne and FooTwo
And one classes ClassOne receiving an IFoo parameter in the constructor.
I have two methods MethodOne and methodTwo in Classone.
If i call MethodOne I need the object FooOne in Classone
If i call MethodTwo I need the object FooTwo in Classone
How I configure unity so ClassOne receives a FooOne instance for MethodOne call and ClassOne receives a FooTwo for MethodTwo call using only one container?.
The main condition is I need to create one object at a time either FooOne or FooTwo.
Upvotes: 1
Views: 364
Reputation: 24903
Using named registration:
container.RegisterType<IFoo, FooOne>("one");
container.RegisterType<IFoo, FooTwo>("two");
//...
class Classone
{
IFoo MethodOne()
{
return _container.Resolve<IFoo>("one");
}
IFoo MethodTwo()
{
return _container.Resolve<IFoo>("two");
}
}
Upvotes: 1