示例#1
0
        public void Should_be_able_to_register(LifetimeType lifetime)
        {
            var bindingName = new Mock <IBindingNamedWithOrOnSyntax>();

            var bindingWhen = new Mock <IBindingWhenInNamedWithOrOnSyntax>();

            if (lifetime == LifetimeType.Transient)
            {
                bindingWhen.Setup(b => b.InTransientScope()).Returns(bindingName.Object).Verifiable();
            }
            else if (lifetime == LifetimeType.Transient)
            {
                bindingWhen.Setup(b => b.InSingletonScope()).Returns(bindingName.Object).Verifiable();
            }
            else if (lifetime == LifetimeType.PerRequest)
            {
                var func = (Func <IContext, object>)Delegate.CreateDelegate(typeof(Func <IContext, object>), typeof(RequestScopeExtensionMethod).GetMethod("GetScope", BindingFlags.NonPublic | BindingFlags.Static));
                bindingWhen.Setup(b => b.InScope(func)).Returns(bindingName.Object).Verifiable();
            }

            var bindingTo = new Mock <IBindingToSyntax>();

            bindingTo.Setup(b => b.To(It.IsAny <Type>())).Returns(bindingWhen.Object);
            kernel.Setup(k => k.Bind(It.IsAny <Type>())).Returns(bindingTo.Object);

            adapter.RegisterType(typeof(object), typeof(object), lifetime);

            bindingWhen.Verify();
        }
示例#2
0
 public DependencyObject(Type interfaceType, Type classType, LifetimeType lifetimeType, string name)
 {
     InterfaceType = interfaceType;
     ClassType     = classType;
     Lifetime      = lifetimeType;
     Name          = name;
 }
示例#3
0
 public DependencyObject(Type interfaceType, Type classType, LifetimeType lifetimeType = LifetimeType.ContainerController, bool isIntercepted = false)
 {
     InterfaceType = interfaceType;
     ClassType     = classType;
     Lifetime      = lifetimeType;
     IsIntercepted = isIntercepted;
 }
示例#4
0
        private static LifetimeType GetLifetime(TimeSpan?lifetimeSpan)
        {
            LifetimeType result;

            if (lifetimeSpan.HasValue)
            {
                DateTime startTime = DateTime.UtcNow;
                DateTime endTime   = startTime + lifetimeSpan.Value;

                result = new LifetimeType {
                    Created = new AttributedDateTime {
                        Value = XmlConvert.ToString(startTime, XmlDateTimeSerializationMode.Utc)
                    },
                    Expires = new AttributedDateTime {
                        Value = XmlConvert.ToString(endTime, XmlDateTimeSerializationMode.Utc)
                    }
                };
            }
            else
            {
                result = null;
            }

            return(result);
        }
 public static DIRegisterTypeObject <TRegister> CreateTypeObject <TRegister>(
     IEnumerable <Type> aliasTypes  = null,
     LifetimeType lifetimeType      = LifetimeType.Dependency,
     IEnumerable <string> scopeTags = null)
 {
     return(new DIRegisterTypeObject <TRegister>(aliasTypes, lifetimeType, scopeTags));
 }
示例#6
0
 protected virtual void Inject <T1, T2, T3, T4>(LifetimeType lifetimeType = LifetimeType.ContainerController, params object[] constructorParameters)
     where T4 : class, T1, T2, T3, new()
 {
     Inject <T1, T4>(lifetimeType, constructorParameters);
     Inject <T2, T4>(lifetimeType, constructorParameters);
     Inject <T3, T4>(lifetimeType, constructorParameters);
 }
示例#7
0
        public static IRegistrationBuilder <object, TActivatorData, TRegistrationStyle> ConfigureLifecycle
        <TActivatorData, TRegistrationStyle>(
            this IRegistrationBuilder <object, TActivatorData, TRegistrationStyle> registrationBuilder,
            LifetimeType lifetimeType, bool classicWeb = false)
        {
            switch (lifetimeType)
            {
            case LifetimeType.Singleton:
                registrationBuilder.SingleInstance();
                break;

            case LifetimeType.Scoped:
                if (classicWeb)     //todo InstancePerRequest for asp.net ?
                {
                    registrationBuilder.InstancePerRequest();
                }
                else
                {
                    registrationBuilder.InstancePerLifetimeScope();
                }
                break;

            case LifetimeType.Transient:
                registrationBuilder.InstancePerDependency();
                break;
            }

            return(registrationBuilder);
        }
