Question

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.
Was it helpful?

Solution

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.

OTHER TIPS

You can add it if you add a reference to the built DLL, instead of the project.

Sounds like a design issue. As i'm not aware of the specific details in your implementation take the following as a guideline/idea.

When the event fires on B I'm assuming it creates some data. If you could store this data in a database or similar and then have perhaps a timed event in application A that essentially listens for this data and then provides to update with it as necessary.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top