Skip to content

Commit

Permalink
Note about open generics and IfNotRegistered.
Browse files Browse the repository at this point in the history
  • Loading branch information
tillig committed Jun 15, 2023
1 parent ae722d1 commit d110cae
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/register/registration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,15 @@ These extensions run at the time of ``ContainerBuilder.Build()`` and will execut
reg.IsRegistered(new TypedService(typeof(IService))) &&
reg.IsRegistered(new TypedService(typeof(HandlerB))));

// Be careful with open generics - IfNotRegistered and IsRegistered
// only work with CLOSED generics because that's what you'd resolve!
// Note the check is for a closed generic that would be registered
// as part of the open generic - if you put an open generic in the
// IfNotRegistered check, it'll always appear not registered.
builder.RegisterGeneric(typeof(CommandHandler<>))
.As(typeof(ICommandHandler<>))
.IfNotRegistered(typeof(ICommandHandler<MyCommand>));

// This is when the conditionals actually run. Again,
// they run in the order the registrations were added
// to the ContainerBuilder.
Expand Down

0 comments on commit d110cae

Please sign in to comment.