使用c#在中介模式中的循环依赖

Gee*_*ekM 6 c# design-patterns mediator

我有一个关于我想在我的应用程序中实现的中介模式的问题(使用C#).在我的代码中实现模式时,我遇到了循环依赖.类的结构如下:

MediatorColleague组件/类在不同的程序集中,并且作为中介模式需要两个组件(类)相互使用.在引用彼此时出现问题.

请考虑以下代码:

namespace Mediator
{
   public abstract class IMediator
   {
      public IColleague colleague{get;set;}
      void Register();
      void Send();           
   }
   public class MediatorA:IMediator
   {        
     void Register(){//code here}
     void Send(){//code here}       
   }
 }
Run Code Online (Sandbox Code Playgroud)
namespace Colleague
{

    public abstract class IColleague
    {
        IMediator mediator;
        void Send();
        void Recieve();       

    }
    public class ColleagueA:IColleague
    {

        void Send(){//code here}
        void Recieve(){//code here}       

    }
}
Run Code Online (Sandbox Code Playgroud)

由于Mediater和同事在不同的命名空间和程序集中,如何解决循环依赖?

Seb*_*Seb 5

您需要定义将包含接口的第三个程序集。恕我直言,没有其他办法。