示例#8
0
 public DependencyObject(Type interfaceType, Type classType, LifetimeType lifetimeType, string name, bool isIntercepted = false)
 {
     InterfaceType = interfaceType;
     ClassType     = classType;
     Lifetime      = lifetimeType;
     Name          = name;
     IsIntercepted = isIntercepted;
 }
示例#9
0
        private void Register <TInterface, TEntity>(LifetimeType lifetimeType) where TEntity : TInterface
        {
            ContainerObject @object = new ContainerObject();

            @object.Lifetime   = lifetimeType;
            @object.ObjectType = typeof(TEntity);
            this._ContainerDic.Add(typeof(TInterface).FullName, @object);
        }
示例#10
0
文件: IocRegistrar.cs 项目: d18zj/qim
 public void RegisterDelegate(Type serviceType, Func <IIocResolver, object> factoryDelegate,
                              LifetimeType lifetime = LifetimeType.Transient,
                              string name           = null)
 {
     Ensure.NotNull(serviceType, nameof(serviceType));
     Ensure.NotNull(factoryDelegate, nameof(factoryDelegate));
     _container.RegisterDelegate(serviceType, r => factoryDelegate(r.Resolve <IIocResolver>()),
                                 ConvertLifetimeToReuse(lifetime), serviceKey: name);
 }
示例#11
0
 public DependencyObject(Type interfaceType, Type classType, LifetimeType lifetimeType, string name, string parserName = null, params object[] parameters)
 {
     InterfaceType = interfaceType;
     ClassType     = classType;
     Lifetime      = lifetimeType;
     Name          = name;
     ParserName    = parserName;
     Parameters    = parameters ?? new object[] {};
 }
 public DIRegisterTypeObject(
     Type registerType,
     IEnumerable <Type> aliasTypes  = null,
     LifetimeType lifetimeType      = LifetimeType.Dependency,
     IEnumerable <object> scopeTags = null) : base(aliasTypes)
 {
     this.RegisterType = registerType;
     this.LifetimeType = lifetimeType;
     this.ScopeTags    = scopeTags?.ToList() ?? new List <object>();
 }
        public void Should_be_able_to_register_type(LifetimeType lifetime)
        {
            var registry = (ConfigurationExpression)Activator.CreateInstance(typeof(ConfigurationExpression), true);

            container.Setup(c => c.Configure(It.IsAny <Action <ConfigurationExpression> >())).Callback((Action <ConfigurationExpression> x) => x(registry)).Verifiable();

            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            container.Verify();
        }
