Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract the composition root in a reusable library

In my first question

Locate the correct composition root for a .NET library

helped clarifying about Dependency Injection. The exhaustive answer of Steven states (in few words if it possible) that a reusable library is a particular a case and here we can use poor-man-injection (see the answer for completeness).

Studying the source code of AutoFixture, you can note that the main assembly use DI everywhere without depending on a container; something well explained in Mark Seemann book.

The question is: can someone introduce a pattern (or good practice) to to abstract the composition root without using a container?

Further notes (optional):

Backing to AutoFixture where the composition root occurs in Fixture type constructors; I think that *Relays class are used to group instances in a similar manner some DI library has the concept of module. It's interesting note that there's a type named BehaviourRoot that sits on the top of the graph (is this the kind of abstraction I'm searching?).

like image 286
jay Avatar asked Mar 14 '13 06:03

jay


1 Answers

The description of the Composition Root pattern explicitly states that it can be implemented without a DI Container. The pattern is independent of container usage, and can be used with or without a container.

The Composition Root is the entry point of an application. By definition, it has no callers so it makes no sense to abstract it.

To quote the above article:

The Composition Root is an application infrastructure component.

Only applications should have Composition Roots. Libraries and frameworks shouldn't.

(bold my emphasis)

AutoFixture is a library, and thus has no Composition Root. The Fixture class is Façade, in the spirit of building a DI-friendly library.

like image 129
Mark Seemann Avatar answered Sep 25 '22 00:09

Mark Seemann