连接字符串应存储在 n 层 asp.net 应用程序中的何处

发布于 2024-09-25 14:53:08 字数 383 浏览 5 评论 0原文

各位,

我有一个 ASP.NET 项目,按名称空间划分,它是 n 层的,但我需要分成三个项目:数据层、中间层和前端。

我这样做是因为...

A) 这似乎是正确的做法,

B) 我在为 ASP.NET 托管程序集运行单元测试时遇到各种问题。

不管怎样,我的问题是,你在哪里保存你的配置信息?

例如,现在,我的中间层类(使用 Linq to SQL)在实例化新数据上下文时自动从 web.config 中提取其连接字符串信息。

如果我的数据层位于另一个项目中,它可以/应该使用 web.config 来获取配置信息吗?

如果是这样,单元测试(通常在单独的程序集中)将如何提供这样的配置信息?

谢谢您的宝贵时间!

Folks,

I have an ASP.NET project which is pretty n-tier, by namespace, but I need to separate into three projects: Data Layer, Middle Tier and Front End.

I am doing this because...

A) It seems the right thing to do, and

B) I am having all sorts of problems running unit tests for ASP.NET hosted assemblies.

Anyway, my question is, where do you keep your config info?

Right now, for example, my middle tier classes (which uses Linq to SQL) automatically pull their connection string information from the web.config when instantiating a new data context.

If my data layer is in another project can/should it be using the web.config for configuration info?

If so, how will a unit test, (typically in a separate assembly) provide soch configuration info?

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墨离汐 2024-10-02 14:53:08

我们将它们保存在一个全局“设置”文件中,该文件恰好是 XML。该文件包含所有全局设置,其中之一是指向相应服务器的连接字符串以及用户名和密码。然后,当我的应用程序使用它时,它们会将所需的特定目录(数据库)放入连接字符串中。

我们为每个操作环境(prod、dev、staging 等)都有一个文件版本。然后,通过两个设置——文件路径(带有代表环境的令牌)和环境——我可以选取正确的设置文件。

这还有 30 秒故障转移的好处。只需更改设置文件中的服务器名称并重新启动应用程序(Web),您就可以进行故障转移(当然,如果需要,您必须恢复数据)。

然后,当应用程序启动时,我们将正确的连接字符串写入 web.config 文件(如果不同)。这样,我们就可以通过更改一个 appSettings 值来将网站从 DEV 更改为 PROD。

We keep them in a global "Settings" file which happens to be XML. This file contains all the GLOBAL settings, one of which is a connection string pointing to the appropriate server as well as username and password. Then, when my applications consume it, they put the specific catalog (database) they need in the connection string.

We have a version of the file for each operating environment (prod, dev, staging, etc). Then, with two settings -- file path (with a token representing the environment) and the environment -- I can pick up the correct settings files.

This also has the nice benefit of a 30 second failover. Simple change the server name in the settings file and restart the applications (web) and you have failed over (of course you have to restore your data if necessary).

Then when the application starts, we write the correct connection string to the web.config file (if it is different). With this, we can change a website from DEV to PROD by changing one appSettings value.

哑剧 2024-10-02 14:53:08

只要不是太多,放在web.config里就很方便了。当然,您的 DAL 应该完全不知道它来自那里。

一个好的选择是,当您的数据层被要求执行某些操作时,它会被赋予其配置信息,并且当网络调用进入时,它将被要求执行某些操作。继续将信息放入您的 web.config 中。在我当前的项目中,我的数据层中有一个连接字符串的静态字典,我在从我的 global.asax 调用的例程中像这样填写它:

CAPPData.ConnectionStrings(DatabaseName.Foo) = 
    ConfigurationManager.ConnectionStrings("FooConnStr").ConnectionString()
CAPPData.ConnectionStrings(DatabaseName.Bar) = 
    ConfigurationManager.ConnectionStrings("BarConnStr").ConnectionString()
etc.

像这样“注入”它可以很好用于自动化测试目的,具体取决于您如何/是否测试 DAL。对我来说,这只是因为我不想制作单独的配置文件。

As long as there isn't too much, it's convenient to have it in the web.config. Of course, your DAL should have absolutely no clue that it comes from there.

A good option is for your data layer to be given its config information when it is called upon to do something, and it will be called upon to do something when a web call comes in. Go ahead and put the information in your web.config. In my current project, I have a static dictionary of connection strings in my data layer, which I fill out like so in a routine called from my global.asax:

CAPPData.ConnectionStrings(DatabaseName.Foo) = 
    ConfigurationManager.ConnectionStrings("FooConnStr").ConnectionString()
CAPPData.ConnectionStrings(DatabaseName.Bar) = 
    ConfigurationManager.ConnectionStrings("BarConnStr").ConnectionString()
etc.

"Injecting" it like this can be good for automated testing purposes, depending on how/if you test your DAL. For me, it's just because I didn't want to make a separate configuration file.

掌心的温暖 2024-10-02 14:53:08

出于测试目的,不要使用默认构造函数实例化 DataContext。将连接字符串信息传递给构造函数。

我更喜欢使用 IoC 框架注入与数据上下文的连接,然后将上下文注入其他类。

For testing purposes don't instantiate DataContext with default ctor. Pass connection string info to constructor.

I prefer to use IoC frameworks to inject connection to data context then inject context to other classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文