I need to build a Data Access Library to be used from many small applications afterwards.
It will heavily use the DataReader objects. The tables may exist with same structure either in SQL Servers or in DB2/400. This means that a method for example
GetItemsByWarehouse()
Must be able to run either against SQL Server DB or DB2. Where it will run depends on the server availability and user selection.
What i plan to do (and need advice on it) is :
- Implement the DAL based on Singleton design Pattern to ensure that i will have only one instance of my Library.
- Have a property that will set the connection string.
- Have a property that will set if the target server is AS400 or SQL.
I dont know if this course of action is correct. Should i implement point #3 or i could get the type from the connection string?
Also How i should implement such a method as above? check the property and decide inside the method if i will use Sqlconnection or OleDbConnection e.t.c?
I paste this code from my micro Orm . There are multiple overloads for the constructor to specify what Db you want used.
Note that the DAO (DbAccess) doesn’t care about the concrete provider.
Here’s how the ProviderFactory looks. Here you can add a method to detect the db and to return a provider.
For more code snippets and inspiration you can check the Github repo
I would advice against the Singleton pattern, it’s much better to let a DI container to manage the instance life. Also, the app should use the interface of the DAO not the concrete instance (this will help you in the future).