示例#14
0
文件: IocRegistrar.cs 项目: d18zj/qim
        public void RegisterDelegate(Type serviceType, Func <IIocResolver, object> factoryDelegate,
                                     LifetimeType lifetime = LifetimeType.Transient,
                                     string name           = null)
        {
            Ensure.NotNull(serviceType, nameof(serviceType));
            Ensure.NotNull(factoryDelegate, nameof(factoryDelegate));

            var registration =
                RegistrationBuilder.ForDelegate(serviceType,
                                                (context, parameters) => factoryDelegate(context.Resolve <IIocResolver>()))
                .ConfigureLifecycle(lifetime).CreateRegistration();

            _builder.RegisterComponent(registration);
        }
        /// <summary>
        /// Register type mappings with container by configuration.
        /// </summary>
        /// <returns>The <see cref="IContainerProvider" /> object that this method was called on.</returns>
        public override IContainerProvider RegisterByConfiguration()
        {
            var section = ConfigurationManagerWrapper.Current.GetSection <UnityConfigurationSection>("unity");

            foreach (var sectionContainer in section.Containers)
            {
                foreach (var extension in sectionContainer.Extensions)
                {
                    var unityExtension = container.Resolve(BuildManagerWrapper.Current.ResolveType(extension.TypeName)) as UnityContainerExtension;

                    if (unityExtension != null)
                    {
                        container.AddExtension(unityExtension);
                    }
                }

                foreach (var registration in sectionContainer.Registrations)
                {
                    Type interfaceType = BuildManagerWrapper.Current.ResolveType(registration.TypeName);
                    Type concreteType  = BuildManagerWrapper.Current.ResolveType(registration.MapToName);

                    LifetimeType lifetimeType = LifetimeType.Default;

                    switch (registration.Lifetime.TypeName)
                    {
                    case "singleton": lifetimeType = LifetimeType.Singleton; break;

                    case "transient": lifetimeType = LifetimeType.Transient; break;
                    }

                    RegisterType(interfaceType, concreteType, lifetimeType);
                }

                foreach (var instance in sectionContainer.Instances)
                {
                    Type interfaceType     = BuildManagerWrapper.Current.ResolveType(instance.TypeName);
                    Type typeConverterType = BuildManagerWrapper.Current.ResolveType(instance.TypeConverterTypeName);

                    var converter = container.Resolve(typeConverterType) as TypeConverter;

                    if (converter != null)
                    {
                        RegisterInstance(interfaceType, converter.ConvertFromInvariantString(instance.Value));
                    }
                }
            }

            return(this);
        }
示例#16
0
        /// <summary>
        /// Registers the specified service type with corresponding implementation type.
        /// </summary>
        /// <param name="serviceType">Service type.</param>
        /// <param name="implementationType">Implementation type.</param>
        /// <param name="lifetimeType">Lifetime type of the registering services type.</param>
        public void Register(Type serviceType, Type implementationType, LifetimeType lifetimeType = LifetimeType.Singleton)
        {
            switch (lifetimeType)
            {
            case LifetimeType.Transient:
                Container.Register(Component.For(serviceType).ImplementedBy(implementationType).LifestyleTransient());
                break;

            case LifetimeType.Singleton:
                Container.Register(Component.For(serviceType).ImplementedBy(implementationType).LifestyleSingleton());
                break;

            case LifetimeType.PerLifetimeScope:
                Container.Register(Component.For(serviceType).ImplementedBy(implementationType).LifestyleScoped());
                break;
            }
        }
        /// <summary>
        /// Registers the specified service type with corresponding implementation type.
        /// </summary>
        /// <param name="serviceType">Service type.</param>
        /// <param name="implementationType">Implementation type.</param>
        /// <param name="lifetimeType">Lifetime type of the registering services type.</param>
        public void Register(Type serviceType, Type implementationType, LifetimeType lifetimeType = LifetimeType.PerLifetimeScope)
        {
            switch (lifetimeType)
            {
            case LifetimeType.Transient:
                Container.Register(serviceType, implementationType, Reuse.Transient);
                break;

            case LifetimeType.Singleton:
                Container.Register(serviceType, implementationType, Reuse.Singleton);
                break;

            case LifetimeType.PerLifetimeScope:
                Container.Register(serviceType, implementationType, Reuse.InCurrentScope);
                break;
            }
        }
示例#18
0
文件: IocRegistrar.cs 项目: d18zj/qim
        public void RegisterType(Type implementationType, string name = null, LifetimeType lifetime = LifetimeType.Transient, object constructorArgsAsAnonymousType = null)
        {
            Ensure.NotNull(implementationType, nameof(implementationType));
            Made made = null;

            if (constructorArgsAsAnonymousType != null)
            {
                made = GetParameterSelector(constructorArgsAsAnonymousType);
            }
            _container.Register(implementationType, ConvertLifetimeToReuse(lifetime), made: made, serviceKey: name);
            if (_interceptType.IsAssignableFrom(implementationType)) //虚方法拦截
            {
                PipelineManager.Instance.InterceptByVirtualMethod(implementationType);

                _container.Intercept <DynamicProxyInterceptor>(implementationType, name);
            }
        }
