Skip to content

Instantly share code, notes, and snippets.

@sskset
Forked from thiagomajesk/AutoFacModule.cs
Created August 25, 2018 16:15
Show Gist options
  • Save sskset/782c0284af8cf3d444b9804fd4a2cdb5 to your computer and use it in GitHub Desktop.
Save sskset/782c0284af8cf3d444b9804fd4a2cdb5 to your computer and use it in GitHub Desktop.
Generic Handlers & Commands for MediatR (+ AutoFac config)
public class GenericHandlersModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<CreateCommandHandler<Foo, CreateFooCommand>>().As<IRequestHandler<CreateFooCommand, bool>>();
builder.RegisterType<DeleteCommandHandler<Foo, DeleteFooCommand>>().As<IRequestHandler<DeleteFooCommand, bool>>();
builder.RegisterType<ListQueryHandler<Foo, ListFooQuery>>().As<IRequestHandler<ListFooQuery, IEnumerable<Foo>>>();
builder.RegisterType<UpdateCommandHandler<Foo, UpdateFooCommand>>().As<IRequestHandler<UpdateFooCommand, bool>>();
}
}
public interface ICreateCommand<TEntity> : IRequest<bool> where TEntity : Entity, new() { }
public class CreateCommandHandler<TEntity, TCommand> : IRequestHandler<TCommand, bool>
where TEntity : Entity, new()
where TCommand : class, ICreateCommand<TEntity>, new()
{
private readonly DatabaseContext context;
private readonly IMapper mapper;
public CreateCommandHandler(DatabaseContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
public Task<bool> Handle(TCommand request, CancellationToken cancellationToken)
{
var entity = mapper.Map<TCommand, TEntity>(request);
var entities = context.Set<TEntity>().Add(entity);
var result = context.SaveChanges();
return Task.FromResult(result > 0);
}
}
public interface IDeleteCommand<TEntity> : IRequest<bool> where TEntity : Entity, new()
{
int Id { get; set; }
}
public class DeleteCommandHandler<TEntity, TCommand> : IRequestHandler<TCommand, bool>
where TEntity : Entity, new()
where TCommand : class, IDeleteCommand<TEntity>, new()
{
private readonly DatabaseContext context;
public DeleteCommandHandler(DatabaseContext context)
{
this.context = context;
}
public Task<bool> Handle(TCommand request, CancellationToken cancellationToken)
{
var entity = context.Set<TEntity>().Find(request.Id);
context.Set<TEntity>().Remove(entity);
var result = context.SaveChanges();
return Task.FromResult(result > 0);
}
}
public interface IListQuery<TEntity> : IRequest<IEnumerable<TEntity>> where TEntity : Entity, new()
{
string[] Includes { get; set; }
int Page { get; set; }
int PageSize { get; set; }
IEnumerable<TEntity> Entities { get; set; }
}
public class ListQueryHandler<TEntity, TQuery> : IRequestHandler<TQuery, IEnumerable<TEntity>>
where TEntity : Entity, new()
where TQuery : class, IListQuery<TEntity>, new()
{
private readonly DatabaseContext context;
private readonly IMapper mapper;
public ListQueryHandler(DatabaseContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
public Task<IEnumerable<TEntity>> Handle(TQuery request, CancellationToken cancellationToken)
{
request.Page = request.Page > 0 ? request.Page : 1;
request.PageSize = request.PageSize > 0 ? request.PageSize : 10;
var entites = context.Set<TEntity>()
.AsQueryable()
.AsNoTracking()
.Skip((request.Page - 1) * request.PageSize)
.Take(request.PageSize)
.AsEnumerable();
return Task.FromResult(entites);
}
}
public interface IShowQuery<out TEntity> : IRequest<TEntity> where TEntity : Entity, new()
{
int Id { get; set; }
}
public class ShowQueryHandler<TEntity, TQuery> : IRequestHandler<TQuery, TEntity>
where TEntity : Entity, new()
where TQuery : class, IShowQuery<TEntity>, new()
{
private readonly DatabaseContext context;
public ShowQueryHandler(DatabaseContext context)
{
this.context = context;
}
public Task<TEntity> Handle(TQuery request, CancellationToken cancellationToken)
{
var entity = context.Set<TEntity>().Find(request.Id);
return Task.FromResult(entity);
}
}
public interface IUpdateCommand<T> : IRequest<bool> where T : Entity, new()
{
int Id { get; set; }
}
public class UpdateCommandHandler<TEntity, TCommand> : IRequestHandler<TCommand, bool>
where TEntity : Entity, new()
where TCommand : class, IUpdateCommand<TEntity>, new()
{
private readonly DatabaseContext context;
private readonly IMapper mapper;
public UpdateCommandHandler(DatabaseContext context, IMapper mapper)
{
this.context = context;
this.mapper = mapper;
}
public Task<bool> Handle(TCommand request, CancellationToken cancellationToken)
{
var entity = mapper.Map<TCommand, TEntity>(request);
var entities = context.Set<TEntity>().Update(entity);
var result = context.SaveChanges();
return Task.FromResult(result > 0);
}
}
@JovanDodic
Copy link

Can you post entire code. Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment