I've came up with some solution on which my IoC/DI container (Castle Windsor) is claiming that there's a cyclic dependency tree. And it's true. But I'm not that sure that this cycle is that harmful.
This is, more or less, the dependency tree:
- A WebAPI controller depends on...
- ...a service A depends on...
- ...an unit of work depends on...
- ...a repository depends on...
- ...a domain event manager(1) depends on many...
- ...domain event handlers, and one depends on...
- ...service A(2)
(1) A domain event manager is a generalized class which aims to coordinate concrete domain events to be listened by the same or other domains and perform side actions.
(2) Here's where the dependency cycle happens
My domain event management and handling are implemented with aspect-oriented programming in mind, hence, while it's part of the dependency tree, a given domain event handler may or may not depend on a service in the same dependency tree: I consider the domain event handler like an additional top-level dependency. But the worst case has already happened.
My point is that, since a domain event is a cross-cutting concept, a given handler should be able to inject any service, even some one that's already in the dependency tree of a given operation flow.
For now, I've fixed the issue using property injection in the affected domain event handler, but anyway there could be an alternative to the whole workaround.