Best way to handle application settings

Mar 22, 2011 at 12:54 AM
Edited Mar 22, 2011 at 12:56 AM

Several discussions suggest handling app settings by creating a seperate viewmodel or POCO, configuring registering it in the bootstrapper as a singleton, and injecting it into any class that needs access to the settings. That makes perfect sense, but I can't put my finger on where you actually instantiate the singleton for the first time in order to perform set up on the object. For example, I'll need to read the settings out of a file in isolated storage (probably the app settings file) and set the values to whatever the user last selected.

In my Windows Phone 7 project I tried creating a simple object and registering it as a singleton in the bootstrapper, but the bootstrapper doesn't execute the constructor. I'm thinking I still need to instantiate the object somewhere. Any suggestions? 

Coordinator
Mar 22, 2011 at 12:59 AM

After you configure it as a singleton in the bootstrapper, just resolve it from the container and set it up there.

Mar 22, 2011 at 1:07 PM

Thanks, Rob. Could you give me a hint about where to start with this? I've tried several things, including working with container.GetInstance() and container.BuildUp(), as well as just going new SettingsPageViewModel() in the bootstrapper's Configure() method, but I'm not getting anywhere. The SettingsPageViewModel gets instantiated when it's injected into the MainPageViewModel, or else I get various errors when the project deploys.

The examples in the documentation are good, but I'm not always able to sort out what is appropriate for WP7.

Coordinator
Mar 22, 2011 at 3:56 PM

Are you using the SimpleContainer from the WP7 sample or something else?

Mar 22, 2011 at 3:58 PM

I'm using SimpleContainer. Most of the examples in the documentation seem to be geared toward using MEF, which is what's making it difficult for me to see the path forward.

Mar 23, 2011 at 2:50 PM

If anybody has any suggestions on this, I'll write up a blog post and build a sample project for other devs to reference later.

It looks like what I'm trying to do is pretty similar to what this dev was aiming for. I'd like my settings viewmodel or POCO to be created as early as possible, so I can access it from everywhere.

There are MEF examples that seem to be accomplishing exactly what I want to do, but I don't know how that translates to WP7.

Mar 23, 2011 at 4:55 PM

Hi Josh

the behaviour you reported is perfectly normal (and common to all IoC containers I'm aware of): registering a compontent as a singleton only ensures that will only be created (at most) a single instance, shared between all components requiring it.

The instance, however, will not be created until another component asks for it. Hence the Rob's suggestion to request your component immediatly to have the component instantiated in a determinate moment (perhaps in the Configure override). 

The code should look like:

	SimpleContainer container;
	protected override void Configure()
	{
		container = new SimpleContainer();
		container.RegisterSingleton(typeof(IMyService), null, typeof(MyConcreteServiceImplementation));
		
		//instance is created here
		IMyService instance = (IMyService)container.GetInstance(typeof(IMyService), null);
	}

If you are still having issues with something similar to this, then there should be something wrong elsewhere. In that case, please describe the exact problem with some more details.

Mar 23, 2011 at 5:30 PM

Excellent--thanks! It makes sense that the singleton is created when it was first called. The IMyService instance = (IMyService)container.GetInstance(typeof(IMyService), null) line appears to be working perfectly.

I was close--I'd tried ViewModel instance = new ViewModel() and container.GetInstance(typeof(ViewModel),null). :)