simpleinjector 3.0不支持RegisterManyForOpenGeneric

sks*_*laj 5 c# simple-injector

所以我决定将我的simpleinjector版本升级到3.0,突然间我收到一条消息:

'SimpleInjector.Extensions.OpenGenericBatchRegistrationExtensions.RegisterManyForOpenGeneric(SimpleInjector.Container,System.Type,params System.Reflection.Assembly [])'已过时:'此扩展方法已被删除.请改用Container.Register(Type,IEnumerable).

文档中仍然有这个方法:

http://simpleinjector.readthedocs.org/en/latest/advanced.html

所以我很好奇,有什么替代方案:

container.RegisterManyForOpenGeneric(typeof(IEventHandler<>),
                                     container.RegisterAll,
                                     typeof(IEventHandler<>).Assembly);
Run Code Online (Sandbox Code Playgroud)

sks*_*laj 7

啊......抓了几个小时后, 我想到了:

container.RegisterCollection(typeof(IEventHandler<>),
                             typeof(IEventHandler<>).Assembly);
Run Code Online (Sandbox Code Playgroud)

RegisterCollection handles open generics as well. Maybe this should be documented somewhere.

EDIT:

I realized in the new documentation, the above code is not a direct translation from RegisterManyForOpenGeneric. All it did was solve my compilation, but it didn't register my handlers, I just checked it today.

Additional information: No registration for type

This is the correct version:

container.Register(typeof(IEventHandler<>),
                   new[] { typeof(IEventHandler<>).Assembly });
Run Code Online (Sandbox Code Playgroud)

Using a RegisterCollection would require some extra code changes (from the document):

Because we register a collection, we can no longer call container.GetInstance>(). Instead instances can be retrieved by having an IEnumerable> constructor argument or by calling container.GetAllInstances>().

Which I haven't done, and don't really need to do since I don't have mixed open-generic and non-generics. But I'll explore this more in the future if I wanna revamp my project.