コード例 #1
0
        public void Register(Type dependencyType, Type implementationType, DependencyLifeTime dependencyLifeTime, ushort?dependencyName = null)
        {
            int dependencyMetadataToken     = dependencyType.MetadataToken;
            int implementationMetadataToken = implementationType.MetadataToken;
            int dependencyNameKey           = dependencyName != null?Convert.ToInt32(dependencyName) : -1;

            if (_dependencies.ContainsKey(dependencyMetadataToken))
            {
                if (_dependencies[dependencyMetadataToken].ContainsKey(dependencyNameKey))
                {
                    if (!_dependencies[dependencyMetadataToken][dependencyNameKey].Where(impl => impl.GenericTypeArguments.First().MetadataToken == implementationMetadataToken).Any())
                    {
                        _dependencies[dependencyMetadataToken][dependencyNameKey].Add(_lifeTypes[dependencyLifeTime].MakeGenericType(implementationType));
                    }
                }
                else
                {
                    _dependencies[dependencyMetadataToken].Add(dependencyNameKey, new List <Type> {
                        _lifeTypes[dependencyLifeTime].MakeGenericType(implementationType)
                    });
                }
            }
            else
            {
                _dependencies.Add(dependencyMetadataToken, new Dictionary <int, List <Type> > {
                    { dependencyNameKey, new List <Type> {
                          _lifeTypes[dependencyLifeTime].MakeGenericType(implementationType)
                      } }
                });
            }
        }
コード例 #2
0
        LifetimeManager GetLifetimeManager(DependencyLifeTime lifeTime)
        {
            LifetimeManager lifetimeManager = null;

            switch (lifeTime)
            {
            case DependencyLifeTime.PerDependency:
            {
                lifetimeManager = new PerResolveLifetimeManager();
                break;
            }

            case DependencyLifeTime.PerRequest:
            {
                lifetimeManager = new PerRequestLifetimeManager();
                break;
            }

            case DependencyLifeTime.SingleInstance:
            {
                lifetimeManager = new ContainerControlledLifetimeManager();
                break;
            }

            default:
            {
                lifetimeManager = new PerResolveLifetimeManager();
                break;
            }
            }
            return(lifetimeManager);
        }
