예제 #1
0
        private ComponentRegistration <T> GetLifeStyle <T>(ComponentRegistration <T> registration, uint index, string baseName)
            where T : class
        {
            Contract.Requires(index < 3,
                              "there are only three supported lifestyles; per transaction, per web request and transient");
            Contract.Ensures(Contract.Result <ComponentRegistration <T> >() != null);

            switch (defaultLifeStyle)
            {
            case DefaultSessionLifeStyleOption.SessionPerTransaction:
                if (index == 0)
                {
                    return(registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction());
                }
                if (index == 1)
                {
                    return(registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest);
                }
                if (index == 2)
                {
                    return(registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient);
                }
                break;

            case DefaultSessionLifeStyleOption.SessionPerWebRequest:
                if (index == 0)
                {
                    return(registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest);
                }
                if (index == 1)
                {
                    return(registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction());
                }
                if (index == 2)
                {
                    return(registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient);
                }
                break;

            case DefaultSessionLifeStyleOption.SessionTransient:
                if (index == 0)
                {
                    return(registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient);
                }
                if (index == 1)
                {
                    return(registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction());
                }
                if (index == 2)
                {
                    return(registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest);
                }
                break;

            default:
                throw new FacilityException("Unknown default life style - please file a bug report");
            }
            throw new FacilityException("Invalid index passed to GetLifeStyle<T> - please file a bug report");
        }
예제 #2
0
        public Component Register()
        {
            foreach (Type serviceType in _services)
            {
                if (_registration == null)
                {
                    _registration = new ComponentRegistration(serviceType);
                    _registration.Named(_name).ImplementedBy(_impl)
                    .DependsOn(_dependencies);

                    RegisterExtensions(_extensions);
                }
                else
                {
                    _registration.Forward(serviceType);
                }
            }

            if (_lifestyle.HasValue)
            {
                _registration.LifeStyle.Is(_lifestyle.Value);
            }

            kernel.Register(_registration);

            _dependencies.Clear();

            return(this);
        }
        protected override void Init()
        {
            ComponentRegistration <object> component = Component.For(typeof(IRepository <>)).ImplementedBy(typeof(NHRepository <>));

            if (!string.IsNullOrEmpty(config.RepositoryKey))
            {
                component.Named(config.RepositoryKey);
            }

            Kernel.Register(component);
            ComponentRegistration <IUnitOfWorkFactory> registerFactory =
                Component.For <IUnitOfWorkFactory>()
                .ImplementedBy <NHibernateUnitOfWorkFactory>();

            registerFactory.Parameters(ComponentParameter.ForKey("configurationFileName").Eq(config.NHibernateConfigurationFile));

            // if we are running in test mode, we don't want to register
            // the assemblies directly, we let the DatabaseTestFixtureBase do it
            // this allow us to share the configuration between the test & prod projects
            if (DatabaseTestFixtureBase.IsRunningInTestMode == false)
            {
                registerFactory.DependsOn(Property.ForKey("assemblies").Eq(Assemblies));
            }
            Kernel.Register(registerFactory);

            Kernel.AddComponentInstance("entitiesToRepositories", typeof(INHibernateInitializationAware),
                                        new EntitiesToRepositoriesInitializationAware(config.IsCandidateForRepository));
        }
예제 #4
0
        public static IRegisterLocator Include <TContract>(
            this ComponentRegistration <TContract> componentRegistration,
            WindsorDependencyContainer container,
            LifeStyle lifeStyle,
            string key)
            where TContract : class
        {
            switch (lifeStyle)
            {
            case LifeStyle.Transient:
                componentRegistration = componentRegistration.LifeStyle.Transient;
                break;

            case LifeStyle.Singleton:
                componentRegistration = componentRegistration.LifeStyle.Singleton;
                break;

            default:
                throw new ArgumentOutOfRangeException("lifeStyle");
            }

            if (key != null)
            {
                componentRegistration = componentRegistration.Named(key);
            }

            container.Container.Register(componentRegistration);

            return(container);
        }
