« Dapper » : différence entre les versions
Apparence
Aucun résumé des modifications |
Aucun résumé des modifications |
||
(6 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:.NET | [[Category:.NET Application]] | ||
= Links = | = Links = | ||
* [https://github.com/cornflourblue/dotnet-7-dapper-mysql-crud-api dotnet-7-dapper-mysql-crud-api] | * [https://github.com/cornflourblue/dotnet-7-dapper-mysql-crud-api dotnet-7-dapper-mysql-crud-api] | ||
* [https://dapper-plus.net Dapper Plus] | |||
* [https://tacta.io/en/news/generic-repository-pattern-using-dapper/20 Generic repository pattern using Dapper] | |||
* [https://github.com/moraleslarios/MoralesLarios.Data Generic Repository based in Dapper] | |||
= [https://www.c-sharpcorner.com/article/dapper-vs-entity-framework-core-vs-ado-net-which-one-should-you-choose/ Description] = | = [https://www.c-sharpcorner.com/article/dapper-vs-entity-framework-core-vs-ado-net-which-one-should-you-choose/ Description] = | ||
Dapper is ideal for scenarios where performance is critical and developers want fine-grained control over the database operations.<br> | Dapper is ideal for scenarios where performance is critical and developers want fine-grained control over the database operations.<br> | ||
Dapper is also easy to learn and use comparing to EF. | Dapper is also easy to learn and use comparing to EF. | ||
{{warn | No code factorization with Dapper like generic repository unless you create your own SQL generator.}} | |||
= Nuget packages = | = Nuget packages = | ||
Ligne 13 : | Ligne 17 : | ||
dotnet add package MySQLConnection # for MySqlConnection | dotnet add package MySQLConnection # for MySqlConnection | ||
</kode> | </kode> | ||
= Dependency Injection = | |||
<filebox fn='Program.cs'> | |||
var connectionString = builder.Configuration.GetConnectionString("MyDatabase"); | |||
builder.Services.AddSingleton<IDbConnectionFactory, MySqlConnectionFactory>( | |||
_ => new MySqlConnectionFactory(connectionString)); | |||
</filebox> | |||
<filebox fn='DataAccess/MySqlConnectionFactory.cs'> | |||
public class MySqlConnectionFactory : IDbConnectionFactory | |||
{ | |||
private readonly string connectionString; | |||
public MySqlConnectionFactory(string connectionString) | |||
{ | |||
this.connectionString = connectionString; | |||
} | |||
public IDbConnection CreateConnection() => new MySqlConnection(connectionString); | |||
} | |||
</filebox> | |||
<filebox fn='DataAccess/IDbConnectionFactory.cs' collapsed> | |||
public interface IDbConnectionFactory | |||
{ | |||
IDbConnection CreateConnection(); | |||
} | |||
</filebox> | |||
<filebox fn='Repositories/ItemRepository.cs'> | |||
using var dbConnection = dbConnectionFactory.CreateConnection(); | |||
var sql = """ | |||
select * from item | |||
"""); | |||
var items = await dbConnection.QueryAsync<Item>(sql); | |||
</filebox> |
Dernière version du 6 avril 2025 à 17:24
Links
- dotnet-7-dapper-mysql-crud-api
- Dapper Plus
- Generic repository pattern using Dapper
- Generic Repository based in Dapper
Description
Dapper is ideal for scenarios where performance is critical and developers want fine-grained control over the database operations.
Dapper is also easy to learn and use comparing to EF.
![]() |
No code factorization with Dapper like generic repository unless you create your own SQL generator. |
Nuget packages
dotnet add package Dapper
dotnet add package MySQLConnection # for MySqlConnection
|
Dependency Injection
Program.cs |
var connectionString = builder.Configuration.GetConnectionString("MyDatabase");
builder.Services.AddSingleton<IDbConnectionFactory, MySqlConnectionFactory>(
_ => new MySqlConnectionFactory(connectionString));
|
DataAccess/MySqlConnectionFactory.cs |
public class MySqlConnectionFactory : IDbConnectionFactory
{
private readonly string connectionString;
public MySqlConnectionFactory(string connectionString)
{
this.connectionString = connectionString;
}
public IDbConnection CreateConnection() => new MySqlConnection(connectionString);
}
|
DataAccess/IDbConnectionFactory.cs |
public interface IDbConnectionFactory
{
IDbConnection CreateConnection();
}
|
Repositories/ItemRepository.cs |
using var dbConnection = dbConnectionFactory.CreateConnection();
var sql = """
select * from item
""");
var items = await dbConnection.QueryAsync<Item>(sql);
|