Perhaps this is too broad a question, but I still want to hear what are the best practices and what is considered 'correct'. I did not find a similar topic, probably due to how generic it is and thus hard to search.
This all began when we took a windows service project and added another service to it. Previously, the main method was what we considered the Composition Root, and our IoC container registration code, app.config reading and similar was in the main method in one form or another. After assembling required dependencies there we created the service by calling Resolve
on the container and running it.
When we created another class inheriting from ServiceBase
we noticed that it did not make sense to have the main as the composition root anymore, because the multiple services are potentially completely different and thus require different initialization code.
The only enlightening post I found on the mather was this one from Mark Seemann, where he states that the OnStart
would be the aggregate root (in that particular case).
In general, what is the best approach to take when choosing and appropriate composition root? Should I consider this windows service scenario an outlier and just code differently for it while leaving the main method as other project types composition roots, or is there a more general approach that would be valid in all contexts?
Perhaps the fact that the services require very different setups is an indicator that they should be separated in multiple standalone projects, where the main would be again used as the composition root?
We generally use .Net but this probably applies to many other programming languages as well.