I’ve been reading Mark Seemann’s book recently about Dependency Injection and it has raised some architectural questions around Inversion of Control. Let’s assume I have the following:
- A very basic executable project that acts as the composition root, called CompositionRoot.exe.
- A domain project that compiles to a library, called Domain.dll
- A data access project that compiles to a library, called DAL.dll
- A logging project that compiles to a library, called Logging.dll
Following IoC patterns from Seeman’s book, repository interfaces are defined in Domain. DAL references Domain and implements these interfaces. CompositionRoot is responsible for instantiating these repositories and injecting them into the Domain. So far, so uneventful.
Now the question; how does logging fit into this scenario?
I had envisioned the logging library being used by both the Domain and DAL. Some reading on StackOverflow shows some developers think logging only belongs in the Domain. There are times when logging in the DAL is useful for me, such as when benchmarking specific pieces of SQL, or logging Entity Framework exceptions without exposing Entity Framework specific exceptions to the Domain.
Let’s assume then that I want logging in both the Domain and the DAL (unless someone can convince me otherwise). Should the logging interface be defined in the Domain similar to the repository interfaces? If so, this would tie the DAL’s logging to Domains logging, which feels wrong. Alternatively, DAL could define its own logging interface which Logging also implements. This, however, leads to Logging having to implement a new interface for each new DLL that requires logging, which also feels wrong. Alternatively, should Domain and DAL reference Logging (seems to go against IoC)? Is there another way? I haven’t finished Seemann’s book yet but at a couple of points he alluded to using an interface library i.e. a DLL that consists of just interfaces. I can’t currently picture how this would work.