ContainerAddRegistration Method (Type, Registration)

Simple Injector
Adds the registration for the supplied serviceType. This method can be used to apply the same Registration to multiple different service types.

Namespace:  SimpleInjector
Assembly:  SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
Syntax

public void AddRegistration(
	Type serviceType,
	Registration registration
)

Parameters

serviceType
Type: SystemType
The base type or interface to register.
registration
Type: SimpleInjectorRegistration
The registration that should be stored for the given serviceType.
Exceptions

ExceptionCondition
ArgumentNullExceptionThrown when one of the supplied arguments is a null reference.
ArgumentExceptionThrown when serviceType is not a reference type, is open generic, is ambiguous, when it is not assignable from the registration's ImplementationType or when the supplied registration is created for a different Container instance.
InvalidOperationException Thrown when this container instance is locked and can not be altered, or when an the serviceType has already been registered.
Examples

C#
public interface IFoo { }
public interface IBar { }
public class FooBar : IFoo, IBar { }

public void AddRegistration_SuppliedWithSameSingletonRegistrationTwice_ReturnsSameInstance()
{
    // Arrange
    Registration registration =
        Lifestyle.Singleton.CreateRegistration<FooBar, FooBar>(container);

    container.AddRegistration(typeof(IFoo), registration);
    container.AddRegistration(typeof(IBar), registration);

    // Act
    IFoo foo = container.GetInstance<IFoo>();
    IBar bar  = container.GetInstance<IBar>();

    // Assert
    bool fooAndBareAreTheSameInstance = object.ReferenceEquals(foo, bar);
    Assert.IsTrue(fooAndBareAreTheSameInstance);
}

In the example above a singleton registration is created for type FooBar and this registration is added to the container for each interface (IFoo and IBar) that it implements. Since both services use the same singleton registration, requesting those services will result in the return of the same (singleton) instance.

ExpressionBuilding events are applied to the Expression of the Registration instance and are therefore applied once. ExpressionBuilt events on the other hand get applied to the Expression of the InstanceProducer. Since each AddRegistration gets its own instance producer (that wraps the Registration instance), this means that the ExpressionBuilt events will be applied for each registered service type.

The most practical example of this is the use of decorators using one of the RegisterDecorator overloads (decorator registration use the ExpressionBuilt event under the covers). Take a look at the following example:

C#
public interface IFoo { }
public interface IBar { }
public class FooBar : IFoo, IBar { }

public class BarDecorator : IBar
{
    public BarDecorator(IBar decoratedBar)
    {
        this.DecoratedBar = decoratedBar;
    }

    public IBar DecoratedBar { get; private set; }
}

public void AddRegistration_SameSingletonRegistrationTwiceAndOneDecoratorApplied_ReturnsSameInstance()
{
    // Arrange
    Registration registration =
        Lifestyle.Singleton.CreateRegistration<FooBar, FooBar>(container);

    container.AddRegistration(typeof(IFoo), registration);
    container.AddRegistration(typeof(IBar), registration);

    // Registere a decorator for IBar, but not for IFoo
    container.RegisterDecorator(typeof(IBar), typeof(BarDecorator));

    // Act
    var foo = container.GetInstance<IFoo>();
    var decorator = container.GetInstance<IBar>() as BarDecorator;
    var bar = decorator.DecoratedBar;

    // Assert
    bool fooAndBareAreTheSameInstance = object.ReferenceEquals(foo, bar);
    Assert.IsTrue(fooAndBareAreTheSameInstance);
}
The example shows that the decorator gets applied to IBar but not to IFoo, but that the decorated IBar is still the same instance as the resolved IFoo instance.
See Also

Reference