コード例 #1
0
ファイル: WindsorRegistrator.cs プロジェクト: shuk/Cortoxa
        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);
        }
コード例 #2
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);
        }