Circular reference among two .net assemblies

前端 未结 5 1524
闹比i
闹比i 2021-01-18 02:28

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

相关标签:
5条回答
  • 2021-01-18 03:16

    If B now depends on bits of A perhaps you should refactor those bits out into a new assembly C which would be referenced by both A and B.

    0 讨论(0)
  • 2021-01-18 03:18

    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.

    0 讨论(0)
  • 2021-01-18 03:32

    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.

    0 讨论(0)
  • 2021-01-18 03:34

    For issue 1, there is not really a solution other then merge the two projects or do some code generation

    For the second, you can do that by implementing the Factory design pattern.

    0 讨论(0)
  • 2021-01-18 03:34

    Refactor your code or merge assemblies = don't use circular reference. It is symptom of very bad design.

    0 讨论(0)
提交回复
热议问题