Does Persisting a Connection String Across Injection Modules Break the MVVM Pattern?

Topics: Prism v4 - WPF 4
Jul 14, 2012 at 5:44 PM
Edited Jul 14, 2012 at 6:17 PM

I have a WPF solution using MVVM with Prism and Unity. My login screen obtains connection strings from the module's app.config and puts them into a combox so that the user can select the connection . I need to persist the string so that it is discoverable by Views that are injected into the main screen. From my reading, I THINK that I have learned that using a Singleton is the best way to accomplish this, but I am not sure. I have read about Singletons in an abstract sense, but I don't have a handle on how it would be used to accomplish my specific goal. So I have these questions: 1) Is a Singleton the best way to persist a string value across injected views/view modes?; 2) If not, then what?; 3) Can you point me to an example that is solves a problem similar to the one I have presented?

Jul 15, 2012 at 6:06 PM

P.S. I have found that a simple static variable in a shared class may be used to persist the connection string. However, in doing so, I believe that the MVVM pattern is broken because any module that will use the "global" connection string is now dependent upon the login module. How will independent unit testing be accomplished?

Jul 16, 2012 at 1:57 PM
Edited Jul 16, 2012 at 1:58 PM


Based on my understanding of your scenario, I believe using a singleton instance like a shared service to persist your string value across your different modules in a loosely coupled fashion should be appropriate. To achieve this you could register your service instance as a singleton in the container, and consume it across your different modules through a common Interface defined for example in a Infrastructure project referenced by your different modules.

For example in Unity you could register the singleton instance like this:

this.Container.RegisterType<IMyServiceInterface, MyService>(new ContainerControlledLifetimeManager()); 

I believe you may find the exercises provided in the Dependency Injection Lab from the Prism Training Kit useful, as they show how to create and consume a shared service using the Unity container.

Also, you could find more information about Shared Services in the following chapter of the Prism documentation:

I hope you find this useful,

Agustin Adami