Dependency injection (DI) is a wonderful thing. Simply add your dependency as a parameter to the constructor (most commonly) of your class, register it with you DI container, and away you go - the DI container will manage the rest. Some of the key benefits of DI are: greater testability, greater maintainability, and greater reusability. : { { ( dbContent = MyDbContext()) { dbContext.Orders.Add(order); dbContent.SaveChanges(); Ok(); } } { ( dbContext = MyDbContext()) { orders = dbContext.Orders.ToList(); JsonResult(orders); } } } : { MyDbContext _dbContext; { _dbContext = dbContext; } { _dbContext.Orders.Add(order); _dbContent.SaveChanges(); Ok(); } { orders = _dbContext.Orders.ToList(); JsonResult(orders); } } // without DI public class OrderController Controller ActionResult ( ) public Post Order order using var new return ActionResult ( ) public Get using var new var return new // with DI public class OrderController Controller private readonly ( ) public OrderController MyDbContext dbContext ActionResult ( ) public Post Order order return ActionResult ( ) public Get var return new However, I recently came across a use case where DI can be a real pain - dependency injection in inherited classes. The Problem I have recently been working a lot with the package, using it's request/request handler pattern to issue commands in the system (inspired by ). Mediatr Jason Taylor's Clean Architecture solution I typically create a RequestHandler base class that contains common dependencies and functionality. Each concrete request handler can then inherit from this base class. < , > : < , > : < > { { DbContext = dbContext; } IApplicationDbContext DbContext { ; } ; } MyRequestHandler : RequestHandler<MyRequest, MyResponse> { { } { } } public abstract class RequestHandler TRequest TResponse IRequestHandler TRequest TResponse where TRequest IRequest TResponse ( ) public RequestHandler IApplicationDbContext dbContext protected get Task<TResponse> ( ) public abstract Handle TRequest request, CancellationToken cancellationToken public ( ) : ( ) public MyRequestHandler IApplicationDbContext dbContext base dbContext Task<MyResponse> ( ) public override Handle MyRequest request, CancellationToken cancellationToken // handler logic The problem comes when I want to add more dependencies to the base class. Now I have to go through to every single concrete request handler and update the constructor to take the new dependency as well. Fortunately, the code will not compile if I miss one, so there is no risk of a runtime error, but it is still incredibly tedious work to have to update every single request handler. Also, you can end up with very large constructors, which obscures the intention of the class. < , > : < , > : < > { { DbContext = dbContext; CurrentUser = currentUser; } IApplicationDbContext DbContext { ; } ICurrentUser CurrentUser { ; } ; } MyRequestHandler : RequestHandler<MyRequest, MyResponse> { { } { } } public abstract class RequestHandler TRequest TResponse IRequestHandler TRequest TResponse where TRequest IRequest TResponse ( ) public RequestHandler IApplicationDbContext dbContext, ICurrentUser currentUser protected get protected get Task<TResponse> ( ) public abstract Handle TRequest request, CancellationToken cancellationToken public ( ) : ( ) public MyRequestHandler IApplicationDbContext dbContext, ICurrentUser currentUser base dbContext, currentUser Task<MyResponse> ( ) public override Handle MyRequest request, CancellationToken cancellationToken // handler logic The Solution - Dependency Aggregates The solution to this problem is really quite simple. Rather than injecting the dependencies directly, create a new class that contains the dependencies (known as an aggregate) and inject that instead. { IApplicationDbContext DbContext { ; } ICurrentUser { ; } } : { { DbContext = dbContext; CurrentUser = currentUser; } IApplicationDbContext DbContext { ; } ICurrentUser CurrentUser { ; } } < , > : < , > : < > { { DbContext = aggregate.DbContext; CurrentUser = aggregate.CurrentUser; } IApplicationDbContext DbContext { ; } ICurrentUser CurrentUser { ; } ; } MyRequestHandler : RequestHandler<MyRequest, MyResponse> { { } { } } public interface IDependencyAggregate get get public class DependencyAggregate IDependencyAggregate ( ) public DependencyAggregate IApplicationDbContext dbContext, ICurrentUser currentUser public get public get public abstract class RequestHandler TRequest TResponse IRequestHandler TRequest TResponse where TRequest IRequest TResponse ( ) public RequestHandler IDependencyAggregate aggregate protected get protected get Task<TResponse> ( ) public abstract Handle TRequest request, CancellationToken cancellationToken public ( ) : ( ) public MyRequestHandler IDependencyAggregate aggregate base aggregate Task<MyResponse> ( ) public override Handle MyRequest request, CancellationToken cancellationToken // handler logic Now if I want to add a new dependency, the only places that I need to change the code are in the DependencyAggregate class and the RequestHandler base class (I don't need to make any changes to the inherited classes). Conclusion In this post I have described a simple method for managing dependency injection in inherited classes, by creating a dependency aggregate class to inject into the base class. This ensures that new dependencies can easily be introduced with having to make changes to every inherited class. I post mostly about full stack .NET and Vue web development. To make sure that you don't miss out on any posts, please follow this blog and . If you found this post helpful, please like it and share it. You can also find me on . subscribe to my newsletter Twitter Previously published at https://samwalpole.com/handling-dependency-injection-in-inherited-classes