Namespace: SimpleInjector
Assembly: SimpleInjector (in SimpleInjector.dll) Version: 5.3.0
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() { // 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:
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); }