DefaultHandler
Summary description for DefaultHandler.
using Castle.Core;
using Castle.MicroKernel.ComponentActivator;
using Castle.MicroKernel.Context;
using System.Text;
namespace Castle.MicroKernel.Handlers
{
public class DefaultHandler : AbstractHandler
{
private ILifestyleManager lifestyleManager;
protected ILifestyleManager LifestyleManager => lifestyleManager;
public DefaultHandler(ComponentModel model)
: base(model)
{
}
public override void Dispose()
{
lifestyleManager.Dispose();
}
public override bool ReleaseCore(Burden burden)
{
return lifestyleManager.Release(burden.Instance);
}
protected void AssertNotWaitingForDependency()
{
if (base.CurrentState == HandlerState.WaitingDependency)
throw UnresolvableHandlerException();
}
protected override void InitDependencies()
{
IComponentActivator componentActivator = base.Kernel.CreateComponentActivator(base.ComponentModel);
lifestyleManager = base.Kernel.CreateLifestyleManager(base.ComponentModel, componentActivator);
IDependencyAwareActivator dependencyAwareActivator = componentActivator as IDependencyAwareActivator;
if (dependencyAwareActivator != null && dependencyAwareActivator.CanProvideRequiredDependencies(base.ComponentModel)) {
foreach (DependencyModel dependency in base.ComponentModel.Dependencies) {
dependency.Init(base.ComponentModel.ParametersInternal);
}
} else
base.InitDependencies();
}
protected override object Resolve(CreationContext context, bool instanceRequired)
{
Burden burden;
return ResolveCore(context, false, instanceRequired, out burden);
}
protected object ResolveCore(CreationContext context, bool requiresDecommission, bool instanceRequired, out Burden burden)
{
if (!IsBeingResolvedInContext(context)) {
if (!CanResolvePendingDependencies(context)) {
if (!instanceRequired) {
burden = null;
return null;
}
AssertNotWaitingForDependency();
}
try {
using (CreationContext.ResolutionContext resolutionContext = context.EnterResolutionContext(this, requiresDecommission)) {
object result = lifestyleManager.Resolve(context, context.ReleasePolicy);
burden = resolutionContext.Burden;
return result;
}
} catch (NoResolvableConstructorFoundException) {
throw UnresolvableHandlerException();
}
}
IContextLifestyleManager contextLifestyleManager = lifestyleManager as IContextLifestyleManager;
if (contextLifestyleManager != null) {
object contextInstance = contextLifestyleManager.GetContextInstance(context);
if (contextInstance != null) {
burden = null;
return contextInstance;
}
}
if (!instanceRequired) {
burden = null;
return null;
}
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat("Dependency cycle has been detected when trying to resolve component '{0}'.", new object[1] {
base.ComponentModel.Name
});
stringBuilder.AppendLine();
stringBuilder.AppendLine("The resolution tree that resulted in the cycle is the following:");
context.BuildCycleMessageFor(this, stringBuilder);
throw new CircularDependencyException(stringBuilder.ToString(), base.ComponentModel);
}
private HandlerException UnresolvableHandlerException()
{
StringBuilder stringBuilder = new StringBuilder("Can't create component '");
stringBuilder.Append(base.ComponentModel.Name);
stringBuilder.AppendLine("' as it has dependencies to be satisfied.");
DependencyInspector dependencyInspector = new DependencyInspector(stringBuilder);
ObtainDependencyDetails(dependencyInspector);
return new HandlerException(dependencyInspector.Message, base.ComponentModel.ComponentName);
}
}
}