locked
circular dependency RRS feed

  • Question

  • Hi,

    I have a circular dependency problem (I read that it is mostly due to bad design.) and hoped someone can give me some advice. I have a application that has 2 way async communication to multiple systems. I abstracted that in a Broker project. All communication goes trough the broker and the broker parses the messages to an internal format. Further more there is an engine. The engine is the core of the application. It receives and sends messages. All these messages go trough the broker. My sollution was to create a broker and set a reverence to the engine and secondly create the engine and set a reference to the broker. (when a message is received the broker should give it to the engine and when the engine needs to deliver a message it should give it to the broker.) What am I doing wrong?

    Patrick

    Friday, June 11, 2010 1:58 PM

Answers

  • So the dependency occurs between the broker and the engine.  There are many solutions to this problem.  Choose the one that works best for you.

    1) Interfaces - Create an interface for one (or both) of the components.  The interfaces reside in a separate, common assembly.  The engine/broker then uses the interface to communicate with the other side.  This removes the dependency on the actual implementation. 

    //Common assembly
    public interface IEngine { }
    public interface IBroker { }

    //Engine assembly
    public class Engine : IEngine
    {
       public Engine ( IBroker broker ) { }
    }

    //Broker assembly
    public class Broker : IBroker
    {
       public void DoWork ( )
       {
          IEngine eng = new Engine(this);
          ...      
       }
    }

    2) Eventing - Rather than having the engine/broker handshake each other expose events from the "master" object (probably the engine) that the other object subscribes to.  The dependency is then one way (from the broker to the engine). 

    public class Engine
    {
       public event EventHandler SomethingImportant;
       public event EventHandler SomethingImportant2;
    }

    public class Broker
    {
       public void DoWork ( )
       {
          Engine eng = new Engine();
          eng.SomethingImportant += OnSomethingImportant; 
       }
    }

    3) SOA - As an extension a service-oriented architecture that uses interfaces to completely decouple the engine/broker is even better but more costly.  Unity from the Enterprise Library is a good start.

    4) WCF - Sort of like SOA but a little better integrated at the cost of more expensive.

    Michael Taylor - 6/11/2010
    http://msmvps.com/blogs/p3net

     

    Friday, June 11, 2010 2:10 PM
    Moderator

All replies

  • So the dependency occurs between the broker and the engine.  There are many solutions to this problem.  Choose the one that works best for you.

    1) Interfaces - Create an interface for one (or both) of the components.  The interfaces reside in a separate, common assembly.  The engine/broker then uses the interface to communicate with the other side.  This removes the dependency on the actual implementation. 

    //Common assembly
    public interface IEngine { }
    public interface IBroker { }

    //Engine assembly
    public class Engine : IEngine
    {
       public Engine ( IBroker broker ) { }
    }

    //Broker assembly
    public class Broker : IBroker
    {
       public void DoWork ( )
       {
          IEngine eng = new Engine(this);
          ...      
       }
    }

    2) Eventing - Rather than having the engine/broker handshake each other expose events from the "master" object (probably the engine) that the other object subscribes to.  The dependency is then one way (from the broker to the engine). 

    public class Engine
    {
       public event EventHandler SomethingImportant;
       public event EventHandler SomethingImportant2;
    }

    public class Broker
    {
       public void DoWork ( )
       {
          Engine eng = new Engine();
          eng.SomethingImportant += OnSomethingImportant; 
       }
    }

    3) SOA - As an extension a service-oriented architecture that uses interfaces to completely decouple the engine/broker is even better but more costly.  Unity from the Enterprise Library is a good start.

    4) WCF - Sort of like SOA but a little better integrated at the cost of more expensive.

    Michael Taylor - 6/11/2010
    http://msmvps.com/blogs/p3net

     

    Friday, June 11, 2010 2:10 PM
    Moderator
  • Thanks for your quick response. Option 1 looks like the sollution to the problem. But ill have a look at sollution 2 to see if its maybe a better way.
    Friday, June 11, 2010 2:15 PM
  • If the circular dependency occur for adding reference then you can add .dll to the reference.

    Hope it will work.



    • Proposed as answer by azamcse Tuesday, March 29, 2011 7:07 AM
    Tuesday, March 29, 2011 7:04 AM