问题描述
我的程序结构如下:
ConcreteCommandSender(instance 1)
尝试通过非托管资源发送内容,然后FailProofCommandSender
发生错误时尝试通过BackupPool
将命令传递给其他命令发送者。 BackupPool
知道所有可用的命令发送者,并选择与原始发送者不同的(备份者),并执行选择为ICommandSender(instance 2)
的选定ConcreteCommandSender(instance 2)
。另请参见下图。
问题是我不知道如何使用装饰器正确注册类型以使其正常工作。到目前为止,我遇到了以下问题:Autofac在ICommandSender
中创建了新的BackupPool
实例,而不是使用在容器配置中创建的实例(因此使用现有实例)。 IEnumerable<ICommandSender>
中的构造函数中有一个BackupPool
,但我也手动解析了第一个ICommandSender
。
奇怪的是,每次注册为装饰器的BackupPool
都被创建为新实例,然后我有了循环依赖关系,因为Autofac不想使用我的BackupPool
单个实例-BackupPool
具有上述所有命令发送者的列表。
问题是-如何正确管理所有与这些修饰符混合的命令发送者的列表。
**主要目标是:
- 具有多个命令发送者作为不可变列表
- 在发生故障时获取备份命令发送者**
我应该采取什么方法?也许完全不同?
interface ICommandSender
{
void Send(ICommand command);
}
class FailProofCommandSender : ICommandSender
{
FailProofCommandSender(BackupPool backupPool) { }
void Send(ICommand command)
{
// try to send command and catch in OnError
}
private OnError(ICommand command)
{
ICommandSender sender = backupPool.GetAnyExcept(this); // there is same context on ICommandSender per decorators family,so same sender is checked against this context
sender.Send(command);
}
}
class BackupPool
{
ICollection<ICommandSender> senders;
// BackupPool is created each time for every FailProofCommandSender,but why? It's registered as single :(
BackupPool(Func<ICommandSender> factory)
{
foreach(0,...,n) => senders.Add(factory());
}
ICommandSender GetAnyExcept(ICommandSender sender)
{
return this.senders
.Where(...) // where sender is not from same context
.Random();
}
}
builder.RegisterType<ConcreteCommandSender>().As<ICommandSender>();
builder.RegisterDecorator<FailProofCommandSender,ICommandSender>();
builder.RegisterType<BackupPool>()
.AsSelf()
.SingleInstance();
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)