我有两个可执行文件访问包含数据访问层对象的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() { .... } }更多推荐
发布评论