문제

I am new to Unity (from Castle Windsor land) so I know what I want to do with dependancy injection I am just unsure of how to do it with Unity.

Guidance would be appreciated.

And further some implementations like this:

public class Strategy
{
... etc
}

public class FooStrategy : Strategy
{
... etc    
}

public class BarStrategy : Strategy
{
... etc    
}

Currently I can register these one by one like this:

container.RegisterType<IStrategy, FooStrategy>("FooStrategy");
container.RegisterType<IStrategy, BarStrategy>("BarStrategy");

var foo = container.Resolve<IStrategy>("FooStrategy");
Assert.IsTrue(foo.GetType() == typeof (FooStrategy));

How can I do this more efficiently? I want to register all types implementing IStrategy by their implementation name.

도움이 되었습니까?

해결책

        container.RegisterTypes(AllClasses.FromLoadedAssemblies().Where(t => typeof(Strategy).IsAssignableFrom(t)));

It seems that by default the Name used in the container is the implementation name, so it is quite trivial in the end.

The only thing I had to change was to use a POCO as a base type, instead of an interface.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top