Beispiel #1
0
        public virtual void RegisterGeneric(Type sourceType, Type targetType, RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve)
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }


            Action <ContainerBuilder> registrationAction;

            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                registrationAction = builder => builder.RegisterGeneric(targetType).As(sourceType);
                break;

            case RegistrationLifetime.InstancePerApplication:
                registrationAction = builder => builder.RegisterGeneric(targetType).As(sourceType).SingleInstance();
                break;

            default:
                throw new NotSupportedException("This operation is not supported");
            }

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }
Beispiel #2
0
        public void RegisterType <T, U>(RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve) where U : T
        {
            _container.RegisterType <T, U>(registrationLifetime);

            JsonPropertyConverterRepository.TryOverride <T, U>();
            JsonPropertiesConverterRepository.TryOverride <T, U>();
        }
Beispiel #3
0
        public virtual void RegisterType <TRegistered, TTo>(RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve) where TTo : TRegistered
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }

            Action <ContainerBuilder> registrationAction;

            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                registrationAction = builder => builder.RegisterType <TTo>().As <TRegistered>();
                break;

            case RegistrationLifetime.InstancePerApplication:
                registrationAction = builder => builder.RegisterType <TTo>().As <TRegistered>().SingleInstance();
                break;

            default:
                throw new NotSupportedException("This operation is not supported");
            }

            JsonPropertyConverterRepository.TryOverride <TRegistered, TTo>();
            JsonPropertiesConverterRepository.TryOverride <TRegistered, TTo>();

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }
Beispiel #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="instanceType">Type instance.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 /// <param name="mode">Mode to use when searching for ctors.</param>
 /// <param name="registrationTypes">Registration types.</param>
 public TypeRegistration(Type instanceType, RegistrationLifetime lifetime, TypeResolutionMode mode, params Type[] registrationTypes)
 {
     InstanceType      = instanceType ?? throw new ArgumentNullException(nameof(instanceType));
     _abstractionTypes = registrationTypes?.ToList() ?? new List <Type>();
     _abstractionTypes.Add(instanceType);
     Lifetime = lifetime;
     Mode     = mode;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="InstanceTypeRegistration"/> class.
 /// </summary>
 /// <param name="value">Object instance value to register.</param>
 /// <param name="types">Collection of types to register as.</param>
 /// <param name="lifetime">Lifetime to consider for this registration.</param>
 public InstanceTypeRegistration(object value, RegistrationLifetime lifetime, params Type[] types)
 {
     Value            = value ?? throw new ArgumentNullException(nameof(value));
     AbstractionTypes = types ?? throw new ArgumentNullException(nameof(types));
     if (!types.Any())
     {
         throw new ArgumentException("InstanceTypeRegistration.ctor() : It's necessary to add at least one type to register as.");
     }
     Lifetime = lifetime;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FactoryRegistration"/> class.
 /// </summary>
 /// <param name="factory">Object instance value to register.</param>
 /// <param name="types">Collection of types to register as.</param>
 /// <param name="lifetime">Lifetime to consider for this registration.</param>
 public FactoryRegistration(Func <object> factory, RegistrationLifetime lifetime, params Type[] types)
 {
     AbstractionTypes = types ?? throw new ArgumentNullException(nameof(types));
     Factory          = factory ?? throw new ArgumentNullException(nameof(factory));
     if (types.Length == 0)
     {
         throw new ArgumentException("FactoryRegistration.ctor() : It's necessary to add at least one type to register as.");
     }
     Lifetime = lifetime;
 }
Beispiel #7
0
 private static IRegistrationBuilder <object, TActivatorData, TRegistrationStyle> AddLifetime <TActivatorData, TRegistrationStyle>(
     this IRegistrationBuilder <object, TActivatorData, TRegistrationStyle> registration,
     RegistrationLifetime lifetime)
 {
     return(lifetime switch
     {
         RegistrationLifetime.Scoped => registration.InstancePerLifetimeScope(),
         RegistrationLifetime.Singleton => registration.SingleInstance(),
         RegistrationLifetime.Transient => registration.InstancePerDependency(),
         _ => throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, $"Specified lifetime {lifetime} is unknown"),
     });
Beispiel #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="instanceType">Type instance.</param>
 /// <param name="forEverything">Flag that indicates if should register has everything possible.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 /// <param name="mode">Mode to use when searching for ctors.</param>
 public TypeRegistration(Type instanceType, bool forEverything, RegistrationLifetime lifetime, TypeResolutionMode mode)
 {
     InstanceType = instanceType;
     if (forEverything)
     {
         _abstractionTypes.Add(instanceType);
         _abstractionTypes.AddRange(instanceType.GetInterfaces());
     }
     Lifetime = lifetime;
     Mode     = mode;
 }
 private static Lifestyle GetLifestyle(RegistrationLifetime lifetime)
 {
     switch (lifetime)
     {
         case RegistrationLifetime.Singleton:
             return Lifestyle.Singleton;
         case RegistrationLifetime.Scoped:
             return new ExecutionContextScopeLifestyle();
         case RegistrationLifetime.Transient:
             return Lifestyle.Transient;
         default:
             throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
     }
 }
        public void RegisterGeneric(Type sourceType, Type targetType, RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve)
        {
            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                _containerBuilder.RegisterGeneric(targetType).As(sourceType);
                return;

            case RegistrationLifetime.InstancePerThread:
                _containerBuilder.RegisterGeneric(targetType).As(sourceType).InstancePerLifetimeScope();
                return;

            case RegistrationLifetime.InstancePerApplication:
                _containerBuilder.RegisterGeneric(targetType).As(sourceType).SingleInstance();
                return;
            }
        }
        public void RegisterType <T, U>(RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve) where U : T
        {
            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                _containerBuilder.RegisterType <U>().As <T>();
                return;

            case RegistrationLifetime.InstancePerThread:
                _containerBuilder.RegisterType <U>().As <T>().InstancePerLifetimeScope();
                return;

            case RegistrationLifetime.InstancePerApplication:
                _containerBuilder.RegisterType <U>().As <T>().SingleInstance();
                return;
            }
        }
 private static GenericFamilyExpression LifecycleIs(this GenericFamilyExpression instance, RegistrationLifetime lifetime)
 {
     switch (lifetime)
     {
         case RegistrationLifetime.Singleton:
             return instance.LifecycleIs(Lifecycles.Singleton);
         case RegistrationLifetime.Scoped:
             // Transient in StructureMap will yield the same instance within a nested container.
             return instance.LifecycleIs(Lifecycles.Transient);
         case RegistrationLifetime.Transient:
             return instance.LifecycleIs(Lifecycles.Unique);
         default:
             throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
     }
 }
