Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular reference among two .net assemblies

I have two assemblies A & B.

A has existing reference to B and it must be kept that way. Right now I made some changes to B that need to refer to A. So circular reference occurs.

Bit of details:

A has a few property grids that the dialog in B needs to be hosted. So to avoid this circular reference issue I tried to define interfaces to grids in third assembly to which A & B both refer, and make B only refers to the interfaces.

Two issues I'm facing:

  1. there’s too much custom data types (properties to be specific) inside the grids which are defined inside A and I have to define interfaces for every one of them.

  2. I see example of this works with function parameter e.g. call target function through the interface passed in. But how would it fit considering the following code - I can't new a ICustomPropertyGridWrapper...

    object = new CustomPropertyGridWrapper(...)
    m_property.SelectedObject = object;

like image 599
Lys Avatar asked Aug 17 '10 09:08

Lys


2 Answers

This is a problem with the language design of C#. In C/C++ you would just use a header to define the interface of the compilation unit and the dependency is resolved.

In C# there are no headers. You have three options

  1. 1> merge the assemblies (increase in compilation time and may not
    make sense if the assemblies are functionally unrelated). C# often forces you to do this, even if the assemblies should logically be separate.
  2. Dependency injection
  3. Creating a third assembly with interfaces which both the modules reference. This accomplishes the dependency injection through a C# language mechanism (interfaces), instead of roll your own; but its the same thing.

Number 3 is typically how these situations are handled in C# but its not as elegant as C/C++ solution to this problem. For large code bases you have to design from the start with this in mind.

like image 61
HaltingState Avatar answered Oct 06 '22 19:10

HaltingState


Sounds like you are attempting death by interface. Not everything has to be exposed by interface.

A simple answer is to either merge the assemblies, or move the common controls and data types to a third assembly. You only need to interface things if you want a consistent contractual way to access or work with things, and you want to hide the actual implementation.

like image 40
slugster Avatar answered Oct 06 '22 17:10

slugster