I have an ASP.NET MVC site which is composed of 3 projects in a solution:
DomainModel (Class Library - Holds LINQ Repo)
DomainServices (Class Library - Holds Business Logic)
WebUI (ASP.NET MVC Site)
I need a place to store a bunch of settings for our site that can be configured via XML.
Which project should this go in? Does anyone have an example of how they load and then access their settings across these different projects?
Do I load the XML file once, in the constructor of some C# class that contains properties for all my settings?
Can someone give me some examples and tips on storing settings in an XML file for use in a multi-project solution?
Settings are usually stored in web.config. All assemblies have acces to these settings.
ConfigurationManager.AppSettings["key"]
You need to add a reference to System.configuration.dll
Rather than using AppSettings, which is just a collection of key value pairs, consider defining your own configuration structure using ConfigurationSection or IConfigurationSectionHandler
That way you get all the safety of the wrapper class, and it doesn't clutter up your AppSettings (and is nicely defined somewhere for your use).
Even better, define your own XML schema, and use XmlSerialization/Deserialization to store this file outside of web.config, listening for changes on it (tie it to the cache, whatever).
If you do it this way, you don't need to modify web.config to get the changes and therefore don't need to restart your web application losing session/cache in the process.
Not that well written stateless web applications should care about losing session/cache - but there is always somebody... :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With