Really Struggling with Class Library, How do I Handle Lazy Loading/ConnectionString in Library?
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开发者_如何学Python to have to adjust a huge number of functions and property and pass through the connections string.
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.
精彩评论