예제 #5
0
        private object ConfigureRegistration(ComponentRegistration registration)
        {
            string typeName = GetTypeName(registration.Implementation);

            registration.Named(typeName);
            return(registration);
        }
        public static ComponentRegistration <TService> NamedAsParameter <TService>(this ComponentRegistration <TService> extended)
            where TService : class
        {
            var componentName = GetParameterName(extended.Implementation);
            var result        = extended.Named(componentName);

            return(result);
        }
예제 #7
0
        private static ComponentRegistration <object> ApplyName(ComponentRegistration <object> registration, string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                return(registration);
            }

            return(registration.Named(name));
        }
        public static ComponentRegistration <object> NamedIfNotEmpty(this ComponentRegistration <object> componentRegistration, string name)
        {
            if (!string.IsNullOrEmpty(name))
            {
                return(componentRegistration.Named(name));
            }

            return(componentRegistration);
        }
        public void RegisterComponent <T>(string componentName = null) where T : class
        {
            ComponentRegistration <T> componentRegistration = Component.For <T>().ImplementedBy <T>();

            if (componentName != null)
            {
                componentRegistration.Named(componentName);
            }

            _container.Register(componentRegistration);
        }
예제 #10
0
        private ComponentRegistration <T> WithName <T>(
            ComponentRegistration <T> componentRegistration,
            string name)
            where T : class
        {
            if (string.IsNullOrEmpty(name))
            {
                return(componentRegistration);
            }

            return(componentRegistration.Named(name));
        }
예제 #11
0
        public static IWindsorContainer ReplaceDefault <TServiceType>(this IWindsorContainer @this, ComponentRegistration <TServiceType> replacement) where TServiceType : class
        {
            var replacementName = Guid.NewGuid().ToString();

            @this.Register(replacement.Named(replacementName));

            @this.Kernel.AddHandlerSelector(
                new DefaultToKeyHandlerSelector(
                    type: typeof(TServiceType),
                    keyToDefaultTo: replacementName));

            return(@this);
        }
예제 #12
0
        public void Register(ServiceConfiguration service)
        {
            ComponentRegistration <object> component = Component.For(service.For);

            if (service.ToFactory != null)
            {
                ServiceConfiguration ctx = service;
                component = component.UsingFactoryMethod((k, cm, c) => ctx.ToFactory(new FactoryContext
                {
                    RequestedType = c.RequestedType,
                    Arguments     = c.HasAdditionalArguments ? c.AdditionalArguments.Values.Cast <object>().ToArray() : null
                }), true);
            }
            else
            {
                component = component.ImplementedBy(service.To);
            }

            component = component.ToWindsorLifeTime(service.Lifetime);

            if (!string.IsNullOrEmpty(service.Name))
            {
                component = component.Named(service.Name);
            }

            if (service.Dependencies.Any())
            {
                var allDependencies = service.Dependencies.Select(dependency => Dependency.OnComponent(dependency.Key, dependency.Value)).ToArray();
                component.DependsOn(allDependencies);
            }

            if (service.Interceptors.Any())
            {
                var interceptorName = string.Format("interceptor_{0}_{1}", service.For.First().Name, Guid.NewGuid().ToString().Replace("-", string.Empty));
                //Register interceptor itself by name
                container.Register(Component.For <RegistrationInterceptor>().Named(interceptorName)
                                   .DynamicParameters((k, c, p) =>
                {
                    p["context"] = c;
                    return(null);
                })
                                   .DependsOn(
                                       Dependency.OnValue("interceptions", service.Interceptors),
                                       Dependency.OnValue("container", container),
                                       Dependency.OnValue("context", null)
                                       ).LifestyleTransient()
                                   );
                component.Interceptors(interceptorName);
            }
            container.Register(component);
        }
예제 #13
0
        public static IWindsorContainer ReplaceComponent <TServiceType>(this IWindsorContainer @this, string componentName, ComponentRegistration <TServiceType> replacement, string replacementName = null) where TServiceType : class
        {
            replacementName = replacementName ?? Guid.NewGuid().ToString();
            if ([email protected](replacementName))
            {
                @this.Register(replacement.Named(replacementName));
            }

            @this.Kernel.AddHandlerSelector(
                new KeyReplacementHandlerSelector(
                    serviceType: typeof(TServiceType),
                    originalKey: componentName,
                    replacementKey: replacementName));

            return(@this);
        }
