Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding this project will cause a circular dependency

I have a WPF application composed by two assemblies.

Assembly A is a wpf application.

Assembly B is a 3D app whit a public API mainly used by A.

Assembly A has a reference to assembly B.

Now when an event fires on assembly B I need to call a method in assembly A in a static class app.

If I try to add a reference to assembly A in B I get an error:

adding this project will cause a circular dependency

Do you have any ideas on how to solve it?

Specifically I would like to know:

  • How to call App.myMethod() in A from assembly B?
  • In order to decouple the classes, could I fire a generic event on assembly B and catch it on assembly A?
  • Any other ideas are welcome.
like image 628
GibboK Avatar asked Dec 19 '22 17:12

GibboK


1 Answers

How do I call App.myMethod in A from assembly B?

You can't if A depends on B.

If the event is fired from some class in B you should subscribe to that event from some class in A:

code in A:

var classB = new ClassB();  // presumably in your code in A somewhere
classB.MyEvent += new EventHandler(this.HandleEventFromB);


public void HandleEventFromB(object sneder, EventArgs args)
{
    ClassA.StaticMethod();
}

That way B is not dependent on A.

You may need to "bubble up" the event within B to some publicly exposed event in order to subscribe to it from A.

like image 99
D Stanley Avatar answered Jan 08 '23 19:01

D Stanley