Container.AddRegistration Method (Type, Registration)
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
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.
| Exception | Condition |
|---|---|
| ArgumentNullException | Thrown when one of the supplied arguments is a null reference. |
| ArgumentException | Thrown 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. |
public interface IFoo { } public interface IBar { } public class FooBar : IFoo, IBar { } public void AddRegistration_SuppliedWithSameSingletonRegistrationTwice_ReturnsSameInstance() { Registration registration = Lifestyle.Singleton.CreateRegistration<FooBar, FooBar>(container); container.AddRegistration(typeof(IFoo), registration); container.AddRegistration(typeof(IBar), registration); IFoo foo = container.GetInstance<IFoo>(); IBar bar = container.GetInstance<IBar>(); 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:
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() { Registration registration = Lifestyle.Singleton.CreateRegistration<FooBar, FooBar>(container); container.AddRegistration(typeof(IFoo), registration); container.AddRegistration(typeof(IBar), registration); container.RegisterDecorator(typeof(IBar), typeof(BarDecorator)); var foo = container.GetInstance<IFoo>(); var decorator = container.GetInstance<IBar>() as BarDecorator; var bar = decorator.DecoratedBar; 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.