示例#19
0
文件: IocRegistrar.cs 项目: d18zj/qim
        protected IReuse ConvertLifetimeToReuse(LifetimeType lifetime)
        {
            switch (lifetime)
            {
            case LifetimeType.Transient:
                return(Reuse.Transient);

            case LifetimeType.Singleton:
                return(Reuse.InCurrentNamedScope(Container.NonAmbientRootScopeName));

            case LifetimeType.Scoped:
                return(Reuse.InCurrentScope);

            default:
                throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
            }
        }
示例#20
0
文件: IocRegistrar.cs 项目: d18zj/qim
        protected ILifetime GetLifetime(LifetimeType lifetime)
        {
            switch (lifetime)
            {
            case LifetimeType.Transient:
                return(new PerRequestLifeTime());

            case LifetimeType.Singleton:
                return(new PerContainerLifetime());

            case LifetimeType.Scoped:
                return(new PerScopeLifetime());

            default:
                throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
            }
        }
示例#21
0
文件: IocRegistrar.cs 项目: d18zj/qim
        public void RegisterType(Type serviceType, Type implementationType, string name,
                                 LifetimeType lifetime = LifetimeType.Transient,
                                 object constructorArgsAsAnonymousType = null)
        {
            Ensure.NotNull(serviceType, nameof(serviceType));
            Ensure.NotNull(implementationType, nameof(implementationType));
            //var builder = _builder;

            var serviceTypeInfo     = serviceType.GetTypeInfo();
            var registrationBuilder =
                serviceTypeInfo.IsGenericTypeDefinition
                    ? (IRegistrationBuilder <object, ReflectionActivatorData, dynamic>)
                _builder.RegisterGeneric(implementationType)
                    : _builder.RegisterType(implementationType);

            if (serviceType != implementationType)
            {
                registrationBuilder.As(serviceType);
            }
            registrationBuilder.ConfigureLifecycle(lifetime);

            if (!string.IsNullOrEmpty(name))
            {
                registrationBuilder.Named(name, serviceType);
            }

            if (constructorArgsAsAnonymousType != null)
            {
                registrationBuilder.WithParameters(Extensions.GeTypedParameters(constructorArgsAsAnonymousType));
            }

            //接口拦截
            if (_interceptType.IsAssignableFrom(serviceType) && serviceType.GetTypeInfo().IsInterface)
            {
                registrationBuilder.EnableInterfaceInterceptors().InterceptedBy(typeof(DynamicProxyInterceptor));
            }
            else if (_interceptType.IsAssignableFrom(implementationType)) //虚方法拦截
            {
                registrationBuilder.EnableClassInterceptors().InterceptedBy(typeof(DynamicProxyInterceptor));
            }
            registrationBuilder.InjectProperties(implementationType);
            EnsureNeedUpdate();
            //builder.Update(Container);
            //builder.Build()
        }
示例#22
0
        public void Should_be_able_to_register_type(LifetimeType lifetime)
        {
            if (lifetime == LifetimeType.PerRequest)
            {
                container.Setup(c => c.RegisterType(It.IsAny <Type>(), It.IsAny <Type>(), It.IsAny <string>(), It.IsAny <PerRequestLifetimeManager>(), It.IsAny <InjectionMember[]>())).Verifiable();
            }
            else if (lifetime == LifetimeType.Singleton)
            {
                container.Setup(c => c.RegisterType(It.IsAny <Type>(), It.IsAny <Type>(), It.IsAny <string>(), It.IsAny <ContainerControlledLifetimeManager>(), It.IsAny <InjectionMember[]>())).Verifiable();
            }
            else if (lifetime == LifetimeType.Transient)
            {
                container.Setup(c => c.RegisterType(It.IsAny <Type>(), It.IsAny <Type>(), It.IsAny <string>(), It.IsAny <TransientLifetimeManager>(), It.IsAny <InjectionMember[]>())).Verifiable();
            }

            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            container.Verify();
        }
        /// <summary>
        /// Registers the specified concrete type for resolve with delegate for concrete implementation instance creation.
        /// </summary>
        /// <typeparam name="TService">Service type.</typeparam>
        /// <param name="instanceCreator">The instance creator.</param>
        /// <param name="lifetimeType">Lifetime type of the registering concrete type.</param>
        public void Register <TService>(Func <IDIContainerProvider, TService> instanceCreator,
                                        LifetimeType lifetimeType = LifetimeType.PerLifetimeScope)
            where TService : class
        {
            switch (lifetimeType)
            {
            case LifetimeType.Transient:
                Container.RegisterDelegate(c =>
                {
                    var provider = new DryIocDIProvider {
                        Container = (Container)c
                    };
                    return(instanceCreator(provider));
                },
                                           Reuse.Transient);

                break;

            case LifetimeType.Singleton:
                Container.RegisterDelegate(c =>
                {
                    var provider = new DryIocDIProvider {
                        Container = (Container)c
                    };
                    return(instanceCreator(provider));
                },
                                           Reuse.Singleton);
                break;

            case LifetimeType.PerLifetimeScope:
                Container.RegisterDelegate(c =>
                {
                    var provider = new DryIocDIProvider {
                        Container = (Container)c
                    };
                    return(instanceCreator(provider));
                },
                                           Reuse.InCurrentScope);
                break;
            }
        }
