Skip to main content
 首页 » 编程设计

c#之使用 IoC 链接层,将下层回调设置为上层并避免循环引用

2024年02月20日12yxwkf

我有一个场景,我需要一个较低层由上层控制,就像木偶大师拉动弦乐一样。

下层也会时不时的产生一些内部事件,回调上层。

我正在使用 SimpleInjector,我将 ILower 注入(inject)到 Upper 构造函数中。我不能将 Upper 注入(inject)到 Lower,因为它会导致循环引用。

相反,我有一个注册回调函数来链接这两个层。但是,我必须使用空检查来分散我的代码。

有没有更好的方法或不同的架构来实现这种对象链接?

// an interface that transport can callback from transport to client 
public interface ILowerToUpperCallback 
{ 
    void ReplyA(); 
    void ReplyB(); 
} 
 
// transport interface that client calls 
public interface ILower 
{ 
    void Test1(); 
    void Test2(); 
    void RegisterCallback(ILowerToUpperCallback callback); 
} 
 
public class Upper : ILowerToUpperCallback 
{ 
    private readonly ILower lower; 
 
    public Upper(ILower lower) 
    { 
        this.lower = lower; 
        this.lower.RegisterCallback(this); 
    } 
 
    void ReplyA() 
    { 
    } 
 
    void ReplyB() 
    { 
    } 
} 
 
public class Lower : ILower 
{ 
    private ILowerToUpperCallback callback; 
 
    /* this is not possible, would cause a circular reference 
    public Lower(ILowerToUpperCallback callback) 
    { 
        this.callback = callback; 
    } 
    */ 
 
    // set by different method instead, what happens if this is never set?! 
    void RegisterCallback(ILowerToUpperCallback callback) 
    { 
        this.callback = callback; 
    } 
 
    void OnTimer() 
    { 
        // some timer function 
 
        if(this.callback != null) // these null checks are everywhere :( 
            this.callback.ReplyA(); 
    } 
} 

请您参考如下方法:

我认为您的设计没有任何问题,尽管您已经注意到它不是最容易配置的东西。问题不在于您的 DI 框架的限制,而在于您必须执行的心理体操。

这是一个想法。将您的类(class)更改为以下内容:

public class Upper : IUpper, ILowerToUpperCallback 
{ 
   public Upper(/* all depedencies except ILower */) { } 
 
    // Promote ILower to property dependency 
    public ILower Lower { get; set; } 
} 
 
public class Lower : ILower 
{ 
    // Use the Null Object Pattern for default implementation to prevent 
    // null checks. 
    private ILowerToUpperCallback callback = new NullCallback(); 
 
    public Upper(/* all dependencies except ILowerToUpperCallback */) 
    { 
        this.callback = callback; 
    } 
 
    // Allow overriding the default implementation using a method, just 
    // as you are already did. 
    public SetCallback(ILowerToUpperCallback callback) 
    { 
        if (callback == null) throw new ArgumentNullException("callback"); 
        this.callback = callback; 
    } 
} 

使用此设计,您可以按如下方式连接所有内容:

container.Register<ILower, Lower>(); 
container.Register<IUpper, Upper>(); 
 
container.RegisterInitializer<Upper>(upper => 
{ 
    var lower = (Lower)container.GetInstance<ILower>(); 
    lower.SetCallback(upper); 
    upper.Lower = lower; 
}); 

由于 Lower 和 Upper 是普通服务,您可以照常解析它们。通过为 Upper 注册一个初始化委托(delegate),您可以在容器创建 Upper 之后进行一些额外的初始化。此初始化程序将 Lower 和 Upper 连接在一起。由于 Composition Root 知道 ILower 和 Lower,我们可以安全地从 ILower 转换为 Lower,而不会破坏任何规则。这种设计的最佳之处在于 ILower 接口(interface)保持干净,并且忽略了 ILowerToUpperCallback,这实际上是一个实现细节。