Beispiel #13
0
 internal TypeRegistration([NotNull] Type serviceType, [NotNull] Type implementationType, RegistrationLifetime lifetime)
     : base(serviceType, lifetime)
 {
     ImplementationType = Check.NotNull(implementationType, nameof(implementationType));
 }
 public void RegisterType <T, U>(RegistrationLifetime registrationLifetime) where U : T
 {
     throw new NotImplementedException();
 }
 public void RegisterGeneric(Type sourceType, Type targetType, RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="forEverything">Flag that indicates if should register has everything possible.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 public TypeRegistration(bool forEverything, RegistrationLifetime lifetime) :
     base(typeof(T), forEverything, lifetime)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="forEverything">Flag that indicates if should register has everything possible.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 /// <param name="mode">Mode to use when searching for ctors.</param>
 public TypeRegistration(bool forEverything, RegistrationLifetime lifetime, TypeResolutionMode mode) :
     base(typeof(T), forEverything, lifetime, mode)
 {
 }
Beispiel #18
0
 internal Registration([NotNull] Type serviceType, RegistrationLifetime lifetime)
 {
     ServiceType = Check.NotNull(serviceType, nameof(serviceType));
     Lifetime = lifetime;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="registrationTypes">Registration types.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 /// <param name="mode">Mode to use when searching for ctors.</param>
 public TypeRegistration(RegistrationLifetime lifetime, TypeResolutionMode mode, params Type[] registrationTypes)
     : base(typeof(T), lifetime, mode, registrationTypes)
 {
 }
 internal CollectionTypeRegistration([NotNull] Type serviceType, [NotNull] IReadOnlyCollection<Type> implementationTypes, RegistrationLifetime lifetime)
     : base(serviceType, lifetime)
 {
     ImplementationTypes = Check.NotNull(implementationTypes, nameof(implementationTypes));
 }
Beispiel #21
0
        private static IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> AddLifetime <TLimit, TActivatorData, TRegistrationStyle>(IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> registration, RegistrationLifetime lifetime)
        {
            switch (lifetime)
            {
            case RegistrationLifetime.Scoped:
                registration.InstancePerLifetimeScope();
                break;

            case RegistrationLifetime.Singleton:
                registration.SingleInstance();
                break;
            }
            return(registration);
        }
Beispiel #22
0
 public void RegisterGeneric(Type sourceType, Type targetType, RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve)
 {
     _container.RegisterGeneric(sourceType, targetType, registrationLifetime);
 }
Beispiel #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="instanceType">Type instance.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 /// <param name="registrationTypes">Registration types.</param>
 public TypeRegistration(Type instanceType, RegistrationLifetime lifetime, params Type[] registrationTypes)
     : this(instanceType, lifetime, TypeResolutionMode.Full, registrationTypes)
 {
 }
Beispiel #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TypeRegistration"/> class.
 /// </summary>
 /// <param name="instanceType">Type instance.</param>
 /// <param name="forEverything">Flag that indicates if should register has everything possible.</param>
 /// <param name="lifetime">Lifetime to consider for this type registration.</param>
 public TypeRegistration(Type instanceType, bool forEverything, RegistrationLifetime lifetime)
     : this(instanceType, forEverything, lifetime, TypeResolutionMode.Full)
 {
 }