private static void RegisterEventTypes(IWindsorContainer container, Type type, object instance = null) { Type[] interfaces = type.GetInterfaces(); foreach (Type i in interfaces.Where(x => x.IsGenericType)) { Type genericTypeDefinition = i.GetGenericTypeDefinition(); if (!typeof(IEventHandler <>).IsAssignableFrom(genericTypeDefinition)) { continue; } string genericArguments = string.Join( ", ", i.GetGenericArguments().Select(x => x.ToString())); ComponentRegistration <object> registration = Component.For(i) .Named($"{type.FullName}<{genericArguments}>"); if (instance != null) { registration.Instance(instance); } else { registration.ImplementedBy(type).LifestyleTransient(); } container.Register(registration); } }
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); }
/// <summary> /// Registers the <see cref="IHttpControllerSelector"/> as a /// <see cref="DefaultHttpControllerSelector"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterHttpControllerSelector( ComponentRegistration <IHttpControllerSelector> registration) { return(registration.ImplementedBy <DefaultHttpControllerSelector>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="IHttpControllerActivator"/> as a /// <see cref="WindsorHttpControllerActivator"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterHttpControllerActivator( ComponentRegistration <IHttpControllerActivator> registration) { return(registration.ImplementedBy <WindsorHttpControllerActivator>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="IContentNegotiator"/> as a /// <see cref="DefaultContentNegotiator"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterContentNegotiator( ComponentRegistration <IContentNegotiator> registration) { return(registration.ImplementedBy <DefaultContentNegotiator>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="ITraceWriter"/> as a <see cref="DefaultTraceWriter"/>. /// Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterTraceWriter( ComponentRegistration <ITraceWriter> registration) { return(registration.ImplementedBy <DefaultTraceWriter>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="ModelMetadataProvider"/> as a /// <see cref="DataAnnotationsModelMetadataProvider"/>. Override to specialize the /// registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterModelMetadataProvider( ComponentRegistration <ModelMetadataProvider> registration) { return(registration.ImplementedBy <DataAnnotationsModelMetadataProvider>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="IHttpActionInvoker"/> as a <see /// cref="ApiControllerActionInvoker"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterHttpActionInvoker( ComponentRegistration <IHttpActionInvoker> registration) { return(registration.ImplementedBy <ApiControllerActionInvoker>().LifestyleSingleton()); }
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); }
/// <summary> /// Registers the <see cref="IActionValueBinder"/> as a <see /// cref="DefaultActionValueBinder"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterActionValueBinder( ComponentRegistration <IActionValueBinder> registration) { return(registration.ImplementedBy <DefaultActionValueBinder>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="IAssembliesResolver"/> as a /// <see cref="DefaultAssembliesResolver"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterAssembliesResolver( ComponentRegistration <IAssembliesResolver> registration) { return(registration.ImplementedBy <DefaultAssembliesResolver>().LifestyleSingleton()); }
private static ComponentRegistration <TService> UsingImplementation <TService>(ComponentRegistration <TService> registration, Microsoft.Extensions.DependencyInjection.ServiceDescriptor service) where TService : class { return(registration.ImplementedBy(service.ImplementationType)); }
private static ComponentRegistration <object> ApplyResolver(ComponentRegistration <object> registration, StaticResolver resolver) { return(registration.ImplementedBy(resolver.Target)); }
/// <summary> /// Registers the <see cref="IHostBufferPolicySelector"/> as an /// <see cref="OwinBufferPolicySelector"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterHostBufferPolicySelector( ComponentRegistration <IHostBufferPolicySelector> registration) { return(registration.ImplementedBy <OwinBufferPolicySelector>().LifestyleSingleton()); }
/// <summary> /// Registers the <see cref="IExceptionHandler"/> with a /// <see cref="NullExceptionHandler"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterExceptionHandler(ComponentRegistration <IExceptionHandler> registration) { return(registration.ImplementedBy <NullExceptionHandler>().LifestyleSingleton()); }
private ComponentRegistration<object> InitializeCreationPolicy(IComponent component, ComponentRegistration componentFor) { if (component.Instance != null) { return componentFor.Instance(component.Instance); } if (component.Implementation != null) { return componentFor.ImplementedBy(component.Implementation); } if (component.Factory != null) { return componentFor.UsingFactoryMethod(a => component.Factory(this)); } throw new ContainerException("Implementation not allowed for interface " + component.Interface); }
private static ComponentRegistration<object> ApplyResolver(ComponentRegistration<object> registration, StaticResolver resolver) { return registration.ImplementedBy(resolver.Target); }
/// <summary> /// Registers the <see cref="IBodyModelValidator"/> as a <see /// cref="DefaultBodyModelValidator"/>. Override to specialize the registration. /// </summary> /// <param name="registration"></param> /// <returns></returns> protected virtual IRegistration RegisterBodyModelValidator( ComponentRegistration <IBodyModelValidator> registration) { return(registration.ImplementedBy <DefaultBodyModelValidator>().LifestyleSingleton()); }