c# - Dependency Injection in multilayered Application -


so problem following. solution contains following projects (with references):

  • presentation layer, contains views (has reference application layer)
  • application layer, contains viewmodels (has reference domain , persistence layer)
  • domain layer, contains models (no reference anything)
  • persistence layer, stores data entity framework (reference domain layer)

all right, want use dependency injection decouple viewmodels services , other stuff. because i'm using dialogs, need inject idialogservice implementation dialogservice. now, dialogservice uses presentation-specific dlls in presentation project had implement idialogservice interface in presentation project, unity-container in application layer. think can see problem is: have reference presentation layer application layer, not other way.

am doing right , how can solve problem?

you missing layer: composition root layer. top-most layer of application , references other layers in application. see layer put same assembly presentation layer (which fine, because layers logical artifacts, while assemblies physical artifacts). in case of wpf however, easy move wpf related stuff different assembly , let start-up project consist of nothing more bootstrapping logic (with container) wires together.

so in general shouldn't let each assembly responsible of own wiring, because cause needless dependency on container. in general composition root has take dependency on di library.

also see this related question.


Comments