A repository pattern implementation for Dapper, a high-performance Micro-ORM supporting SQL Server, MySQL, Sqlite, SqlCE, Firebird etc...
This example is shown using Autofac since this is the go-to IoC for us.
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new DapperDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("SqlConnectionString"))).As<IDapperDefaultProvider>().SingleInstance();
}
}
public class DatabaseModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.Register(c => new DapperDefaultProvider(c.Resolve<IConfiguration>().GetConnectionString("SqlConnectionString"), new MemoryCacheOptions
{
CompactionPercentage = 0.25,
ExpirationScanFrequency = TimeSpan.FromMinutes(5),
SizeLimit = 1024
})).As<IDapperDefaultProvider>().SingleInstance();
}
}
Repository
CachedRepository
ReadOnlyRepository
public interface IRepository<TEntity>
{
IEnumerable<TEntity> List();
Task<IEnumerable<TEntity>> ListAsync();
IEnumerable<TEntity> Where(string conditions);
Task<IEnumerable<TEntity>> WhereAsync(string conditions);
TEntity Get(int id);
Task<TEntity> GetAsync(int id);
IEnumerable<TEntity> Get(int[] ids);
Task<IEnumerable<TEntity>> GetAsync(int[] ids);
TEntity Insert(TEntity entity);
Task<TEntity> InsertAsync(TEntity entity);
void Insert(IEnumerable<TEntity> entities);
Task InsertAsync(IEnumerable<TEntity> entities);
TEntity Update(TEntity entity);
Task<TEntity> UpdateAsync(TEntity entity);
void Update(IEnumerable<TEntity> entities);
Task UpdateAsync(IEnumerable<TEntity> entities);
void Delete(TEntity entity);
Task DeleteAsync(TEntity entity);
void Delete(IEnumerable<TEntity> entities);
Task DeleteAsync(IEnumerable<TEntity> entities);
}
public interface IUserRepository : IRepository<Models.User>
{
}
public class UserRepository : Repository<Models.User>, IUserRepository
{
public UserRepository(IDapperDefaultProvider provider)
: base(provider)
{
}
}
public interface IUserRepository : IRepository<Models.User>
{
}
public class UserRepository : Repository<Models.User>, IUserRepository
{
public UserRepository(IDapperDefaultProvider provider)
: base(provider)
{
}
public override Models.User Get(int id)
{
var cacheKey = "_Id_" + id;
var item = CacheGet<Models.User>(cacheKey);
if (item != null)
return item;
using (var connection = new SqlConnection(ConnectionString))
{
var sql = $@"
SELECT * FROM [{TableName}]
WHERE Id = @id";
var parameters = new
{
id
};
var result = connection.QueryFirstOrDefault<Models.User>(sql, parameters);
CacheAdd(cacheKey, result);
return result;
}
}
}