为了账号安全,请及时绑定邮箱和手机立即绑定

温莎-从容器中提取瞬变对象

温莎-从容器中提取瞬变对象

神不在的星期二 2019-07-05 10:36:28
温莎-从容器中提取瞬变对象我如何从容器中拉出本质上是短暂的物体?我是否必须将它们注册到容器中并插入所需类的构造函数中?将所有内容都注入构造函数并不好。也只是为了一个类,我不想创建一个TypedFactory并将工厂注入需要类。我想到的另一个想法是“新的”他们是根据需要来的。但我也在注射Logger将组件(通过属性)添加到我的所有类中。因此,如果我更新了它们,就必须手动实例化Logger在那些课上。如何继续为我的所有类使用容器?记录器注入:我的大多数班级都有Logger属性定义,除非存在继承链(在这种情况下,只有基类具有此属性,所有派生类都使用该属性)。当它们通过温莎容器实例化时,它们将得到我的ILogger注射到他们体内。//Install QueueMonitor as SingletonContainer.Register(Component.For<QueueMonitor>().LifestyleSingleton()); //Install DataProcessor as TrnsientContainer.Register(Component.For<DataProcessor>().LifestyleTransient()); Container.Register(Component.For<Data>().LifestyleScoped());public class QueueMonitor{     private dataProcessor;     public ILogger Logger { get; set; }     public void OnDataReceived(Data data)     {         //pull the dataProcessor from factory             dataProcessor.ProcessData(data);     }}public class DataProcessor{     public ILogger Logger { get; set; }     public Record[] ProcessData(Data data)     {         //Data can have multiple Records         //Loop through the data and create new set of Records         //Is this the correct way to create new records?         //How do I use container here and avoid "new"          Record record = new Record(/*using the data */);         ...         //return a list of Records         }}public class Record{     public ILogger Logger { get; set; }     private _recordNumber;     private _recordOwner;     public string GetDescription()     {         Logger.LogDebug("log something");         // return the custom description     }}问题:如何创建新的Record对象而不使用“new”?QueueMonitor是Singleton,鉴于Data是“范围”。我怎么注射Data进OnDataReceived()方法?
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

我同意您在这里所说的一切,我也不同意仅仅为了记录和重新抛出而捕获异常,因为我认为应用程序域中应该有一个中心点来执行此操作,但是我确实觉得有时候您需要捕获一个特定的异常,比如SQLException来重试该操作。

查看完整回答
反对 回复 2019-07-05
  • 3 回答
  • 0 关注
  • 193 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信