예제 #14
0
        public void Register()
        {
            _registration = new ComponentRegistration(_services.ToArray());
            _registration.Named(_name).ImplementedBy(_impl).DependsOn(_dependencies);

            RegisterExtensions(_extensions);

            if (_lifestyle.HasValue)
            {
                _registration.LifeStyle.Is(_lifestyle.Value);
            }

            kernel.Register(_registration);

            _dependencies.Clear();
        }
예제 #15
0
        /// <summary>
        /// Names a component registration for an <see cref="IDatabaseConnectionStringProvider"/> using
        /// the supplied enumeration value's name as the suffix on the key.
        /// </summary>
        /// <typeparam name="T">The service type being registered.</typeparam>
        /// <typeparam name="TEnum">The type of the <see cref="Enum"/> whose values represent the databases used by the application.</typeparam>
        /// <param name="registration">The <see cref="ComponentRegistration"/> instance.</param>
        /// <param name="database">The enumeration value representing the database to use for naming the registration.</param>
        /// <returns>The <see cref="ComponentRegistration"/> instance, for method chaining.</returns>
        public static ComponentRegistration <T> NamedForDatabase <T, TEnum>(this ComponentRegistration <T> registration, TEnum database)
            where T : class, IDatabaseConnectionStringProvider
        {
            // Only enumerations are supported
            if (!typeof(TEnum).IsEnum)
            {
                throw new ArgumentException("Generic type 'TEnum' must be an enumeration.");
            }

            string enumName = Enum.GetName(typeof(TEnum), database);

            string componentName = typeof(T).GetServiceNameWithSuffix(enumName);

            registration.Named(componentName);

            return(registration);
        }
        private void Configure <TService>(ComponentRegistration <TService> conf, string name, bool isDefault, bool isFallback, Action <ComponentRegistration <TService> > configuration)
            where TService : class
        {
            if (isDefault)
            {
                conf.IsDefault();
            }
            if (isFallback)
            {
                conf.IsFallback();
            }

            if (!string.IsNullOrWhiteSpace(name))
            {
                conf.Named(name);
            }

            configuration?.Invoke(conf);
        }
예제 #17
0
        /// <summary>
        /// Registers the transient.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <param name="name">The name.</param>
        /// <param name="isDefault">if set to <c>true</c> [is default].</param>
        /// <returns></returns>
        public override IIocManager RegisterTransient <TService>(string name = null, bool isDefault = false)
        {
            ComponentRegistration <TService> config = Component.For <TService>().LifestyleTransient();

            if (isDefault)
            {
                config.IsDefault();
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                Container.Register(config);
            }
            else
            {
                Container.Register(config.Named(name));
            }
            return(this);
        }
    	protected override void Init()
        {
    		ComponentRegistration<object> component =
				Component.For(typeof (IRepository<>)).ImplementedBy(typeof (ARRepository<>));
			if(!string.IsNullOrEmpty(repositoryKey))
			{
				component.Named(repositoryKey);
			}
    		Kernel.Register(component);
        	ComponentRegistration<IUnitOfWorkFactory> registerFactory = 
				Component.For<IUnitOfWorkFactory>()
        		.ImplementedBy<ActiveRecordUnitOfWorkFactory>();

			// if we are running in test mode, we don't want to register
			// the assemblies directly, we let the DatabaseTestFixtureBase do it
			// this allow us to share the configuration between the test & prod projects
			if(DatabaseTestFixtureBase.IsRunningInTestMode == false)
			{
				registerFactory.DependsOn(Property.ForKey("assemblies").Eq(assemblies));
			}
        	Kernel.Register(registerFactory);
        }
예제 #19
0
 public IComponent Named(string name)
 {
     component.Named(name);
     return(this);
 }
 public static ComponentRegistration <T> OverridesExistingRegistration <T>(this ComponentRegistration <T> componentRegistration) where T : class
 {
     return(componentRegistration
            .Named(Guid.NewGuid().ToString())
            .IsDefault());
 }
        private static ComponentRegistration<object> ApplyName(ComponentRegistration<object> registration, string name)
        {
            if (string.IsNullOrEmpty(name))
                return registration;

            return registration.Named(name);
        }
예제 #22
0
 protected virtual void ConfigureConsumer(ComponentRegistration registration)
 {
     registration.Named(registration.Implementation.Name);
 }
