2

So my problem is the following. My solution contains the following projects (with references):

  • Presentation Layer, contains Views (has a reference to Application Layer)
  • Application Layer, contains ViewModels (has a reference to Domain and Persistence Layer)
  • Domain Layer, contains all Models (no reference to anything)
  • Persistence Layer, stores data with Entity Framework (reference to Domain Layer)

All right, now I want to use Dependency Injection to decouple my ViewModels from Services and other stuff. Because I'm also using a dialogs, I also need to inject the IDialogService with the implementation DialogService. Now, the DialogService uses some Presentation-specific DLLs which are only in the Presentation project so I had to implement the IDialogService interface in the Presentation project, but the Unity-Container is in the Application Layer. I think you can see what my problem is: I only have a reference from Presentation Layer to Application Layer, not the other way.

Am I doing this right and how can I solve this problem?

rmnblm
  • 147
  • 2
  • 9
  • Your first bulleted point states that your presentation layer has reference to application layer but last line in your question states that you only have a reference from Application Layer to Presentation Layer. Is it correct? – BSG May 08 '15 at 06:46
  • I'm sorry, that was a mistake. I just corrected it. Thanks :-) – rmnblm May 08 '15 at 06:57

1 Answers1

4

You are missing a layer: the Composition Root layer. This is the top-most layer of your application and it references all other layers in you application. Often you see that this layer is put into the same assembly as the presentation layer (which is fine, because layers are logical artifacts, while assemblies are physical artifacts). In the case of WPF however, it is very easy to move all WPF related stuff to a different assembly and let the start-up project consist of nothing more than the bootstrapping logic (with the container) that wires everything together.

So in general you shouldn't let each assembly be responsible of its own wiring, because that would cause a needless dependency on the container. In general only the composition root has to take a dependency on your DI library.

Also see this related question.

Steven
  • 166,672
  • 24
  • 332
  • 435