Search code examples
c#simple-injector

simpleinjector 3.0 not supporting RegisterManyForOpenGeneric


So I decided to upgrade my version of simpleinjector to 3.0, and all of a sudden I get a message:

'SimpleInjector.Extensions.OpenGenericBatchRegistrationExtensions.RegisterManyForOpenGeneric(SimpleInjector.Container, System.Type, params System.Reflection.Assembly[])' is obsolete: 'This extension method has been removed. Please use Container.Register(Type, IEnumerable) instead.

The documentation still has this method in there:

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

So I'm curious, what's the alternative to:

container.RegisterManyForOpenGeneric(typeof(IEventHandler<>),
                                     container.RegisterAll,
                                     typeof(IEventHandler<>).Assembly);

Solution

  • Ahh.. after scratching my head for several hours, I figured it out:

    container.RegisterCollection(typeof(IEventHandler<>),
                                 typeof(IEventHandler<>).Assembly);
    

    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 });
    

    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.