真的很纠结类库,如何处理库中的延迟加载/ConnectionString?
我真的很难理解使用类库的想法,而且我几乎只是用重复的类而不是共享类库来维护单独的项目。
我创建了一个包含一个类库项目和两个 Web 应用程序的解决方案。我的主要问题是连接字符串。这些是在网络项目中保存/声明的,每次执行任何类型的数据访问时,我都必须将它们传递到类库中。我有点明白为什么我应该这样做,所以我暂时就这样做。
这现在导致我遇到了延迟加载的问题。我对以下属性使用延迟加载:
Public Property KeyRelationshipManager() As Employee
Get
If _keyRelationshipManager Is Nothing Then
_keyRelationshipManager = Employee.GetEmployee(_keyRelationshipManagerStaffNumber)
Return _keyRelationshipManager
Else
Return _keyRelationshipManager
End If
End Get
Set(ByVal value As AECOM.Employee)
_keyRelationshipManager = value
End Set
End Property
因为此属性正在使用函数:
Employee.GetEmployee
我需要将连接字符串传递给该函数。
这意味着我每次使用连接字符串时都需要将其传递到属性中,以便我可以将其传递到函数中。
这是正确的吗?我“感觉”不对,因为我将不得不调整大量函数和属性并传递连接字符串。
I'm really struggling to get my head around the idea of working with a Class Library and I'm pretty much at the point of just maintaining separate projects with duplicate classes rather than a shared class library.
I've created a solution that contains a single class library project and two web applications. My main problems are the connection strings. These are held/declared in the web projects and I'm having to pass them into the class library every time I perform any kind of data access. I sort of understand why I should do this so I'm going with it for the moment.
This has now led me to a problem/question with lazy loading. I'm using lazy loading for the following property:
Public Property KeyRelationshipManager() As Employee
Get
If _keyRelationshipManager Is Nothing Then
_keyRelationshipManager = Employee.GetEmployee(_keyRelationshipManagerStaffNumber)
Return _keyRelationshipManager
Else
Return _keyRelationshipManager
End If
End Get
Set(ByVal value As AECOM.Employee)
_keyRelationshipManager = value
End Set
End Property
Because this property is using the function:
Employee.GetEmployee
I need to pass in the connection string to that function.
This means I would need to pass the connection string in to the property every time I use it so I could pass it into the function.
Is this correct? It doesn't 'feel' right to me because I'm going to have to adjust a huge number of functions and property and pass through the connections string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要将连接字符串传递到类库中?在类库中使用
ConfigurationManager.ConnectionStrings["myClassLibraryConnection"]
。只要您的主机应用程序的配置文件中都有该连接字符串,就应该没问题。 web.config 文件的作用是绑定不同类库的配置以形成单个应用程序。Why are you passing the connection string in to the class library? Use
ConfigurationManager.ConnectionStrings["myClassLibraryConnection"]
in your class library. As long as you have that connection string in both your host applications' config files, it should be fine. It's the web.config files' jobs to bind the configuration of different class libraries to form a single application.