Burden
using Castle.Core;
using System;
using System.Collections.Generic;
namespace Castle.MicroKernel
{
public class Burden
{
private object instance;
private IHandler handler;
private bool instanceRequiresDecommission;
private bool childRequiresDecommission;
private readonly List<Burden> children = new List<Burden>();
public bool GraphRequiresDecommission {
get {
if (!instanceRequiresDecommission)
return childRequiresDecommission;
return true;
}
}
public bool HasChildren => children.Count != 0;
public ComponentModel Model => handler.ComponentModel;
public void SetRootInstance(object instance, IHandler handler, bool hasDecomission)
{
if (instance == null)
throw new ArgumentNullException("instance");
if (handler == null)
throw new ArgumentNullException("handler");
this.instance = instance;
this.handler = handler;
instanceRequiresDecommission = hasDecomission;
}
public void AddChild(Burden child)
{
children.Add(child);
if (child.GraphRequiresDecommission)
childRequiresDecommission = true;
}
public bool Release(IReleasePolicy policy)
{
if (policy == null)
throw new ArgumentNullException("policy");
if (!handler.Release(instance))
return false;
foreach (Burden child in children) {
policy.Release(child.instance);
}
return true;
}
}
}