Пример #1
0
        public static ComponentRegistration <TService> Settings <TService>(this ComponentRegistration <TService> config, params Expression <Func <Settings, object> >[] usedSettings)
            where TService : class
        {
            return(config.DependsOn((k, d) =>
            {
                var settings = k.Resolve <Settings>();
                var serviceType = config.Implementation ?? typeof(TService);
                var ctorArguments = serviceType.GetConstructors().Single().GetParameters();

                foreach (var setting in usedSettings)
                {
                    var memberExpression = (MemberExpression)(setting.Body is UnaryExpression ? ((UnaryExpression)setting.Body).Operand : setting.Body);
                    var target = ctorArguments.Where(x => x.ParameterType == ((PropertyInfo)memberExpression.Member).PropertyType).ToArray();
                    string targetName;
                    if (target.Length == 1)
                    {
                        targetName = target[0].Name;
                    }
                    else
                    {
                        targetName = memberExpression.Member.Name;
                        targetName = targetName.Substring(0, 1).ToLowerInvariant() + targetName.Substring(1);
                    }

                    d[targetName] = setting.Compile().Invoke(settings);
                }
            }));
        }
        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));
        }
Пример #3
0
 public static ComponentRegistration <TFor> WithArgument <TFor>(
     this ComponentRegistration <TFor> component,
     string name,
     object value)
     where TFor : class
 {
     return(component.DependsOn(Dependency.OnValue(name, value)));
 }
Пример #4
0
        /// <summary>
        /// Extend the given component registration with service dependency information from attributes.
        /// </summary>

        private static ComponentRegistration <object> SetupNamedDependencies(ComponentRegistration <object> registration, Type c)
        {
            var attrs = c.GetCustomAttributes(typeof(DependsOn), true);
            var namedDependencyAttrs = attrs.Select(o => (DependsOn)o);

            namedDependencyAttrs.ForEach(d => registration = registration.DependsOn(ServiceOverride.ForKey(d.ServiceType).Eq(d.Name)));
            return(registration);
        }
Пример #5
0
 private static ComponentRegistration <TService> ActAs <TService>(this ComponentRegistration <TService> registration, params object[] actors) where TService : class
 {
     foreach (var actor in actors)
     {
         if (actor != null)
         {
             registration.DependsOn(Property.ForKey(Guid.NewGuid().ToString()).Eq(actor));
         }
     }
     return(registration);
 }
Пример #6
0
        private void ApplyHints <T>(ComponentRegistration <T> component, IEnumerable <Tuple <Type, string> > hints)
            where T : class
        {
            if (hints == null)
            {
                return;
            }

            foreach (var hint in hints)
            {
                component.DependsOn(Dependency.OnComponent(hint.Item1, hint.Item2));
            }
        }
Пример #7
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);
        }
Пример #8
0
        public static void DependsOnNamedAction(ComponentRegistration registration)
        {
            var implementationType = registration.Implementation;
            var constructor = implementationType.GetConstructors()
                .OrderByDescending(x=>x.GetParameters().Count()).FirstOrDefault();

            if(constructor==null)
                return;
            
            foreach (var parameterInfo in constructor.GetParameters())
            {
                var attribute = parameterInfo.GetCustomAttribute<DependsOnNamedAttribute>();
                if (attribute != null)
                    registration.DependsOn(Dependency.OnComponent(parameterInfo.ParameterType,
                        attribute.Name));
            }
        }
Пример #9
0
        public static void DependsOnNamedAction(ComponentRegistration registration)
        {
            var implementationType = registration.Implementation;
            var constructor        = implementationType.GetConstructors()
                                     .OrderByDescending(x => x.GetParameters().Count()).FirstOrDefault();

            if (constructor == null)
            {
                return;
            }

            foreach (var parameterInfo in constructor.GetParameters())
            {
                var attribute = parameterInfo.GetCustomAttribute <DependsOnNamedAttribute>();
                if (attribute != null)
                {
                    registration.DependsOn(Dependency.OnComponent(parameterInfo.ParameterType,
                                                                  attribute.Name));
                }
            }
        }
    	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);
        }
Пример #11
0
 void IDependencyVisitor.Value(ValueDependency dependency)
 {
     _registration.DependsOn(Dependency.OnValue(dependency.DependencyType, dependency.Value));
 }
Пример #12
0
 public static ComponentRegistration <TService> DependsOnComponent <TService>(this ComponentRegistration <TService> config, string name)
     where TService : class
 {
     return(config.DependsOn(Dependency.OnComponent(name, name)));
 }