예제 #1
0
        public static void Register <I, T>(LifetimeEnum lifetimeEnum) where T : I
        {
            switch (lifetimeEnum)
            {
            case LifetimeEnum.Transcient:
                UnityContainer.RegisterType <I, T>(new TransientLifetimeManager());
                break;

            case LifetimeEnum.Singleton:
                UnityContainer.RegisterType <I, T>(new ContainerControlledLifetimeManager());
                break;

            case LifetimeEnum.Hierarchical:
                UnityContainer.RegisterType <I, T>(new HierarchicalLifetimeManager());
                break;

            case LifetimeEnum.PerResolve:
                UnityContainer.RegisterType <I, T>(new PerResolveLifetimeManager());
                break;

            case LifetimeEnum.PerThread:
                UnityContainer.RegisterType <I, T>(new PerThreadLifetimeManager());
                break;

            case LifetimeEnum.External:
                UnityContainer.RegisterType <I, T>(new ExternallyControlledLifetimeManager());
                break;

            default:
                UnityContainer.RegisterType <I, T>(new ContainerControlledTransientManager());
                break;
            }
        }
예제 #2
0
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="cat">容器</param>
        /// <param name="from">服务类型</param>
        /// <param name="to">服务实现类型</param>
        /// <param name="lifetime">生命周期</param>
        /// <returns></returns>
        public static RobotCat Register(this RobotCat cat, Type from, Type to, LifetimeEnum lifetime)
        {
            Func <RobotCat, Type[], object> factory = (robotCat, arguments) => Create(robotCat, to, arguments);

            cat.Register(new ServiceRegistry(from, lifetime, factory));
            return(cat);
        }
예제 #3
0
        public SprocketWTWContainer Register(Type typeToRegister, Type typeToBuild, LifetimeEnum lifetime)
        {
            var details = new RegistrationDetails
            {
                RegisteredType = typeToRegister,
                ResolvedType   = typeToBuild,
                Lifetime       = lifetime
            };

            _registrationCache.RegisterType(details);
            return(this);
        }
예제 #4
0
        private void Register <TInterface, TImplementation>(
            LifetimeEnum lifetimeEnum,
            string shortName     = null,
            object[] constParams = null
            ) where TImplementation : TInterface
        {
            string key = GetKey(typeof(TInterface).FullName, shortName);

            // define type and life scope
            map[key] = new RegisteredService()
            {
                LifetimeType = lifetimeEnum,
                TargetType   = typeof(TImplementation)
            };

            // enable constant constructor
            if (constParams != null)
            {
                paramMap[key] = constParams;
            }
        }
예제 #5
0
 /// <summary>
 /// 注册
 /// </summary>
 /// <typeparam name="TService">服务泛型</typeparam>
 /// <param name="cat">容器</param>
 /// <param name="factory">实例化委托</param>
 /// <param name="lifetime">生命周期</param>
 /// <returns></returns>
 public static RobotCat Register <TService>(this RobotCat cat,
                                            Func <RobotCat, TService> factory, LifetimeEnum lifetime)
 {
     cat.Register(new ServiceRegistry(typeof(TService), lifetime, (_, arguments) => factory(_)));
     return(cat);
 }
예제 #6
0
 /// <summary>
 /// 注册
 /// </summary>
 /// <param name="cat">容器</param>
 /// <param name="serviceType">服务类型</param>
 /// <param name="factory">实例化委托</param>
 /// <param name="lifetime">生命周期</param>
 /// <returns></returns>
 public static RobotCat Register(this RobotCat cat, Type serviceType,
                                 Func <RobotCat, object> factory, LifetimeEnum lifetime)
 {
     cat.Register(new ServiceRegistry(serviceType, lifetime, (_, arguments) => factory(_)));
     return(cat);
 }
예제 #7
0
 /// <summary>
 /// 注册
 /// </summary>
 /// <typeparam name="TFrom">服务</typeparam>
 /// <typeparam name="TTo">服务实现</typeparam>
 /// <param name="cat">容器</param>
 /// <param name="lifetime">生命周期</param>
 /// <returns></returns>
 public static RobotCat Register <TFrom, TTo>(this RobotCat cat, LifetimeEnum lifetime) where TTo : TFrom
 => cat.Register(typeof(TFrom), typeof(TTo), lifetime);
 public MapToAttribute(Type serviceType, LifetimeEnum lifetime)
 {
     ServiceType = serviceType;
     Lifetime    = lifetime;
 }
예제 #9
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="serviceType">服务类型</param>
 /// <param name="lifetime">生命周期</param>
 /// <param name="factory">实例化委托</param>
 public ServiceRegistry(Type serviceType, LifetimeEnum lifetime, Func <RobotCat, Type[], object> factory)
 {
     ServiceType = serviceType;
     Lifetime    = lifetime;
     Factory     = factory;
 }
예제 #10
0
 public SprocketWTWContainer Register <I, T>(LifetimeEnum lifetime)
 {
     return(Register(typeof(I), typeof(T), lifetime));
 }