简体   繁体   中英

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?

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

I agree with Mathias to use the default way offered by the framework. However, you can (should, IMHO) still make some kind of wrapper class, probably behind an interface for facilitating mocking in unit tests, eg:

class MyAppSettings
{
    public int SomeSetting 
    { 
       get 
       {
         return Convert.ToInt32(ConfigurationManager.AppSettings["SomeSetting"]); 
       } 
    }
}

I would store it in a new service with an interface.

What I do is have a class have all the properties/settings. It would also manage the XML file.

An Inversion of Control container would allow you to injection them using dependency injection into your other classes.

Define a Shared property in your class library to hold the app settings (an instance of Specialized.NameValueCollection ). Then in your site logic, set this new property to your ASP.NET settings from web.config in Application_Start sub of global.asax.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs on application startup
    DomainModel.Common.AppSettings = ConfigurationManager.AppSettings
End Sub

This way, you can get to the settings from the class library projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM