I have to design a Data Access Layer with .NET that probably will use more than one database management system (Mysql and Sql Server) with the same relational design.
Basically, it has to be simple to switch from one database to another so I would like you to recommend me some web-sites or books that has been useful for you, with common design patterns or information in general to implement this kind of data access layer.
Thank you.
Access patterns or query patterns define how the users and the system access the data to satisfy business needs.
Design patterns in software engineering are repeatable solutions to common software design requirements. A design pattern is an abstraction that does not translate directly into executable code. It is a problem-solving template that can be used as the foundation to design a solution.
Data-Access Layer (DAL) Data-Access Layer is a layer in an application that provides easy and simplified access to data stored in persistent storage, such as an entity-relational database or any database for that matter. It is layer that exists between the Business Logic Layer (BLL) and the storage layer.
A data access layer (DAL) in computer software is a layer of a computer program which provides simplified access to data stored in persistent storage of some kind, such as an entity-relational database. This acronym is prevalently used in Microsoft environments.
I recommend Patterns of Enterprise Application Architecture by Martin Fowler.
A list of the patterns is also on his website
The DataMapper pattern is also relevant.
I like using interface based Db access. Every db provider for Ado.net implements basic interfaces, and when you use them your code may look like this:
public static IDbConnection GetConnection(string connectionName)
{
ConnectionStringSettings ConnectString = ConfigurationManager.ConnectionStrings[connectionName];
DbProviderFactory Factory = DbProviderFactories.GetFactory(ConnectString.ProviderName);
IDbConnection Connection = Factory.CreateConnection();
Connection.ConnectionString = ConnectString.ConnectionString;
return Connection;
}
Then, when you need to communicate with db:
public static DataTable Dummy()
{
using (IDbConnection Connection = GetConnection("SiteSqlServer"))
{
IDbCommand Command = Connection.CreateCommand();
Command.CommandText = "DummyCommand";
Command.CommandType = CommandType.StoredProcedure;
Connection.Open();
using (IDataReader reader = Command.ExecuteReader())
{
DataTable Result = new DataTable();
Result.Load(reader);
return Result;
}
}
}
With this technique you can create fully db independent DAL. Of course for some complex scenarios this is not enough. But mostly this will do the work, and you don't needed various external libs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With