在DAL层使用的AppSettings [英] Using appsettings in the DAL layer

查看:149
本文介绍了在DAL层使用的AppSettings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有其中的一些数据被存储在XML文件的WinForms应用程序。
中的位置,这些XML文件应存储可以由用户来配置,并存储在AppSettings的。
我所有的层是分开的组件。我可以访问我的设置从我DAL组装,或者我应该通过这通过我的所有层的参数?

I have a winforms application in which some of the data is stored in XML files. The location where these XML files should be stored can be configured by the user, and is stored in the AppSettings. All my layers are separate assemblies. Can I access my settings from my DAL assembly, or should I pass this as an argument through all my layers?

当我试图从我的DAL层读取设置,我遇到的另外一个问题。

When I try to read the settings from my DAL layer, I encounter another problem

        Configuration config = ConfigurationManager.OpenExeConfiguration(
            System.Reflection.Assembly.GetEntryAssembly().Location);
        string dataStorageLocation = config.AppSettings["DataStorageLocation"];



config.AppSettings [DataStorageLocation]给出一个编译错误:System.Configuration.ConfigurationElement.this [ System.Configuration.ConfigurationProperty]无法访问由于其保护级别。这是为什么?

config.AppSettings["DataStorageLocation"] gives a compilation error: System.Configuration.ConfigurationElement.this[System.Configuration.ConfigurationProperty] is inaccessible due to its protection level. Why is that?

有人可以把我在正确的轨道上?谢谢你。

Can someone put me on the right track? Thanks.

推荐答案

您需要使用 config.AppSettings.Settings [DataStorageLocation]
为样本MSDN文档

另外,更好恕我直言,你可以使用 System.Configuration.ConfigurationManager.AppSettings [名] 来访问的的AppSettings主机应用程序。这可能比你的技术更灵活,因为它也将工作,如果你的DAL组件例如,在IIS服务层主持。从主机应用程序的配置文件直接以这种方式获取配置信息是完全accceptable,一般比了下去配置信息传递通过层层越好。

Alternatively, and IMHO better, you could use System.Configuration.ConfigurationManager.AppSettings[name] to access the AppSettings of the host application. This is probably more flexible than your technique, as it will also work if your DAL assembly is, for example, hosted in a service tier on IIS. Accessing configuration information from the host application's configuration file directly in this way is perfectly accceptable, and generally better than passing configuration information down through the layers.

这篇关于在DAL层使用的AppSettings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