visit
// without DI
public class OrderController : Controller
{
public ActionResult Post(Order order)
{
using (var dbContent = new MyDbContext())
{
dbContext.Orders.Add(order);
dbContent.SaveChanges();
return Ok();
}
}
public ActionResult Get()
{
using (var dbContext = new MyDbContext())
{
var orders = dbContext.Orders.ToList();
return new JsonResult(orders);
}
}
}
// with DI
public class OrderController : Controller
{
private readonly MyDbContext _dbContext;
public OrderController(MyDbContext dbContext)
{
_dbContext = dbContext;
}
public ActionResult Post(Order order)
{
_dbContext.Orders.Add(order);
_dbContent.SaveChanges();
return Ok();
}
public ActionResult Get()
{
var orders = _dbContext.Orders.ToList();
return new JsonResult(orders);
}
}
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public RequestHandler(IApplicationDbContext dbContext)
{
DbContext = dbContext;
}
protected IApplicationDbContext DbContext { get; }
public abstract Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}
public MyRequestHandler : RequestHandler<MyRequest, MyResponse>
{
public MyRequestHandler(IApplicationDbContext dbContext) : base(dbContext) { }
public override Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
// handler logic
}
}
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public RequestHandler(IApplicationDbContext dbContext, ICurrentUser currentUser)
{
DbContext = dbContext;
CurrentUser = currentUser;
}
protected IApplicationDbContext DbContext { get; }
protected ICurrentUser CurrentUser { get; }
public abstract Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}
public MyRequestHandler : RequestHandler<MyRequest, MyResponse>
{
public MyRequestHandler(IApplicationDbContext dbContext, ICurrentUser currentUser) : base(dbContext, currentUser) { }
public override Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
// handler logic
}
}
public interface IDependencyAggregate
{
IApplicationDbContext DbContext { get; }
ICurrentUser { get; }
}
public class DependencyAggregate : IDependencyAggregate
{
public DependencyAggregate(IApplicationDbContext dbContext, ICurrentUser currentUser)
{
DbContext = dbContext;
CurrentUser = currentUser;
}
public IApplicationDbContext DbContext { get; }
public ICurrentUser CurrentUser { get; }
}
public abstract class RequestHandler<TRequest, TResponse> : IRequestHandler<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
public RequestHandler(IDependencyAggregate aggregate)
{
DbContext = aggregate.DbContext;
CurrentUser = aggregate.CurrentUser;
}
protected IApplicationDbContext DbContext { get; }
protected ICurrentUser CurrentUser { get; }
public abstract Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken);
}
public MyRequestHandler : RequestHandler<MyRequest, MyResponse>
{
public MyRequestHandler(IDependencyAggregate aggregate) : base(aggregate) { }
public override Task<MyResponse> Handle(MyRequest request, CancellationToken cancellationToken)
{
// handler logic
}
}
Previously published at