コード例 #3
0
 public void Register <TService, TImplementation>(
     DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
     where TService : class
     where TImplementation : class, TService
 {
     IocContainer.Register <TService, TImplementation>(ConvertLifetimeToReuse(lifeTime));
 }
コード例 #4
0
 public void Register <TService>(Func <IIocResolver, TService> implementationFactory,
                                 DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
     where TService : class
 {
     IocContainer.RegisterDelegate(
         resolver => implementationFactory(resolver.Resolve <IIocResolver>()),
         ConvertLifetimeToReuse(lifeTime));
 }
コード例 #5
0
 internal static IIocRegistrator RegisterMany(
     this IIocRegistrator registrator,
     Type implementationType,
     bool autoInjectProperty     = false,
     DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
 {
     registrator.RegisterMany(
         new[] { implementationType }, autoInjectProperty, lifeTime);
     return(registrator);
 }
コード例 #6
0
        /// <summary>
        /// Registers a type as self registration if it's not registered before.
        /// </summary>
        /// <param name="iocRegistror">Registrar</param>
        /// <param name="type">Type of the class</param>
        /// <param name="lifeTime">Life time of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot(this IIocRegistrator iocRegistror,
                                         Type type, DependencyLifeTime lifeTime = DependencyLifeTime.Singleton)
        {
            if (iocRegistror.IsRegistered(type))
            {
                return(false);
            }

            iocRegistror.Register(type, lifeTime);
            return(true);
        }
コード例 #7
0
 internal static IIocRegistrator RegisterMany <TImplementation>(
     this IIocRegistrator registrator,
     bool autoInjectProperty     = false,
     DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
 {
     registrator.AsDryIocManager().IocContainer
     .RegisterMany <TImplementation>(
         DryIocMannager.ConvertLifetimeToReuse(lifeTime),
         autoInjectProperty ? PropertiesAndFields.Auto : null);
     return(registrator);
 }
コード例 #8
0
        /// <summary>
        /// Registers a type as self registration if it's not registered before.
        /// </summary>
        /// <typeparam name="T">Type of the class</typeparam>
        /// <param name="iocRegistror">Registrar</param>
        /// <param name="lifeTime">Life time of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot <T>(this IIocRegistrator iocRegistror,
                                             DependencyLifeTime lifeTime = DependencyLifeTime.Singleton)
            where T : class
        {
            if (iocRegistror.IsRegistered <T>())
            {
                return(false);
            }

            iocRegistror.Register <T>(lifeTime);
            return(true);
        }
コード例 #9
0
        /// <summary>
        /// Registers a type with it's implementation if it's not registered before.
        /// </summary>
        /// <param name="iocRegistror">Registrar</param>
        /// <param name="serviceType">Type of the class</param>
        /// <param name="implementationType">The type that implements <paramref name="serviceType"/></param>
        /// <param name="lifeTime">Life time of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot(
            this IIocRegistrator iocRegistror,
            Type serviceType, Type implementationType,
            DependencyLifeTime lifeTime = DependencyLifeTime.Singleton)
        {
            if (iocRegistror.IsRegistered(serviceType))
            {
                return(false);
            }

            iocRegistror.Register(serviceType, implementationType, lifeTime);
            return(true);
        }
コード例 #10
0
        /// <summary>
        /// Registers a type with it's implementation if it's not registered before.
        /// </summary>
        /// <typeparam name="TService">Registering type</typeparam>
        /// <typeparam name="TImplementation">The type that implements <see cref="TType"/></typeparam>
        /// <param name="iocRegistror">Registrar</param>
        /// <param name="lifeTime">Life time of the objects of this type</param>
        /// <returns>True, if registered for given implementation.</returns>
        public static bool RegisterIfNot <TService, TImplementation>(
            this IIocRegistrator iocRegistror,
            DependencyLifeTime lifeTime = DependencyLifeTime.Singleton)
            where TService : class
            where TImplementation : class, TService
        {
            if (iocRegistror.IsRegistered <TService>())
            {
                return(false);
            }

            iocRegistror.Register <TService, TImplementation>(lifeTime);
            return(true);
        }
コード例 #11
0
 internal static IIocRegistrator RegisterMany(
     this IIocRegistrator registrator,
     IEnumerable <Type> seviceTypes,
     Type implementationType,
     bool autoInjectProperty     = false,
     DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
 {
     registrator.AsDryIocManager().IocContainer.RegisterMany(
         seviceTypes.ToArray(),
         implementationType,
         DryIocMannager.ConvertLifetimeToReuse(lifeTime),
         autoInjectProperty ? PropertiesAndFields.Auto : null);
     return(registrator);
 }
コード例 #12
0
        internal static IReuse ConvertLifetimeToReuse(DependencyLifeTime lifeTime)
        {
            switch (lifeTime)
            {
            case DependencyLifeTime.Transient:
                return(Reuse.Transient);

            case DependencyLifeTime.Scoped:
                return(Reuse.ScopedOrSingleton);

            case DependencyLifeTime.Singleton:
                return(Reuse.Singleton);

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(lifeTime), lifeTime, "Not supported lifetime");
            }
        }
コード例 #13
0
        internal static IIocRegistrator RegisterAssemblies(
            this IIocRegistrator registrator,
            IEnumerable <Assembly> assemblies,
            Func <Type, bool> typeSelector,
            bool autoInjectProperty,
            DependencyLifeTime lifeTime)
        {
            Check.NotNull(registrator, nameof(registrator));
            assemblies = assemblies ?? AppDomain.CurrentDomain.GetAssemblies();

            var types = assemblies
                        .SelectMany(asm => asm.DefinedTypes)
                        .Select(typeInfo => typeInfo.AsType())
                        .Where(type => typeSelector?.Invoke(type) ?? true);

            foreach (var type in types)
            {
                registrator.RegisterMany(type, autoInjectProperty, lifeTime);
            }

            return(registrator);
        }
コード例 #14
0
        private void MakeLifeTime <TLimit, TActivatorData, TRegistrationStyle>(
            IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> reg, DependencyLifeTime lifeTime)
        {
            switch (lifeTime)
            {
            case DependencyLifeTime.PerDependency:
            {
                reg.InstancePerDependency();
                break;
            }

            case DependencyLifeTime.PerRequest:
            {
                reg.InstancePerLifetimeScope();
                break;
            }

            case DependencyLifeTime.SingleInstance:
            {
                reg.SingleInstance();
                break;
            }
            }
        }
コード例 #15
0
 public IContainerAdapter RegisterType <T>(DependencyLifeTime lifeTime)
 {
     MakeLifeTime(_builder.RegisterType <T>(), lifeTime);
     return(this);
 }
コード例 #16
0
 public void Register(Type serviceType, Type implementationType,
                      DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
 {
     IocContainer.Register(serviceType,
                           implementationType, ConvertLifetimeToReuse(lifeTime));
 }
コード例 #17
0
 public void Register(Type type,
                      DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
 {
     IocContainer.Register(type, reuse: ConvertLifetimeToReuse(lifeTime));
 }
コード例 #18
0
 public void Register <T>(
     DependencyLifeTime lifeTime = DependencyLifeTime.Transient)
     where T : class
 {
     IocContainer.Register <T>(reuse: ConvertLifetimeToReuse(lifeTime));
 }
コード例 #19
0
 public IContainerAdapter RegisterType <TIt, T>(DependencyLifeTime lifeTime) where T : TIt
 {
     _container.RegisterType <TIt, T>(GetLifetimeManager(lifeTime));
     _container.RegisterType <TIt, T>(typeof(TIt).Name + typeof(T).FullName, GetLifetimeManager(lifeTime));
     return(this);
 }
コード例 #20
0
 public IContainerAdapter RegisterType(Type itype, Type type, DependencyLifeTime lifeTime)
 {
     _container.RegisterType(itype, type, GetLifetimeManager(lifeTime));
     _container.RegisterType(itype, type, itype.Name + type.FullName, GetLifetimeManager(lifeTime));
     return(this);
 }
コード例 #21
0
 public IContainerAdapter RegisterType <TIt, T>(DependencyLifeTime lifeTime) where T : TIt
 {
     MakeLifeTime(_builder.RegisterType <T>().As <TIt>(), lifeTime);
     return(this);
 }
コード例 #22
0
 public void Register <T, F>(DependencyLifeTime dependencyLifeTime, ushort?dependencyName = null)
 {
     Register(typeof(T), typeof(F), dependencyLifeTime, dependencyName);
 }
コード例 #23
0
 public IContainerAdapter RegisterType(Type itype, Type type, DependencyLifeTime lifeTime)
 {
     MakeLifeTime(_builder.RegisterType(type).As(itype), lifeTime);
     return(this);
 }
コード例 #24
0
 public Dependency(Type type, DependencyLifeTime dependencyLifeTime)
 {
     DependencyLifeTime = dependencyLifeTime;
     Type = type;
 }