예제 #23
0
 protected virtual void ConfigureConsumer(ComponentRegistration registration)
 {
     registration.Named(registration.Implementation.FullName);
 }
예제 #24
0
        public Component Register()
        {
            foreach (Type serviceType in _services)
            {
                if (_registration == null)
                {
                    _registration = new ComponentRegistration(serviceType);
                    _registration.Named(_name).ImplementedBy(_impl)
                        .DependsOn(_dependencies);

                    RegisterExtensions(_extensions);
                }
                else
                {
                    _registration.Forward(serviceType);
                }
            }

            if (_lifestyle.HasValue)
            {
                _registration.LifeStyle.Is(_lifestyle.Value);
            }

            kernel.Register(_registration);

            _dependencies.Clear();

            return this;
        }
예제 #25
0
        public void Execute(ServiceContext context)
        {
            ComponentRegistration <object> component = Component.For(context.For);

            if (context.ToFactory != null)
            {
                ServiceContext ctx = context;
                component = component.UsingFactoryMethod((k, cm, c) => ctx.ToFactory(new FactoryContext
                {
                    RequestedType = c.RequestedType,
                    Arguments     = c.HasAdditionalArguments ? c.AdditionalArguments.Values.Cast <object>().ToArray() : null
                }), true);
            }
            else if (context.ToFactoryResolver != null)
            {
                ServiceContext ctx = context;
                component = component.UsingFactoryMethod((k, cm, c) => ctx.ToFactoryResolver(new FactoryContext
                {
                    RequestedType = c.RequestedType,
                    Arguments     = c.HasAdditionalArguments ? c.AdditionalArguments.Values.Cast <object>().ToArray() : null
                }, new WindsorResolver(k)), true);
            }
            else
            {
                component = component.ImplementedBy(context.To);
            }

            if (context.ComponentDependencies.Any())
            {
                component = context.ComponentDependencies.Aggregate(component, (current, componentDependency) => current.DependsOn(Dependency.OnComponent(componentDependency.Key, componentDependency.Value)));
            }

            if (context.ValueDependencies.Any())
            {
                component = context.ValueDependencies.Aggregate(component, (current, dependency) => current.DependsOn(Property.ForKey(dependency.Key).Eq(dependency.Value)));
            }
            var interceptors = new List <string>();

            if (context.Interceptors.Any())
            {
                var interceptorName = string.Format("interceptor_{0}_{1}", context.For.First().Name, Guid.NewGuid().ToString().Replace("-", string.Empty));
                //Register interceptor itself by name
                container.Register(Component.For <RegistrationInterceptor>().Named(interceptorName)
                                   .DynamicParameters((k, c, p) =>
                {
                    p["context"] = c;
                    return(null);
                })
                                   .DependsOn(
                                       Dependency.OnValue("interceptions", context.Interceptors),
                                       Dependency.OnValue("container", container),
                                       Dependency.OnValue("context", null)
                                       ).LifestyleTransient()
                                   );
                interceptors.Add(interceptorName);
            }

            if (context.InterceptorsByName.Any())
            {
                interceptors.AddRange(context.InterceptorsByName);
            }

            if (interceptors.Any())
            {
                component.Interceptors(interceptors.ToArray());
            }

            if (!string.IsNullOrEmpty(context.Name))
            {
                component = component.Named(context.Name);
            }

            component = component.ToWindsorLifeTime(context.Lifetime);

            container.Register(component);
        }
 private static void ConfigureComponent(ComponentRegistration componentRegistration)
 {
     componentRegistration.Named(componentRegistration.Implementation.FullName);
 }
예제 #27
0
        public void Register()
        {
            _registration = new ComponentRegistration(_services.ToArray());
            _registration.Named(_name).ImplementedBy(_impl).DependsOn(_dependencies);

            RegisterExtensions(_extensions);

            if (_lifestyle.HasValue)
            {
                _registration.LifeStyle.Is(_lifestyle.Value);
            }

            kernel.Register(_registration);

            _dependencies.Clear();
        }
 private object ConfigureRegistration(ComponentRegistration registration)
 {
     string typeName = GetTypeName(registration.Implementation);
     registration.Named(typeName);
     return registration;
 }