我应该如何存储我的ASP.NET MVC网站的设置?

时间:2021-06-04 03:13:14

I have an ASP.NET MVC site which is composed of 3 projects in a solution:

我有一个ASP.NET MVC站点,它由一个解决方案中的3个项目组成:

DomainModel (Class Library - Holds LINQ Repo)
DomainServices (Class Library - Holds Business Logic)
WebUI (ASP.NET MVC Site)

DomainModel(类库 - 包含LINQ回购)DomainServices(类库 - 包含业务逻辑)WebUI(ASP.NET MVC站点)

I need a place to store a bunch of settings for our site that can be configured via XML.

我需要一个地方来存储我们网站的一系列设置,可以通过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?

我是否在包含所有设置属性的某个C#类的构造函数中加载XML文件一次?

Can someone give me some examples and tips on storing settings in an XML file for use in a multi-project solution?

有人可以给我一些关于在XML文件中存储设置的示例和技巧,以便在多项目解决方案中使用吗?

5 个解决方案

#1


8  

Settings are usually stored in web.config. All assemblies have acces to these settings.

设置通常存储在web.config中。所有程序集都可以访问这些设置。

ConfigurationManager.AppSettings["key"]

You need to add a reference to System.configuration.dll

您需要添加对System.configuration.dll的引用

#2


7  

Rather than using AppSettings, which is just a collection of key value pairs, consider defining your own configuration structure using ConfigurationSection or IConfigurationSectionHandler

而不是使用仅仅是键值对集合的AppSettings,考虑使用ConfigurationSection或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).

这样你就可以获得包装类的所有安全性,并且它不会使你的AppSettings变得混乱(并且很好地定义了适合你的用途)。

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

更好的是,定义自己的XML模式,并使用XmlSerialization / Deserialization将此文件存储在web.config之外,监听它的更改(将其绑定到缓存,无论如何)。

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.

如果以这种方式执行此操作,则无需修改web.config以获取更改,因此无需重新启动Web应用程序,从而在此过程中丢失会话/缓存。

Not that well written stateless web applications should care about losing session/cache - but there is always somebody... :)

不是写得好的无状态Web应用程序应该关心丢失会话/缓存 - 但总有人...... :)

#3


2  

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, e.g:

我同意Mathias使用框架提供的默认方式。但是,您可以(应该,恕我直言)仍然制作某种包装类,可能在一个界面后面,以便于在单元测试中进行模拟,例如:

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

#4


1  

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.

我所做的是拥有一个具有所有属性/设置的类。它还将管理XML文件。

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

控制容器的反转将允许您使用依赖注入将它们注入到其他类中。

#5


0  

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.

在类库中定义共享属性以保存应用程序设置(Specialized.NameValueCollection的实例)。然后在您的站点逻辑中,将此新属性从global.asax的Application_Start sub中的web.config设置为ASP.NET设置。

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.

这样,您就可以从类库项目中获取设置。

#1


8  

Settings are usually stored in web.config. All assemblies have acces to these settings.

设置通常存储在web.config中。所有程序集都可以访问这些设置。

ConfigurationManager.AppSettings["key"]

You need to add a reference to System.configuration.dll

您需要添加对System.configuration.dll的引用

#2


7  

Rather than using AppSettings, which is just a collection of key value pairs, consider defining your own configuration structure using ConfigurationSection or IConfigurationSectionHandler

而不是使用仅仅是键值对集合的AppSettings,考虑使用ConfigurationSection或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).

这样你就可以获得包装类的所有安全性,并且它不会使你的AppSettings变得混乱(并且很好地定义了适合你的用途)。

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

更好的是,定义自己的XML模式,并使用XmlSerialization / Deserialization将此文件存储在web.config之外,监听它的更改(将其绑定到缓存,无论如何)。

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.

如果以这种方式执行此操作,则无需修改web.config以获取更改,因此无需重新启动Web应用程序,从而在此过程中丢失会话/缓存。

Not that well written stateless web applications should care about losing session/cache - but there is always somebody... :)

不是写得好的无状态Web应用程序应该关心丢失会话/缓存 - 但总有人...... :)

#3


2  

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, e.g:

我同意Mathias使用框架提供的默认方式。但是,您可以(应该,恕我直言)仍然制作某种包装类,可能在一个界面后面,以便于在单元测试中进行模拟,例如:

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

#4


1  

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.

我所做的是拥有一个具有所有属性/设置的类。它还将管理XML文件。

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

控制容器的反转将允许您使用依赖注入将它们注入到其他类中。

#5


0  

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.

在类库中定义共享属性以保存应用程序设置(Specialized.NameValueCollection的实例)。然后在您的站点逻辑中,将此新属性从global.asax的Application_Start sub中的web.config设置为ASP.NET设置。

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.

这样,您就可以从类库项目中获取设置。