示例#24
0
        internal static ComponentRegistration <T> ApplyLifestyle <T>(this ComponentRegistration <T> registration,
                                                                     LifetimeType lifetimeType, bool classicWeb = false)
            where T : class
        {
            switch (lifetimeType)
            {
            case LifetimeType.Transient:
                return(registration.LifestyleTransient());

            //return registration.LifestyleCustom<MsScopedTransientLifestyleManager>();
            case LifetimeType.Singleton:
                return(registration.LifestyleSingleton());

            //return registration.LifestyleCustom<MsScopedLifestyleManager>();
            case LifetimeType.Scoped:
                //return classicWeb ? registration.LifestylePerWebRequest() : registration.LifestyleCustom<MyScopedSingletonLifestyleManager>(); //registration.LifestyleScoped();////
                return(classicWeb ? registration.LifestylePerWebRequest() : registration.LifestyleScoped());    ////

            default:
                return(registration);
            }
        }
        public void Should_be_able_to_register_type(LifetimeType lifetime)
        {
            container.Setup(c => c.Register(It.IsAny<IRegistration[]>())).Verifiable();

            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            container.Verify();
        }
示例#26
0
        /// <summary>
        /// Registers the type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            var key = MakeKey(serviceType, implementationType);
            LifestyleType lifestyle;
            switch (lifetime)
            {
                case LifetimeType.PerRequest:
                    lifestyle = LifestyleType.PerWebRequest;
                    break;
                case LifetimeType.Singleton:
                    lifestyle = LifestyleType.Singleton;
                    break;
                default:
                    lifestyle = LifestyleType.Transient;
                    break;
            }

            Container.Register(Component.For(serviceType).ImplementedBy(implementationType).Named(key).LifeStyle.Is(lifestyle));

            return this;
        }
示例#27
0
文件: IocRegistrar.cs 项目: d18zj/qim
 public void Replace(Type serviceType, Type implementationType, string name,
                     LifetimeType lifetime = LifetimeType.Transient, object constructorArgsAsAnonymousType = null)
 {
     throw new NotImplementedException();
 }
示例#28
0
文件: IocRegistrar.cs 项目: d18zj/qim
 public void Register <TService>(string name = null, LifetimeType lifetime = LifetimeType.Transient,
                                 object constructorArgsAsAnonymousType = null) where TService : class
 {
     RegisterType(typeof(TService), typeof(TService), name, lifetime, constructorArgsAsAnonymousType);
 }
示例#29
0
        public void Should_be_able_to_register_with_lifetime_type(LifetimeType lifetime)
        {
            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            IComponentRegistration registration;

            adapter.Container.ComponentRegistry.TryGetRegistration(new TypedService(typeof(DummyObject)), out registration);

            Assert.NotNull(registration);

            if (lifetime == LifetimeType.PerRequest)
            {
                Assert.Equal(registration.Sharing, InstanceSharing.Shared);
                Assert.IsType<MatchingScopeLifetime>(registration.Lifetime);
            }
            else if (lifetime == LifetimeType.Singleton)
            {
                Assert.Equal(registration.Sharing, InstanceSharing.Shared);
                Assert.IsType<RootScopeLifetime>(registration.Lifetime);
            }
            else
            {
                Assert.Equal(registration.Sharing, InstanceSharing.None);
                Assert.IsType<CurrentScopeLifetime>(registration.Lifetime);
            }
        }
