Castle Windsor:在任何对象解决后通过容器获取通知

时间:2017-06-29 12:10:21

标签: castle-windsor

我使用Castle Windsor作为DI Container并有以下问题:

每次都可以通过容器获取通知,容器是否创建了任何对象并获取对该对象的引用?

我想在每个解析后检查,如果解析的对象实现了一个特殊的接口(例如IEmergencyStop)。我想在特殊服务(EmergencyStopHelper)上注册此对象。

举个例子:

interface IEmergencyStop
{
   void Stop();
}

interface IMotor : IEmergencyStop
{
   void Run();
}

class Motor : IMotor
{
}

class EmergencyStopHelper
{
   List<IEmergencyStop> emergencyStopList = new List<IEmergencyStop>();

   public void Register(IEmergencyStop aClass)
   { 
      emergencyStopList.Add(aClass); 
   }

   public void StopAll() => emergencyStopList.ForEach( x => x.Stop());
}


container.Register(Component.For<IMotor>().ImplementedBy<Motor>().LifestlyleTransient());
container.Register(Component.For<EmergencyStopHelper>());

// TODO: any magic code which calls EmergencyStopHelper.Register(...)
// after following resolve

IMotor aMotor = container.Resolve<IMotor>();

var emergencyStop = container.Resolve<EmergencyStopHelper>();
emergencyStop.StopAll();

0 个答案:

没有答案