在程序集之间传递连接字符串(Passing connection strings between assemblies)

编程入门 行业动态 更新时间:2024-10-19 13:24:22
在程序集之间传递连接字符串(Passing connection strings between assemblies)

我有两个可执行文件访问包含数据访问层对象的DLL每个exe可以有一个不同的配置包含服务器和数据库名称,连接字符串等信息。

如何将配置信息传递给DAL对象?

显然我可以在每次实例化时将配置字符串传递给DAL对象,但这看起来很麻烦且重复,因为DAL中的每个类都需要Config字符串的属性。

谁能告诉我是否有更好的方法?

我想知道是否有一种方法可以让实例化的DAL对象从调用对象中读取一些属性而不必显式传递它们?

I have two executables that access a DLL containing Data Access Layer objects Each exe can have a different config containing info on server and database name, connection string etc.

How do I pass the config info to the DAL objects?

Obviously I can pass the config strings to the DAL objects every time I instanciate one, but that seems messy and repetitive as every class in the DAL will require Properties for the Config strings..

Can anyone tell me if there is a better way?

I am wondering if there is a way for the instanciated DAL objects to read some properties from the calling object without me having to pass them explicitly?

最满意答案

如何让所有DAL类派生自包含连接字符串属性的基类?

在这种情况下,您可以在子构造函数调用上调用base的构造函数,然后该基本构造函数将成为加载Connection字符串的单个位置。

如果在两个Exe中都使用了相同的ConfigKey,例如:“DBConnectionString” - 那么构造函数将只从配置文件“DBConnection”条目加载值 - 无论哪个exe调用dal,连接字符串的正确值都将被传递它

实现将在以下几行......对于使用C#类似的语法表示抱歉。 我很久以前上次使用过VB。

class BaseDAL { protected String _connectionString; BaseDAL () { _connectionString = ConfigurationManager.AppSettings("DBConnectionString") } .... } class ChildDAL : BaseDal { ChildDAL():base() { .... } }

What about having all DAL classes derive from a base class which contains the connection string property?

In that case, you could call the base's constructor on the child contructor invocation and that base contructor would then be the single place for loading the Connection string.

If you have the same ConfigKey used in both Exe's eg: "DBConnectionString" - then the contructor will just load the values from config files "DBConnection" entry - no matter which exe is calling the dal and the correct value of connection string will be passed to it

Implementation would be something on below lines... Sorry for using C# - like syntax. i last used VB very many years ago.

class BaseDAL { protected String _connectionString; BaseDAL () { _connectionString = ConfigurationManager.AppSettings("DBConnectionString") } .... } class ChildDAL : BaseDal { ChildDAL():base() { .... } }

更多推荐

本文发布于:2023-04-28 06:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   程序   Passing   assemblies   strings

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!