示例#30
0
 /// <summary>
 /// Registers the service and its implementation with the lifetime behavior.
 /// </summary>
 /// <param name="serviceType">Type of the service.</param>
 /// <param name="implementationType">Type of the implementation.</param>
 /// <param name="lifetime">The lifetime of the service.</param>
 /// <returns></returns>
 public abstract IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime);
示例#31
0
        /// <summary>
        /// Registers the type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            IBindingWhenInNamedWithOrOnSyntax<object> expression = Kernel.Bind(serviceType).To(implementationType);

            switch (lifetime)
            {
                case LifetimeType.PerRequest:
                    expression.InRequestScope();
                    break;
                case LifetimeType.Singleton:
                    expression.InSingletonScope();
                    break;
                default:
                    expression.InTransientScope();
                    break;
            }

            return this;
        }
示例#32
0
 /// <summary>
 /// Registers the service and its implementation with the lifetime behavior.
 /// </summary>
 /// <param name="serviceType">Type of the service.</param>
 /// <param name="implementationType">Type of the implementation.</param>
 /// <param name="lifetime">The lifetime of the service.</param>
 /// <returns></returns>
 public abstract IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime);
示例#33
0
        public void Should_be_able_to_register_type(LifetimeType lifetime)
        {
            if (lifetime == LifetimeType.PerRequest)
            {
                container.Setup(c => c.RegisterType(It.IsAny<Type>(), It.IsAny<Type>(), It.IsAny<string>(), It.IsAny<PerRequestLifetimeManager>(), It.IsAny<InjectionMember[]>())).Verifiable();
            }
            else if (lifetime == LifetimeType.Singleton)
            {
                container.Setup(c => c.RegisterType(It.IsAny<Type>(), It.IsAny<Type>(), It.IsAny<string>(), It.IsAny<ContainerControlledLifetimeManager>(), It.IsAny<InjectionMember[]>())).Verifiable();
            }
            else if (lifetime == LifetimeType.Transient)
            {
                container.Setup(c => c.RegisterType(It.IsAny<Type>(), It.IsAny<Type>(), It.IsAny<string>(), It.IsAny<TransientLifetimeManager>(), It.IsAny<InjectionMember[]>())).Verifiable();
            }

            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            container.Verify();
        }
 public IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
 {
     return null;
 }
示例#35
0
        /// <summary>
        /// Registers the type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            LifetimeManager lifeTimeManager;
            switch (lifetime)
            {
                case LifetimeType.PerRequest:
                    lifeTimeManager = new PerRequestLifetimeManager();
                    break;
                case LifetimeType.Singleton:
                    lifeTimeManager = new ContainerControlledLifetimeManager();
                    break;
                default:
                    lifeTimeManager = new TransientLifetimeManager();
                    break;
            }

            if (Container.Registrations.Any(registration => registration.RegisteredType.Equals(serviceType)))
            {
                Container.RegisterType(serviceType, implementationType, implementationType.FullName, lifeTimeManager);
            }
            else
            {
                Container.RegisterType(serviceType, implementationType, lifeTimeManager);
            }

            return this;
        }
示例#36
0
        public void Should_be_able_to_register(LifetimeType lifetime)
        {
            var bindingName = new Mock<IBindingNamedWithOrOnSyntax>();

            var bindingWhen = new Mock<IBindingWhenInNamedWithOrOnSyntax>();

            if (lifetime == LifetimeType.Transient)
            {
                bindingWhen.Setup(b => b.InTransientScope()).Returns(bindingName.Object).Verifiable();
            }
            else if (lifetime == LifetimeType.Transient)
            {
                bindingWhen.Setup(b => b.InSingletonScope()).Returns(bindingName.Object).Verifiable();
            }
            else if (lifetime == LifetimeType.PerRequest)
            {
                var func = (Func<IContext, object>)Delegate.CreateDelegate(typeof(Func<IContext, object>), typeof(RequestScopeExtensionMethod).GetMethod("GetScope", BindingFlags.NonPublic | BindingFlags.Static));
                bindingWhen.Setup(b => b.InScope(func)).Returns(bindingName.Object).Verifiable();
            }

            var bindingTo = new Mock<IBindingToSyntax>();

            bindingTo.Setup(b => b.To(It.IsAny<Type>())).Returns(bindingWhen.Object);
            kernel.Setup(k => k.Bind(It.IsAny<Type>())).Returns(bindingTo.Object);

            adapter.RegisterType(typeof(object), typeof(object), lifetime);

            bindingWhen.Verify();
        }
