FromDescriptor
Describes the source of types to register.
using System;
using System.Collections.Generic;
namespace Castle.MicroKernel.Registration
{
public abstract class FromDescriptor : IRegistration
{
private bool allowMultipleMatches;
private readonly IList<BasedOnDescriptor> criterias;
internal FromDescriptor()
{
allowMultipleMatches = false;
criterias = new List<BasedOnDescriptor>();
}
public FromDescriptor AllowMultipleMatches()
{
allowMultipleMatches = true;
return this;
}
public BasedOnDescriptor BasedOn<T>()
{
return BasedOn(typeof(T));
}
public BasedOnDescriptor BasedOn(Type basedOn)
{
BasedOnDescriptor basedOnDescriptor = new BasedOnDescriptor(basedOn, this);
criterias.Add(basedOnDescriptor);
return basedOnDescriptor;
}
public BasedOnDescriptor Pick()
{
return BasedOn<object>();
}
public BasedOnDescriptor Where(Predicate<Type> accepted)
{
BasedOnDescriptor basedOnDescriptor = new BasedOnDescriptor(typeof(object), this).If(accepted);
criterias.Add(basedOnDescriptor);
return basedOnDescriptor;
}
void IRegistration.Register(IKernel kernel)
{
if (criterias.Count != 0) {
foreach (Type item in SelectedTypes(kernel)) {
foreach (BasedOnDescriptor criteria in criterias) {
if (criteria.TryRegister(item, kernel) && !allowMultipleMatches)
break;
}
}
}
}
protected abstract IEnumerable<Type> SelectedTypes(IKernel kernel);
}
}