public override IDiContainerBuilder AddScopedCollection <TService>(params Func <IDiContainer, TService>[] implementationFactories) { var registrations = implementationFactories.Select(implementationFactory => _scopedLifestyle.CreateRegistration(typeof(TService), () => implementationFactory(_container.GetInstance <IDiContainer>()), _container)); _container.Collection.Register <TService>(registrations); return(this); }
public static void RegisterFuncFactory <TService, TImpl>( this Container container, Lifestyle lifestyle = null) where TService : class where TImpl : class, TService { lifestyle = lifestyle ?? Lifestyle.Transient; // Register the Func<T> that resolves that instance. container.RegisterSingle( () => { var producer = new InstanceProducer( typeof(TService), lifestyle.CreateRegistration <TService, TImpl>(container)); Func <TService> instanceCreator = () => (TService)producer.GetInstance(); if (container.IsVerifying()) { instanceCreator.Invoke(); } return(instanceCreator); }); }
public void Register <TService>(Func <TService> instanceCreator, Lifestyle lifestyle, string name) where TService : class { var reg = lifestyle.CreateRegistration(instanceCreator, this); this.AddRegistration(typeof(TService), reg, name); }
// NOTE: This overload is needed because the addition of the (Type, object, Container) overload caused // C# to always pick that overload over the (Type, Func<object>, Container) overload in the base class. /// <summary> /// Creates a new <see cref="Registration"/> instance defining the creation of the /// specified <paramref name="serviceType"/> using the supplied <paramref name="instanceCreator"/> /// with the caching as specified by this lifestyle. /// </summary> /// <param name="serviceType">The interface or base type that can be used to retrieve the instances.</param> /// <param name="instanceCreator">The delegate that will be responsible for creating new instances.</param> /// <param name="container">The <see cref="Container"/> instance for which a /// <see cref="Registration"/> must be created.</param> /// <returns>A new <see cref="Registration"/> instance.</returns> /// <exception cref="ArgumentNullException">Thrown when on of the supplied arguments is a null /// reference.</exception> public new Registration CreateRegistration( Type serviceType, Func <object> instanceCreator, Container container) { Lifestyle lifestyle = this; return(lifestyle.CreateRegistration(serviceType, instanceCreator, container)); }
protected override Registration CreateRegistrationCore <TService>(Func <TService> instanceCreator, Container container) { Lifestyle lifestyle = this.options.SelectLifestyle(typeof(TService), typeof(TService)); return(lifestyle.CreateRegistration <TService>(instanceCreator, container)); }
public static void RegisterFuncFactory <TService, TImpl>( this Container container, Lifestyle lifestyle = null) where TService : class where TImpl : class, TService { lifestyle = lifestyle ?? Lifestyle.Transient; // Register the Func<T> that resolves that instance. container.RegisterSingleton <Func <TService> >(() => { var producer = new InstanceProducer(typeof(TService), lifestyle.CreateRegistration <TImpl>(container)); producer.Registration.SuppressDiagnosticWarning(DiagnosticType.DisposableTransientComponent, "Ignored during explicit Func Registration"); Func <TService> instanceCreator = () => (TService)producer.GetInstance(); //if (container.IsVerifying()) //{ // instanceCreator.Invoke(); //} return(instanceCreator); }); }
public static void RegisterAllConcreteTypesFor(this Container container, Type serviceType, Assembly assembly, Lifestyle lifestyle) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (serviceType == null) { throw new ArgumentNullException(nameof(serviceType)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } if (lifestyle == null) { throw new ArgumentNullException(nameof(lifestyle)); } var types = assembly.GetExportedTypes() .Where(type => serviceType.IsAssignableFrom(type) && !type.IsInterface && !type.IsAbstract); foreach (var type in types) { var registration = lifestyle.CreateRegistration(type, container); container.AddRegistration(type, registration); } }
public void Register(Type type, string name, Lifestyle lifestyle = null) { lifestyle = lifestyle ?? Lifestyle.Transient; var registration = lifestyle.CreateRegistration(typeof(IAction), type, _container); var producer = new InstanceProducer(typeof(IAction), registration); _producers.Add(name, producer); }
public LifestyleCreateRegistrationNonGeneric() { var container = new Container(); Lifestyle lifestyle = Lifestyle.Transient; lifestyle.CreateRegistration(typeof(ICommon), typeof(CommonImpl1), container); }
public LifestyleCreateRegistrationGeneric() { var container = new Container(); Lifestyle lifestyle = Lifestyle.Transient; lifestyle.CreateRegistration <ICommon, CommonImpl1>(container); }
public void Register <TService, TImplementation>(Lifestyle lifestyle, string name) where TImplementation : class, TService where TService : class { var reg = lifestyle.CreateRegistration <TImplementation>(this); this.AddRegistration(typeof(TService), reg, name); }
public static void Register<TService1, TService2, TImplementation>( this Container container, Lifestyle lifestyle) where TService1 : class where TService2 : class where TImplementation : class, TService1, TService2 { var registration = lifestyle.CreateRegistration<TImplementation, TImplementation>(container); container.AddRegistration(typeof(TService1), registration); container.AddRegistration(typeof(TService2), registration); }
public static void AppendCollection <TService, TImplementation>( this Container container, Lifestyle lifestyle, DiagnosticType suppression) where TImplementation : class, TService { var reg = lifestyle.CreateRegistration <TImplementation>(container); reg.SuppressDiagnosticWarning(suppression, "For testing"); container.Collection.Append(typeof(TService), reg); }
public void CreateRegistration_CalledWithValueType_ThrowsExpectedException() { // Arrange Container container = new Container(); Lifestyle lifestyle = Lifestyle.Transient; // Act Action action = () => lifestyle.CreateRegistration(typeof(int), container); // Assert AssertThat.ThrowsWithExceptionMessageContains <ArgumentException>( "The supplied type Int32 is not a reference type. Only reference types are supported.", action); }
public void CreateRegistrationFunc_SuppliedWithOpenGenericServiceType_ThrowsExpectedException() { // Arrange Container container = new Container(); Lifestyle lifestyle = Lifestyle.Transient; // Act Action action = () => lifestyle.CreateRegistration(typeof(ICommandHandler <>), () => null, container); // Assert AssertThat.ThrowsWithExceptionMessageContains <ArgumentException>( "The supplied type ICommandHandler<TCommand> is an open-generic type.", action); AssertThat.ThrowsWithParamName("serviceType", action); }
public void CreateRegistration_SuppliedWithOpenGenericImplementationType_ThrowsExpectedException() { // Arrange Container container = new Container(); Lifestyle lifestyle = Lifestyle.Transient; // Act Action action = () => lifestyle.CreateRegistration(typeof(object), typeof(NullCommandHandler <>), container); // Assert AssertThat.ThrowsWithExceptionMessageContains <ArgumentException>( "The supplied type NullCommandHandler<T> is an open generic type.", action); AssertThat.ThrowsWithParamName("implementationType", action); }
public InstanceProducer GetInstanceProducer(InjectionConsumerInfo consumer, bool throwOnFailure) { if (this.canResolve(consumer)) { var key = keyFunc(consumer); if (!registrations.ContainsKey(key)) { registrations[key] = lifestyle.CreateRegistration <T>(() => resolve(consumer), container); } var registration = registrations[key]; return(InstanceProducer.FromExpression( typeof(T), registration.BuildExpression(), container)); } return(innerBehavior.GetInstanceProducer(consumer, throwOnFailure)); }
private void Register(Type serviceType, Type implementationType, Lifestyle lifestyle) { var isOpenGeneric = serviceType.IsGenericTypeDefinition; if (isOpenGeneric) { this.container.RegisterOpenGeneric(serviceType, implementationType, lifestyle); } else { this.container.Register(serviceType, implementationType, lifestyle); // Registering collections in Simple Injector is done using the RegisterAll overloads, but // this forces all elements to be registered at once. For integration scenarios, the // AppendToCollection extension method can be used. This allows adding elements to a collection // one by one. this.container.AppendToCollection(serviceType, lifestyle.CreateRegistration(serviceType, implementationType, this.container)); } }
/// <summary> /// Mapeia interfaces pelo mesmo namespace /// </summary> /// <param name="container"></param> /// <param name="assembly"></param> /// <param name="lifestyle"></param> public static void AutoMap(this Container container, Assembly assembly, Lifestyle lifestyle) { container.ResolveUnregisteredType += (s, e) => { if (e.UnregisteredServiceType.IsInterface && !e.Handled) { Type[] concreteTypes = ( from type in assembly.GetTypes() where !type.IsAbstract && !type.IsGenericType where e.UnregisteredServiceType.IsAssignableFrom(type) select type) .ToArray(); if (concreteTypes.Length == 1) { e.Register(lifestyle.CreateRegistration(concreteTypes[0], container)); } } }; }
protected override Registration CreateRegistrationCore <TService, TImplementation>(Container container) { Lifestyle lifestyle = this.options.SelectLifestyle(typeof(TService), typeof(TImplementation)); return(lifestyle.CreateRegistration <TService, TImplementation>(container)); }
public void Register(Func <TService> instanceCreator, TKey key, Lifestyle lifestyle) { this.Register(lifestyle.CreateRegistration(typeof(TService), instanceCreator, this.container), key); }
public void Register(Type serviceType, Type implementationType, Lifestyle lifestyle, string name) { var reg = lifestyle.CreateRegistration(implementationType, this); this.AddRegistration(serviceType, reg, name); }
protected internal override Registration CreateRegistrationCore <TConcrete>(Container container) { Lifestyle lifestyle = this.options.SelectLifestyle(typeof(TConcrete)); return(lifestyle.CreateRegistration <TConcrete>(container)); }
private static void RegisterFallback <TService>(this Container container, Func <TService> factory, Lifestyle lifestyle) where TService : class => container.RegisterFallback <TService>(a => a.Register(lifestyle.CreateRegistration(factory, container)));
public void Register(Type implementationType, TKey key, Lifestyle lifestyle) { this.Register(lifestyle.CreateRegistration(typeof(TService), implementationType, this.container), key); }