c# - Storing application configuration data into a static list -
i implementing mvvm in wpf application , have hard time deciding , how store configuration data (loaded xml file) it's accessible in every view model. came is:
static class configurations { private static list<object> container = new list<object>(); public static void loadconfigurations() { } public static list<object> getconfiguration(string configuration) { -- return needed subelement -- return container(configuration); } }
i thinking of using static class because accessible without passing data between view models. have call configurations.loadconfigurations() @ start of application , call configurations.getconfigurations() when it's needed.
so question is, way deal kind of situations or it's bad practice?
don't mind correctness of code, it's example purpose.
so question is, way deal kind of situations or it's bad practice?
it depends :) if want able unit test view models want able mock configuration settings. in case better off using shared service inject view model classes with.
the service should implement interface:
public interface iconfiguration { list<object> getconfiguration(string configuration); } public class configurations : iconfiguration { private readonly list<object> container = new list<object>(); public list<object> getconfiguration(string configuration) { -- return needed subelement -- return container(configuration); } }
you can provide implementation of interface in unit test:
iconfiguration config = new configurations(); //or other implementation... viewmodel vm = new viewmodel(config);
view model:
public class viewmodel { public viewmodel(iconfiguration config) { var config = config.getconfiguration("..."); //... } }
Comments
Post a Comment