Dependency Injection and Circular Link

I am just starting out with DI and unit testing and have fallen into a trap that, I am sure, has no problems for those more experienced developers:

I have a MessageManager class that receives data and stores it in db. Within the same assembly (project in Visual Studio) I created a repository interface with all the methods needed to access db. The specific implementation of this interface is in a separate assembly called DataAccess.

Therefore, DataAccess needs a reference to the MessageManager project to find out about the repository interface. And MessageManager needs a reference to the DataAccess project so that the MessageManager client can implement a specific implementation of the repository interface. This is prohibited by the course.

I could move the interface to a data access assembly, but I believe that the repository interface should be in the same assembly as the client that uses it.

So what have I done wrong?

+3
source share
7 answers

Do you use inverse of the control container? If so, the answer is simple.

Assembly A contains:

  • Messagemanager
  • IRepository
  • ContainerA (add MessageManager)

Assembly B contains (and ref AssemblyA):

  • Repository implements IRepository
  • ContainerB extends ContainerA (add repository)

Build C (or B) will launch the application / ask for a container for MessageManager, which will know how to enable MessageManager and IRepository.

+2
source

. .

- , , , , DataAccess. , MessageManager - .

, .

+3

: DataAccess. , DataAccess - ( ) , DataAccess.

+2

, DataAccess. DataAccess MessageManager.

, , ...

+1

, .

:

Foo f = new Foo();
Bar b = new Bar();
f.setBar(b);
b.setFoo(f);
+1

, ( MessageManager DataAccess, ), MessageManager , , System.Reflection.Assembly.

0

:

. . . .

An abstraction that depends on the classes in the DatAccess assembly must be in a separate assembly from the DataAccess classes and the specific implementation of this abstraction (MessageManager).

Yes, these are more assemblies. For me personally, this is not a big deal. I do not see a big drawback in additional builds.

0
source

Source: https://habr.com/ru/post/1697098/


All Articles