Abstract composition root in a reusable library

In my first question

Find the correct root directory for the .NET library

helped clarify the issue of dependency injection. Stephen's comprehensive answer says (in a few words, if possible) that a reusable library is a special case, and here we can use poor-man-injection (see answer for completeness).

As you study AutoFixture source code, you may notice that the main assembly uses DI everywhere, regardless of the container; which is well described in Mark Seemann .

Question: can someone introduce a template (or good practice) to abstract the root of the composition without using a container?

Additional notes (optional):

Backup in AutoFixture, where the root of the composition is found in designers of the Fixture type; I think the *Relays class is used to group instances in a similar way, some DI libraries have a module concept. It is interesting to note that there is a type called BehaviourRoot , which is located at the top of the graph (is this the kind of abstraction I'm looking for?).

+4
source share
1 answer

A description of the root composition template description explicitly indicates that it can be implemented without a DI container. The template is independent of container use and can be used with or without a container.

The root of the composition is the entry point of the application. By definition, he has no callers , so there is no point in abstracting him.

To quote the above article:

Composition root is a component of the application infrastructure.

Only applications should have composition roots. Libraries and frameworks should not .

(my bold accent)

AutoFixture is a library and therefore does not have a composition root. Fixture FaΓ§ade class, in the spirit of building a DI-friendly library .

+9
source

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


All Articles