Domain-Driven-Design based repository pattern for Dapper.
$ dotnet add package Dapper.DDD.Repository.DependencyInjectionThis package provides support for using Dapper.DDD.Repository with the built-in Dependency Injection in .NET.
After adding this package, you can use the methods
On an IServiceCollection to inject all the repositories you're going to use.
Typically this will be available in Program.cs, though I suggest moving the actual Dapper configuration to your
infrastructure layer, similar to this:
public static IServiceCollection AddInfrastructure(this IServiceCollection services, string connectionString)
{
return services.ConfigureDapperRepositoryDefaults(config =>
{
config.QueryGeneratorFactory = new SqlQueryGeneratorFactory();
config.ConnectionFactory = new SqlConnectionFactory(connectionString);
config.DapperInjectionFactory = new DapperInjectionFactory();
config.Schema = "dbo";
})
.AddTableRepository<WeatherForecast, long>(config =>
{
config.HasKey(weatherForecast => weatherForecast.Id);
config.HasIdentity(weatherForecast => weatherForecast.Id);
config.TableName = "WeatherForecasts";
config.HasDefault(weatherForecast => weatherForecast.Timestamp);
})
.AddTableRepository<WeatherStation, int, IWeatherStationRepository, WeatherStationRepository>(config =>
{
config.HasKey(weatherStation => weatherStation.Id);
config.HasIdentity(weatherStation => weatherStation.Id);
config.TableName = "WeatherStations";
})
.AddViewRepository<WeatherForecastView, IWeatherForecastViewRepository, WeatherForecastViewRepository>(config =>
{
config.ViewName = "WeatherForecastView";
});
}
And then simply calling builder.Services.AddInfrastructure from Program.cs.
Auto generated documentation via DocFx is available here: https://steffenskov.github.io/Dapper.DDD.Repository/