zachvaldez
asked on
Placing a Configuration connection string in the Class so it can be viewed and use by all classes
Adding configuration manager in namespace so it can be visible to all the classes is not possible as below
Public string cs = ConfigurationManager.Conne ctionStrin gs["Produc tCalls"].C onnectionS tring; >>>>>> This does not work.
It seems that it works only if code is place inside each Public classes.
Namespace ABC
{
Public Class XYX
{
,,,,,,,,}
Public Class XXX
{
}
Public Class ZZZ
{
}
Public string cs = ConfigurationManager.Conne
It seems that it works only if code is place inside each Public classes.
Namespace ABC
{
Public Class XYX
{
,,,,,,,,}
Public Class XXX
{
}
Public Class ZZZ
{
}
Another approach (maybe more inline with OOP) is to write a data access class that contains your connection string (as a property or method), along with any other methods and properties that you need to share.
You can then either inherit from it (maybe implemented as an abstract class), or dependency inject it - very easy to do if you're using a DI Framework, such as AutoFac.
You can then either inherit from it (maybe implemented as an abstract class), or dependency inject it - very easy to do if you're using a DI Framework, such as AutoFac.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Dustin - not a great example - you're hardcoding the connection into the class, which makes testing impossible. You'll also create a new connection for every query you run. Not great on performance.
The cons of a static class (not an embedded one like above) is it's effectively a global variable, accesible to all sorts of other classess. This may be a quick-fix to a problem, but it'll be more robust if you just inject an instance to only those classes that need it. And for testing - just register a different class with the IoC.
The cons of a static class (not an embedded one like above) is it's effectively a global variable, accesible to all sorts of other classess. This may be a quick-fix to a problem, but it'll be more robust if you just inject an instance to only those classes that need it. And for testing - just register a different class with the IoC.
Not sure what your qualms are with the quick example, demonstrating how to call the static class from an instanced class.
There is plenty of utility in using the static class as such, and you should wrap your queries in using blocks or close the connections. Are you forgetting about Connection Pooling?
There is plenty of utility in using the static class as such, and you should wrap your queries in using blocks or close the connections. Are you forgetting about Connection Pooling?
Ideally you write a class, say DataAccess and then in that class
Open in new window
Please tweak the code as I wrote it without IDE, it can be improved a lot. Error handling can be added and connection usage can be optimized even further by making this connection limited to DataAccess class only and have various methods likeExecuteSQLReader, ExecuteDataSet, ExecuteInsert, etc.. (This is how Microsoft Enterprise Library handles DataAccess) so that your connection is only utilized via this DataAccess Class only.
So, XYX, XXX, ZZZ whenever you need to perform a data operation, you will do something like this...
Open in new window
Regards,
Chinmay.