从单独的类库访问web.config? [英] Access web.config from separate Class Library?

查看:115
本文介绍了从单独的类库访问web.config?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个好的方法来实现以下:

I'm looking for a good way to achieve the following:

我有一个Web应用程序(MVC 3),有一个单独的类库, - 我做一个CMS的逻辑。此CMS使用NHibernate连接到数据库。我希望用户能够在其web.config文件中配置连接字符串(最终甚至是数据库的风格)。

I have a web application (MVC 3), with a separate Class Library that contains the back-end logic of a CMS that I'm making. This CMS uses NHibernate to connect to a database. I want the user to be able to configure the connectionstring (and eventually even the flavour of the database) in their web.config file.

我要找的是一个很好的方法来从web.config文件中获取连接字符串,即使DLL是完全独立的。这可能吗?我不得不把我的连接字符串到我的类库不知何故?或者我可以在应用程序运行时访问它吗?

What I'm looking for is a good way to get the connection string from the web.config file, even though the DLL is completely separate. Is this possible? Will I have to pass my connection string to my class library somehow? Or will I be able to access it when the application runs?

如果我必须在我的web应用程序中创建一些代码来将连接字符串传递给我的类库,我如何使这个代码尽可能便携,所以我赢了

If I have to create some code in my web application to pass the connection string to my Class Library, how can I make this code as portable as possible, so I won't have to write it again for my next webapp?

非常感谢您有任何想法。

Thanks a lot for any ideas you have.

推荐答案

您可以将连接字符串传递到类库中的类从网站。

You can pass in the connection string to the classes in the class library from the web site.

这是一个更好的选择比试图直接从配置文件获取信息,否则你将有一个依赖于配置文件存在与确切的正确的键(使测试类稍微更难)。

This is a better choice than trying to get the information directly from the configuration file, as otherwise you will have a dependency on the configuration file existing with the exact right key (making testing the class somewhat harder).

请参阅这篇博文直接访问配置的参数(这是非常常见的,但不是最佳实践)。

See this blog post for arguments against accessing configuration directly (which is very commonly done, but is not best practice).

这篇关于从单独的类库访问web.config?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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