由多个 DLL 使用的外部配置文件
我有多个 DLL,用于将数据读/写到我的数据库中。
有一个表示层DLL和一个数据访问层DLL。我希望这些 DLL 共享一组连接字符串。
我的想法是将连接字符串存储在外部配置文件中的单独 DLL 中。我不确定这是否是一个好主意,以及是否可以在表示层和数据访问层中引用该外部 DLL。
另一个问题是我是否应该编写一个辅助类来从外部配置文件读取数据,或者是否应该使用内置的 .Net 方法?
谢谢
I have multiple DLLs that are used to read/write data into my database.
There is a presentation layer DLL and a data access layer DLL. I want these DLLs to share a set of the connection strings.
My idea is to store the connection string in a seperate DLL in the external configuration file. I'm not sure whether it's a good idea and whether I can reference that external DLL in both presentation and data access layers.
The other question is whether I should write a helper class to read the data from the external config file or whether I should be using built in .Net methods?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将配置文件访问代码隔离在单独的类中。通过该类上的接口使配置数据(连接字符串等)可用。让接口存在于共享程序集中。让任何需要此接口的类通过 依赖注入 获取对配置类实例的引用。如果您尚未使用 DI 框架,我强烈推荐 Autofac。
你取得了什么成就?表示和数据访问类现在仅依赖于共享接口定义。他们不关心该接口的实现是什么,无论它是从 web.config、machine.config 还是其他存储中读取连接字符串。更好的是,您现在可以通过伪造实现来更轻松地测试您的类。
更新:首先,说明如何通过接口获取配置数据。假设我们有以下配置服务:
我的数据访问类可以直接使用此类:
这种方法的问题是耦合。
DataAccess
现在直接依赖于ConfigurationService
类。我们为DataAccess
编写的任何测试都会无意中受到ConfigurationService
类的影响。另外,如果我们需要切换ConfigurationService
的实现,则需要更改DataAccess
(以及直接依赖于此类的所有其他类)。为了解决这个问题,我们反转依赖层次结构并引用接口而不是具体的类,如下所示:
数据访问类现在不知道配置服务实现是什么以及如何创建该实例。
Isolate the configuration file access code in a separate class. Make the configuration data (connectionstrings and whatnot) available via an interface on that class. Let the interface live in a shared assembly. Let any class that needs this interface get a reference to an instance of the configuration class via dependency injection. If you're not already using a DI framework I can highly recommend Autofac.
What have you achieved? Presentation and data access classes are now only dependent on a shared interface definition. They don't care what the implementation of that interface is, whether it reads connection strings from web.config, machine.config or some other store. Even better, you can now more easily test your classes by faking the implementation.
Update: First, to illustrate making the configuration data available via an interface. Say we have the following conffiguration service:
My data access class could use this class directly:
The problem with this approach is coupling.
DataAccess
is now directly dependent on theConfigurationService
class. Any tests we write forDataAccess
will inadvertently be affected by theConfigurationService
class. Also, should we need to switch out the implementation ofConfigurationService
it would require changes toDataAccess
(and all other classes directly dependent on this class).To solve this, we invert the dependency hierarchy and references an interface instead of the concrete class, like this:
The data access class is now oblivious as to what the configuration service implementation is and how that instance is created.
据我所知,DLL 文件无法使用 .net 配置项(例如 app.config 文件),因此如果您希望通过 xml 文件配置您的 dll,则必须自己编写它。
As far as I'm aware, DLL files cannot make use of .net config items like app.config files, so if you want your dll to be configurable through say an xml file, you'll have to write it yourself.
我可以将连接字符串存储在 machine.config 中,但再次不确定所有含义......
I can store the connection string in the machine.config, but once again not sure about all the implications....