This package offers a flexible and generic database abstraction supporting multiple providers, including MySQL. It facilitates asynchronous execution of SQL commands, stored procedures, and parameterized queries with methods like ExecuteReaderAsync and ExecuteNonQueryAsync. Designed for extensibility and integration with dependency injection, it is ideal for applications using the repository pattern. The package simplifies multi-database integration, providing customizable database connections for efficient data handling in .NET applications.
$ dotnet add package DataAccessProvider.MSSQLThe Data Access Provider Framework offers a flexible, pluggable way to interact with various data sources such as SQL databases (MSSQL, PostgreSQL, MySQL), file-based sources (JSON), and non-relational databases (MongoDB). By standardizing the approach to data access with source parameters (SourceParams), the framework allows developers to seamlessly switch between different data sources with minimal code changes.
async/await for efficient, non-blocking database operations.MSSQLSourceParams)You can also extend the provider to support any custom data source by registering new data source implementations.
The DataAccessProvider is designed to provide a simple, consistent interface for interacting with multiple database types, such as MSSQL, MySQL, PostgreSQL, MongoDB, and more. By leveraging this provider, you can easily switch between databases by passing the appropriate data source parameters, without needing to modify your core application logic.
Microsoft.Data.SqlClientTo get started, clone the repository and reference it in your project. You'll also need to install the required NuGet packages for your data sources.
git clone https://github.com/your-repo/dataaccessprovider.git
The DataAccessProvider is available as a NuGet package from GitHub. You can add it as a dependency to your project directly from the GitHub NuGet package repository.
RegisterDataSource<TParams, TSource>(): This method allows the external consumer to register new custom data source types.BaseDataSourceParams and IDataSource implementations.Startup.cs.IDataSourceProvider to execute queries on the custom data source. // Add connection strings for each database type
services.AddDataAccessProviderMSSQL(configuration);
serviceProvider.UseDataAccessProviderMySQL();
To store your connection strings in the appsettings.json file, use the following structure:
appsettings.json:{
"ConnectionStrings": {
"MSSQLSource": "Server=myServerAddress;Database=myDataBase;User=myUsername;Password=myPassword;",
}
}
IDataSourceProviderThe IDataSourceProvider in this framework automatically determines which data source to use based on the provided SourceParams. This makes it flexible to switch between different data sources like MSSQL, PostgreSQL, or even JSON files, without changing your core logic.
Additionally, when using generic types, the provider can infer the type and return results mapped to a specified class, making it easy to handle type-safe responses.
// Resolve the IDataSourceProvider from the service provider
var dataSourceProvider = serviceProvider.GetService<IDataSourceProvider>();
// Example 1: Execute a query using MSSQLSourceParams
var mssqlParams1 = new MSSQLSourceParams
{
Query = "SELECT TOP 1 * FROM [dbo].[Diary]"
};
var result1 = await dataSourceProvider.ExecuteReaderAsync(mssqlParams1);
// Example 2: Execute a query with a typed return (e.g., Diary class)
var mssqlParams2 = new MSSQLSourceParams<Diary>
{
Query = "SELECT TOP 1 * FROM [dbo].[Diary]"
};
var result2 = await dataSourceProvider.ExecuteReaderAsync(mssqlParams2);
1: [{"Id":1,"Title":"First Entry","Date":"2022-01-01T00:00:00"}]
2: [{"Id":1,"Title":"First Entry","Date":"2022-01-01T00:00:00"}]
The DataAccessProvider allows you to register your own custom data sources at runtime using the RegisterDataSource<TParams, TSource>() method. This enables you to extend the framework by adding support for new data source types, without modifying the existing factory.
DbParameter Extension MethodThe library includes an extension method for adding database parameters to a list in a type-safe and generic manner. This method supports multiple database types, including SQL Server and PostgreSQL.
Here is how you can use the AddParameter extension method to add parameters to a List<DbParameter>:
public class Example
{
public void AddParameters()
{
// For SQL Server
var parameters = new List<SqlParameter>();
parameters.AddParameter("@Id", DataAccessDbType.Int32, 1);
parameters.AddParameter("@Id", DataAccessDbType.Int32, 2);
// Use parameters with your database command
}
}
Contributions are welcome! If you'd like to add support for additional databases or improve the library, feel free to open a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.