ComponentRegistration<TService>
Registration for a single type as a component with the kernel.
You can create a new registration with the Component factory.
using Castle.Core;
using Castle.Core.Configuration;
using Castle.Core.Internal;
using Castle.DynamicProxy;
using Castle.MicroKernel.ComponentActivator;
using Castle.MicroKernel.Context;
using Castle.MicroKernel.LifecycleConcerns;
using Castle.MicroKernel.Proxy;
using Castle.MicroKernel.Registration.Interceptor;
using Castle.MicroKernel.Registration.Lifestyle;
using Castle.MicroKernel.Registration.Proxy;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Castle.MicroKernel.Registration
{
public class ComponentRegistration<TService> : IRegistration
{
private readonly ICollection<IRegistration> additionalRegistrations;
private readonly ICollection<ComponentDescriptor<TService>> descriptors;
private readonly ICollection<Type> forwardedTypes;
private ComponentModel componentModel;
private ComponentFilter ifFilter;
private Type implementation;
private string name;
private bool overwrite;
private bool registered;
private Type serviceType;
private ComponentFilter unlessFilter;
public Type[] ForwardedTypes => Enumerable.ToArray<Type>((IEnumerable<Type>)forwardedTypes);
public Type Implementation => implementation;
public LifestyleGroup<TService> LifeStyle => new LifestyleGroup<TService>(this);
public string Name => name;
public ProxyGroup<TService> Proxy => new ProxyGroup<TService>(this);
public Type ServiceType {
get {
return serviceType;
}
protected set {
serviceType = value;
}
}
internal bool IsOverWrite => overwrite;
public ComponentRegistration()
{
overwrite = false;
registered = false;
serviceType = typeof(TService);
forwardedTypes = new List<Type>();
descriptors = new List<ComponentDescriptor<TService>>();
additionalRegistrations = new List<IRegistration>();
}
protected ComponentRegistration(ComponentModel componentModel)
: this()
{
if (componentModel == null)
throw new ArgumentNullException("componentModel");
this.componentModel = componentModel;
name = componentModel.Name;
serviceType = componentModel.Service;
implementation = componentModel.Implementation;
}
[Obsolete("If you're using WCF Facility use AsWcfClient/AsWcfService extension methods instead.")]
public ComponentRegistration<TService> ActAs(params object[] actors)
{
foreach (object obj in actors) {
if (obj != null)
DependsOn(Property.ForKey(Guid.NewGuid().ToString()).Eq(obj));
}
return this;
}
public ComponentRegistration<TService> Activator<A>() where A : IComponentActivator
{
return this.AddAttributeDescriptor("componentActivatorType", typeof(A).AssemblyQualifiedName);
}
public ComponentRegistration<TService> AddAttributeDescriptor(string key, string value)
{
AddDescriptor(new AttributeDescriptor<TService>(key, value));
return this;
}
public ComponentRegistration<TService> AddDescriptor(ComponentDescriptor<TService> descriptor)
{
descriptor.Registration = this;
descriptors.Add(descriptor);
return this;
}
public AttributeKeyDescriptor<TService> Attribute(string key)
{
return new AttributeKeyDescriptor<TService>(this, key);
}
public ComponentRegistration<TService> Configuration(params Node[] configNodes)
{
return AddDescriptor(new ConfigurationDescriptor<TService>(configNodes));
}
public ComponentRegistration<TService> Configuration(IConfiguration configuration)
{
return AddDescriptor(new ConfigurationDescriptor<TService>(configuration));
}
[Obsolete("Obsolete, use DependsOn(Property[]) instead.", true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public ComponentRegistration<TService> CustomDependencies(params Property[] dependencies)
{
return DependsOn(dependencies);
}
[Obsolete("Obsolete, use DependsOn(IDictionary) instead.", true)]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public ComponentRegistration<TService> CustomDependencies(IDictionary dependencies)
{
return DependsOn(dependencies);
}
[EditorBrowsable(EditorBrowsableState.Advanced)]
[Obsolete("Obsolete, use DependsOn(object) instead.", true)]
public ComponentRegistration<TService> CustomDependencies(object dependencies)
{
return DependsOn(dependencies);
}
public ComponentRegistration<TService> DependsOn(params Property[] dependencies)
{
if (dependencies == null || dependencies.Length == 0)
return this;
ServiceOverride[] array = Enumerable.ToArray<ServiceOverride>(Enumerable.OfType<ServiceOverride>((IEnumerable)dependencies));
if (array.Length > 0) {
AddDescriptor(new ServiceOverrideDescriptor<TService>(array));
dependencies = Enumerable.ToArray<Property>(Enumerable.Except<Property>((IEnumerable<Property>)dependencies, (IEnumerable<Property>)array));
}
return AddDescriptor(new CustomDependencyDescriptor<TService>(dependencies));
}
public ComponentRegistration<TService> DependsOn(IDictionary dependencies)
{
return AddDescriptor(new CustomDependencyDescriptor<TService>(dependencies));
}
public ComponentRegistration<TService> DependsOn(object anonymous)
{
return AddDescriptor(new CustomDependencyDescriptor<TService>(anonymous));
}
public ComponentRegistration<TService> DynamicParameters(DynamicParametersDelegate resolve)
{
return DynamicParameters(delegate(IKernel k, CreationContext c, IDictionary d) {
resolve(k, d);
return null;
});
}
public ComponentRegistration<TService> DynamicParameters(DynamicParametersResolveDelegate resolve)
{
return DynamicParameters((IKernel k, CreationContext c, IDictionary d) => resolve(k, d));
}
public ComponentRegistration<TService> DynamicParameters(DynamicParametersWithContextResolveDelegate resolve)
{
AddDescriptor(new DynamicParametersDescriptor<TService>(resolve));
return this;
}
public ComponentRegistration<TService> ExtendedProperties(params Property[] properties)
{
return AddDescriptor(new ExtendedPropertiesDescriptor<TService>(properties));
}
public ComponentRegistration<TService> ExtendedProperties(object anonymous)
{
return AddDescriptor(new ExtendedPropertiesDescriptor<TService>(anonymous));
}
public ComponentRegistration<TService> Forward(params Type[] types)
{
return Forward((IEnumerable<Type>)types);
}
public ComponentRegistration<TService> Forward<TSecondService>()
{
return this.Forward(new Type[1] {
typeof(TSecondService)
});
}
public ComponentRegistration<TService> Forward<TSecondService, TThirdService>()
{
return this.Forward(new Type[2] {
typeof(TSecondService),
typeof(TThirdService)
});
}
public ComponentRegistration<TService> Forward<TSecondService, TThirdService, TFourthService>()
{
return this.Forward(new Type[3] {
typeof(TSecondService),
typeof(TThirdService),
typeof(TFourthService)
});
}
public ComponentRegistration<TService> Forward<TSecondService, TThirdService, TFourthService, TFifthService>()
{
return this.Forward(new Type[4] {
typeof(TSecondService),
typeof(TThirdService),
typeof(TFourthService),
typeof(TFifthService)
});
}
public ComponentRegistration<TService> Forward(IEnumerable<Type> types)
{
foreach (Type type in types) {
if (!forwardedTypes.Contains(type) && (object)type != serviceType)
forwardedTypes.Add(type);
}
return this;
}
public ComponentRegistration<TService> If(ComponentFilter ifFilter)
{
this.ifFilter = (ComponentFilter)Delegate.Combine(this.ifFilter, ifFilter);
return this;
}
public ComponentRegistration<TService> ImplementedBy<TImpl>() where TImpl : TService
{
return this.ImplementedBy(typeof(TImpl));
}
public ComponentRegistration<TService> ImplementedBy(Type type)
{
if ((object)implementation != null && (object)implementation != typeof(LateBoundComponent)) {
string message = $"""{implementation.FullName}";
throw new ComponentRegistrationException(message);
}
implementation = type;
return this;
}
public ComponentRegistration<TService> Instance(TService instance)
{
if (instance == null)
throw new ArgumentNullException("instance");
return ImplementedBy(instance.GetType()).Activator<ExternalInstanceActivator>().ExtendedProperties(Property.ForKey("instance").Eq(instance));
}
public InterceptorGroup<TService> Interceptors(params InterceptorReference[] interceptors)
{
return new InterceptorGroup<TService>(this, interceptors);
}
public ComponentRegistration<TService> Interceptors(params Type[] interceptors)
{
return AddDescriptor(new InterceptorDescriptor<TService>(Enumerable.ToArray<InterceptorReference>(Enumerable.Select<Type, InterceptorReference>((IEnumerable<Type>)interceptors, (Func<Type, InterceptorReference>)((Type t) => new InterceptorReference(t))))));
}
public ComponentRegistration<TService> Interceptors<TInterceptor>() where TInterceptor : IInterceptor
{
return this.AddDescriptor((ComponentDescriptor<TService>)new InterceptorDescriptor<TService>(new InterceptorReference[1] {
new InterceptorReference(typeof(TInterceptor))
}));
}
public ComponentRegistration<TService> Interceptors<TInterceptor1, TInterceptor2>() where TInterceptor1 : IInterceptor where TInterceptor2 : IInterceptor
{
return Interceptors<TInterceptor1>().Interceptors<TInterceptor2>();
}
public ComponentRegistration<TService> Interceptors(params string[] keys)
{
return AddDescriptor(new InterceptorDescriptor<TService>(Enumerable.ToArray<InterceptorReference>(Enumerable.Select<string, InterceptorReference>((IEnumerable<string>)keys, (Func<string, InterceptorReference>)InterceptorReference.ForKey))));
}
public ComponentRegistration<TService> Named(string name)
{
if (this.name != null) {
string message = $"""{this.name}""";
throw new ComponentRegistrationException(message);
}
this.name = name;
return this;
}
public ComponentRegistration<TService> OnCreate(params OnCreateActionDelegate<TService>[] actions)
{
AddDescriptor(new OnCreateComponentDescriptor<TService>(actions));
return this;
}
public ComponentRegistration<TService> OverWrite()
{
overwrite = true;
return this;
}
public ComponentRegistration<TService> Parameters(params Parameter[] parameters)
{
return AddDescriptor(new ParametersDescriptor<TService>(parameters));
}
public ComponentRegistration<TService> SelectInterceptorsWith(IInterceptorSelector selector)
{
return SelectInterceptorsWith((Action<ItemRegistration<IInterceptorSelector>>)delegate(ItemRegistration<IInterceptorSelector> s) {
s.Instance(selector);
});
}
public ComponentRegistration<TService> SelectInterceptorsWith(Action<ItemRegistration<IInterceptorSelector>> selector)
{
ItemRegistration<IInterceptorSelector> itemRegistration = new ItemRegistration<IInterceptorSelector>();
selector(itemRegistration);
return AddDescriptor(new InterceptorSelectorDescriptor<TService>(itemRegistration.Item));
}
public ComponentRegistration<TService> ServiceOverrides(params ServiceOverride[] overrides)
{
return AddDescriptor(new ServiceOverrideDescriptor<TService>(overrides));
}
public ComponentRegistration<TService> ServiceOverrides(IDictionary overrides)
{
return AddDescriptor(new ServiceOverrideDescriptor<TService>(overrides));
}
public ComponentRegistration<TService> ServiceOverrides(object anonymous)
{
return AddDescriptor(new ServiceOverrideDescriptor<TService>(anonymous));
}
public ComponentRegistration<TService> Unless(ComponentFilter unlessFilter)
{
this.unlessFilter = (ComponentFilter)Delegate.Combine(this.unlessFilter, unlessFilter);
return this;
}
public ComponentRegistration<TService> UsingFactory<U, V>(Converter<U, V> factory) where V : TService
{
return UsingFactoryMethod((IKernel kernel) => factory(kernel.Resolve<U>()));
}
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<TImpl> factoryMethod) where TImpl : TService
{
return UsingFactoryMethod((IKernel k, ComponentModel m, CreationContext c) => factoryMethod());
}
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Converter<IKernel, TImpl> factoryMethod) where TImpl : TService
{
return UsingFactoryMethod((IKernel k, ComponentModel m, CreationContext c) => factoryMethod(k));
}
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<IKernel, ComponentModel, CreationContext, TImpl> factoryMethod) where TImpl : TService
{
Activator<FactoryMethodActivator<TImpl>>().ExtendedProperties(new Property[1] {
Property.ForKey("factoryMethodDelegate").Eq(factoryMethod)
});
if ((object)this.implementation == null && !this.serviceType.IsSealed)
this.implementation = typeof(LateBoundComponent);
return this;
}
public ComponentRegistration<TService> UsingFactoryMethod<TImpl>(Func<IKernel, CreationContext, TImpl> factoryMethod) where TImpl : TService
{
return UsingFactoryMethod((IKernel k, ComponentModel m, CreationContext c) => factoryMethod(k, c));
}
internal void AddParameter(IKernel kernel, ComponentModel model, string key, string value)
{
IConfiguration val = EnsureParametersConfiguration(kernel);
MutableConfiguration item = new MutableConfiguration(key, value);
((List<IConfiguration>)val.get_Children()).Add(item);
model.Parameters.Add(key, value);
}
internal void AddParameter(IKernel kernel, ComponentModel model, string key, IConfiguration value)
{
IConfiguration val = EnsureParametersConfiguration(kernel);
MutableConfiguration val2 = new MutableConfiguration(key);
((List<IConfiguration>)val2.get_Children()).Add(value);
((List<IConfiguration>)val.get_Children()).Add(val2);
model.Parameters.Add(key, value);
}
private IConfiguration EnsureComponentConfiguration(IKernel kernel)
{
IConfiguration val = kernel.ConfigurationStore.GetComponentConfiguration(name);
if (val == null) {
val = new MutableConfiguration("component");
kernel.ConfigurationStore.AddComponentConfiguration(name, val);
}
return val;
}
private IConfiguration EnsureParametersConfiguration(IKernel kernel)
{
IConfiguration val = EnsureComponentConfiguration(kernel);
IConfiguration val2 = val.get_Children().get_Item("parameters");
if (val2 == null) {
val2 = new MutableConfiguration("parameters");
((List<IConfiguration>)val.get_Children()).Add(val2);
}
return val2;
}
private bool ExecuteIfCondition(IKernel kernel)
{
if (ifFilter == null)
return true;
Delegate[] invocationList = ifFilter.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++) {
ComponentFilter componentFilter = (ComponentFilter)invocationList[i];
if (!componentFilter(kernel, componentModel))
return false;
}
return true;
}
private bool ExecuteUnlessCondition(IKernel kernel)
{
if (unlessFilter == null)
return false;
Delegate[] invocationList = unlessFilter.GetInvocationList();
for (int i = 0; i < invocationList.Length; i++) {
ComponentFilter componentFilter = (ComponentFilter)invocationList[i];
if (componentFilter(kernel, componentModel))
return true;
}
return false;
}
private IKernelInternal GetInternalKernel(IKernel kernel)
{
IKernelInternal kernelInternal = kernel as IKernelInternal;
if (kernelInternal == null)
throw new ArgumentException($"""{typeof(IKernelInternal)}""", "kernel");
return kernelInternal;
}
private void InitializeDefaults()
{
if ((object)implementation == null)
implementation = serviceType;
if (string.IsNullOrEmpty(name)) {
if ((object)implementation == typeof(LateBoundComponent))
name = "Late bound " + serviceType.FullName;
else
name = implementation.FullName;
}
}
private bool IsMatch(IKernel kernel)
{
if (ExecuteIfCondition(kernel))
return !ExecuteUnlessCondition(kernel);
return false;
}
void IRegistration.Register(IKernel kernel)
{
if (!registered) {
registered = true;
InitializeDefaults();
IConfiguration configuration = EnsureComponentConfiguration(kernel);
foreach (ComponentDescriptor<TService> descriptor in descriptors) {
descriptor.ApplyToConfiguration(kernel, configuration);
}
if (componentModel == null)
componentModel = kernel.ComponentModelBuilder.BuildModel(name, serviceType, implementation, null);
foreach (ComponentDescriptor<TService> descriptor2 in descriptors) {
descriptor2.ApplyToModel(kernel, componentModel);
}
if (componentModel.Implementation.IsInterface && componentModel.Interceptors.Count > 0) {
ProxyOptions proxyOptions = ProxyUtil.ObtainProxyOptions(componentModel, true);
proxyOptions.OmitTarget = true;
}
if (IsMatch(kernel)) {
IKernelInternal internalKernel = GetInternalKernel(kernel);
internalKernel.AddCustomComponent(componentModel);
if (forwardedTypes.Count > 0) {
foreach (Type forwardedType in forwardedTypes) {
internalKernel.RegisterHandlerForwarding(forwardedType, name);
}
}
foreach (IRegistration additionalRegistration in additionalRegistrations) {
additionalRegistration.Register(kernel);
}
}
}
}
}
}