0
votes

I am designing a WPF application using MVVM and I am experimenting with the UnityContainer to resolve my MVVM objects. My problem is that my UnityContainer needs references to all my Views and ViewModels to construct the appropriate object that I need. This is fine except when one of those ViewModels needs to open a new window. To handle this I thought of having a DialogService that would get MVVM objects from the UnityContainer and then open the new window. This ends up creating a circular dependency because my ViewModel needs to see the DialogService, the DialogService needs to see the UnityContainer, and my UnityContainer needs to see my views and viewmodels. I'm not using Prism or anything just the UnityContainer installed form Nuget. The following is a pseudo-code simplified version of my problem.

Assembly-Interfaces (I reference nobody)

  • IViewX
  • IViewY
  • IViewModelX
  • IViewModelY

Assembly-Views (I reference Interfaces)

  • ViewX : IViewX
  • ViewY : IViewY

Assembly-ViewModels (I reference Interfaces and DialogService) <- this is circular dependency

  • ViewModelX : IViewModelX
  • ViewModelY : IViewModelY

Assembly-MyUnityContainer (I reference Interfaces, Views, and ViewModels)

  • UnityService (provides access to the UnityContainer)

Assembly-DialogService (I reference Interfaces and MyUnityContainer)

  • DialogService

Obviously my design is flawed. I'm just not sure what is the right way to use the UnityContainer from anywhere since it has to have references to everywhere in order to resolve my Views and ViewModels. So those of you experienced with using UnityContainer what am I doing wrong and how would you recommend I design this? Btw I am using WPF 4.5.

Note: My problem is design related, I literally cannot add a reference to my DialogService in my ViewModel, VS won't allow it. This is not the problem where a circular dependency causes a stack overflow. Just wanted to make that clear.

Thanks!

EDIT: RESOLUTION As per the advice I received I ended up doing a design similar to the following.

Design: How assemblies reference each other.

The MainApp sees everything, so it creates the UnityContainer, registers all of the types and hands it to the DialogService assembly as an IUnityContainer. Now the DialogService assembly can resolve any interface type without knowing about the concrete implementations. The ViewModel can use DialogService to create dialog services, which in turn open windows or other dialogues. The ViewModel just needs to know about the interface that it wants to open and give that to the DialogService, the dialog service does the work of opening the window.

1

1 Answers

1
votes

I would say that ideally, Assembly-MyUnityContainer is the one that should reference everything.

So in order to break the dependency from Assembly-DialogService to Assembly-MyUnityContainer, you need to stop depending on interfaces or classes in Assembly-MyUnityContainer from Assembly-DialogService.

Why not add your own IContainer interface to your interfaces assembly, which abstracts the Unity Container? Then have the dialog service assembly only refer to the interfaces assembly...