Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I store my ASP.NET MVC site's settings?

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?

like image 684
Rinndar Avatar asked Jan 06 '10 17:01

Rinndar


2 Answers

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

like image 164
Mathias F Avatar answered Nov 07 '22 12:11

Mathias F


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... :)

like image 21
Rob Ashton Avatar answered Nov 07 '22 11:11

Rob Ashton