Component
Factory for creating ComponentRegistration objects. Use static methods on the class to fluently build registration.
using Castle.Core;
using System;
using System.Collections.Generic;
namespace Castle.MicroKernel.Registration
{
public static class Component
{
public static ComponentRegistration For(Type serviceType)
{
if (serviceType == (Type)null)
throw new ArgumentNullException("serviceType", "The argument was null. Check that the assembly is referenced and the type available to your application.");
return new ComponentRegistration(serviceType);
}
public static ComponentRegistration For(params Type[] serviceTypes)
{
if (serviceTypes.Length == 0)
throw new ArgumentException("At least one service type must be supplied");
return new ComponentRegistration(serviceTypes);
}
public static ComponentRegistration For(IEnumerable<Type> serviceTypes)
{
ComponentRegistration componentRegistration = null;
foreach (Type serviceType in serviceTypes) {
if (componentRegistration == null)
componentRegistration = For(serviceType);
else
componentRegistration.Forward(serviceType);
}
if (componentRegistration == null)
throw new ArgumentException("At least one service type must be supplied");
return componentRegistration;
}
public static ComponentRegistration<TService> For<TService>() where TService : class
{
return new ComponentRegistration<TService>();
}
public static ComponentRegistration<TService1> For<TService1, TService2>() where TService1 : class
{
return new ComponentRegistration<TService1>().Forward<TService2>();
}
public static ComponentRegistration<TService1> For<TService1, TService2, TService3>() where TService1 : class
{
return new ComponentRegistration<TService1>().Forward<TService2, TService3>();
}
public static ComponentRegistration<TService1> For<TService1, TService2, TService3, TService4>() where TService1 : class
{
return new ComponentRegistration<TService1>().Forward<TService2, TService3, TService4>();
}
public static ComponentRegistration<TService1> For<TService1, TService2, TService3, TService4, TService5>() where TService1 : class
{
return new ComponentRegistration<TService1>().Forward<TService2, TService3, TService4, TService5>();
}
public static bool HasAttribute<TAttribute>(Type type) where TAttribute : Attribute
{
return Attribute.IsDefined(type, typeof(TAttribute));
}
public static Predicate<Type> HasAttribute<TAttribute>(Predicate<TAttribute> filter) where TAttribute : Attribute
{
return delegate(Type type) {
if (Component.HasAttribute<TAttribute>(type))
return filter((TAttribute)Attribute.GetCustomAttribute(type, typeof(TAttribute)));
return false;
};
}
public static bool IsCastleComponent(Type type)
{
return HasAttribute<CastleComponentAttribute>(type);
}
public static Predicate<Type> IsInNamespace(string namespace)
{
return IsInNamespace(namespace, false);
}
public static Predicate<Type> IsInNamespace(string namespace, bool includeSubnamespaces)
{
if (includeSubnamespaces)
return delegate(Type type) {
if (!(type.Namespace == namespace))
return type.Namespace.StartsWith(namespace + ".");
return true;
};
return (Type type) => type.Namespace == namespace;
}
public static Predicate<Type> IsInSameNamespaceAs(Type type)
{
return IsInNamespace(type.Namespace);
}
public static Predicate<Type> IsInSameNamespaceAs(Type type, bool includeSubnamespaces)
{
return IsInNamespace(type.Namespace, includeSubnamespaces);
}
public static Predicate<Type> IsInSameNamespaceAs<T>() where T : class
{
return IsInSameNamespaceAs(typeof(T));
}
public static Predicate<Type> IsInSameNamespaceAs<T>(bool includeSubnamespaces) where T : class
{
return IsInSameNamespaceAs(typeof(T), includeSubnamespaces);
}
}
}