bbqchickenrobot
bbqchickenrobot

Reputation: 3709

Simple Injector Fallback Registration

I read in the documentation that it's possible to create fallback registrations for unhandled types but I can't seem to find that page any longer. In any case, my question is quite simple assuming I understand fallback registration.

I would like to setup the container to instantiate a NullCommandHandler<T> : ICommandHandler<T> type anytime it is unable to locate a registration for a ICommandHandler<T> service type. Is this possible?

Upvotes: 1

Views: 261

Answers (1)

Steven
Steven

Reputation: 172606

That's easy, you just use RegisterOpenGeneric. This method uses unregistered type resolution, which means it only gets picked up when there's no registration. So with your command handlers, it would look like this:

// Register all implementations of ICommandHandler<T>
container.RegisterManyForOpenGeneric(
    typeof(ICommandHandler<>),
    AppDomain.CurrentDomain.GetAssemblies());

container.RegisterOpenGeneric(
    typeof(ICommandHandler<>),
    typeof(NullCommandHandler<>),
    Lifestyle.Singleton);

Here is the documentation that you were searching for.

Upvotes: 1

Related Questions