« Dapper » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 9 : | Ligne 9 : | ||
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 unless you create your own SQL generator.}} | {{warn | No code factorization with Dapper like generic repository unless you create your own SQL generator.}} | ||
= Nuget packages = | = Nuget packages = |
Dernière version du 13 août 2023 à 15:08
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); |