示例#37
0
文件: IocRegistrar.cs 项目: d18zj/qim
 public void RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime,
                          object constructorArgsAsAnonymousType = null)
 {
     RegisterType(serviceType, implementationType, null, lifetime, constructorArgsAsAnonymousType);
 }
示例#38
0
        /// <summary>
        /// Registers the service and its implementation with the lifetime behavior.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            var builder = new ContainerBuilder();

            var registration = builder.RegisterType(implementationType).As(serviceType);

            switch (lifetime)
            {
                case LifetimeType.PerRequest:
                    registration.InstancePerHttpRequest();
                    break;
                case LifetimeType.Singleton:
                    registration.SingleInstance();
                    break;
                default:
                    registration.InstancePerDependency();
                    break;
            }

            builder.Update(Container.ComponentRegistry);

            return this;
        }
示例#39
0
文件: IocRegistrar.cs 项目: d18zj/qim
 public void RegisterType(Type implementationType, string name = null,
                          LifetimeType lifetime = LifetimeType.Transient, object constructorArgsAsAnonymousType = null)
 {
     RegisterType(implementationType, implementationType, name, lifetime, constructorArgsAsAnonymousType);
 }
        /// <summary>
        /// Registers the type.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            switch (lifetime)
            {
                case LifetimeType.PerRequest:
                    Container.Configure(x => x.For(serviceType).HttpContextScoped().Use(implementationType));
                    break;
                case LifetimeType.Singleton:
                    Container.Configure(x => x.For(serviceType).Singleton().Use(implementationType));
                    break;
                default:
                    Container.Configure(x => x.For(serviceType).Use(implementationType));
                    break;
            }

            return this;
        }
示例#41
0
        /// <summary>
        /// Registers the type.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="implementationType">Type of the implementation.</param>
        /// <param name="lifetime">The lifetime of the service.</param>
        /// <returns></returns>
        public override IServiceRegistrar RegisterType(string key, Type serviceType, Type implementationType, LifetimeType lifetime)
        {
            Invariant.IsNotNull(serviceType, "serviceType");
            Invariant.IsNotNull(implementationType, "implementationType");

            LifetimeManager lifeTimeManager = (lifetime == LifetimeType.PerRequest) ?
                                              new PerRequestLifetimeManager() :
                                              ((lifetime == LifetimeType.Singleton) ?
                                               new ContainerControlledLifetimeManager() :
                                               new TransientLifetimeManager() as LifetimeManager);

            if (string.IsNullOrEmpty(key))
            {
                if (Container.Registrations.Any(registration => registration.RegisteredType.Equals(serviceType)))
                {
                    Container.RegisterType(serviceType, implementationType, implementationType.FullName, lifeTimeManager);
                }
                else
                {
                    Container.RegisterType(serviceType, implementationType, lifeTimeManager);
                }
            }
            else
            {
                Container.RegisterType(serviceType, implementationType, key, lifeTimeManager);
            }

            return(this);
        }
        public void Should_be_able_to_register_type(LifetimeType lifetime)
        {
            var registry = (ConfigurationExpression)Activator.CreateInstance(typeof(ConfigurationExpression), true);

            container.Setup(c => c.Configure(It.IsAny<Action<ConfigurationExpression>>())).Callback((Action<ConfigurationExpression> x) => x(registry)).Verifiable();

            adapter.RegisterType(typeof(DummyObject), typeof(DummyObject), lifetime);

            container.Verify();
        }