Around four and a half years ago, I started a project at my work with the goal of it becoming our admin system and to remove our painfully expensive dependency on Salesforce. When I started the project, I applied a layered design to the architecture because I thought it was going to be robust and scalable, and everyone was doing it. After some time, it started to become clear that it was anything but that. Time was precious and I had to commit to the architecture.
The solution was split into 10 different projects. Some were utilities or extensions, and others were the core of the system. Those core projects were very tightly coupled to each other and were very rigid. Changes needed to propagate all the way through all the layers and sometimes they didn't quite fit or feel right. Slowly, but surely, the system started to become difficult to maintain, and almost impossible to add new features to.
As I kept going with this monster I had created, I happened to read an article by Jimmy Bogard about the Vertical Slices architecture. I "understood" it, but it didn't become very clear until I watched the recording of a talk he did about the architecture. There's a more recent talk he did on the architecture that I found as I was preparing my material for this post. In the talks he shows how MediatR came about and how to use it to implement a Vertical Slices architecture, so I decided to give it a shot since the layered architecture I was using wasn't going to get any better.
I had some trial and error periods until I started to get the hang of it and morph my thought process to the new architecture. While I started to get some improvements, I also started to run into issues with the way I was querying the database. Jimmy came to the rescue again with another article where he spoke of Entity Framework Plus and its future queries.
After implementing EFP the load on the database improved, but it wasn't quite enough. You see, I was carrying over a relic from the old architecture where I had set up massive projections in AutoMapper to literally project into a complete View. The more data I was trying to query out into a view, the larger and more bloated the queries became. Entity Framework didn't help with the bloat that it generates, but I was easily having queries that were 200+ lines long.
I thought about how to improve the situation and realized that the answer was actually really simple, just break it up into smaller queries. The query into a view "pattern" came about because I was trying to be efficient when querying the database. With EFP in the picture now, I could accomplish the same thing, without having bloated, complex queries.
After breaking up the queries I saw massive improvements, but I also noticed that the projection models didn't always match the view models. This is how what I call the Projection-View pattern came to be. I split the query handlers into two mappings. First, I projected the data from the database into DTOs, then, I mapped those projections to the final view models. Sometimes the projection and view models were the same, but quite often they were not.
So, what does the Projection-View Pattern Look Like?
Let's see if I can make this make more sense with some code examples. First, I introduced a couple of base classes:
ProjectionBase
, which was responsible for holding onto the projection models.ViewBase
, which was responsible for the final view models.
- Technically, I already had this from long ago, but it was serving a new more proper purpose.
QueryHandlerBase<TQuery, TProjection, TView>
, which was the new handler for the queries and would perform the mapping of projections and views.
Here are the actual classes.
public abstract class ProjectionBase {
}
public abstract class ViewBase {
}
public abstract class QueryHandlerBase<TQuery, TProjection, TView> :
HandlerBase<TQuery, TView>
where TQuery : IRequest<TView>
where TProjection : ProjectionBase, new()
where TView : ViewBase {
protected QueryHandlerBase(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override TView Handle(
TQuery query) => GetView(query);
protected virtual TProjection GetProjection(
TQuery query) => new TProjection();
protected virtual TView GetView(
TQuery query) {
var projection = GetProjection(query);
var view = Mapper.Map<TView>(projection);
Normalize(projection, view);
return view;
}
protected virtual void Normalize(
TProjection projection,
TView view) {
}
}
The QueryHandlerBase<TQuery, TProjection, TView>
class inherits from HandlerBase<TRequest, TResponse>
class because it contains the MyDbContext
and IMapper
properties. The HandlerBase
class is also inherited by all command handlers for this same reason.
What About a Real Code Example?
So, how would we use the above QueryHandlerBase
then? Let's pretend we have an admin system (built on ASP.NET Core) for a construction company and we need to add, edit and list jobs. Our Add
, Edit
and List
slices would look like this:
Add
public sealed class Add {
public sealed class Command :
IRequest<bool> {
public int? CsrId { get; set; }
public string Name { get; set; }
public int? StateId { get; set; }
public int? StatusId { get; set; }
public int? TypeId { get; set; }
}
public sealed class CommandHandler :
HandlerBase<Command, int> {
public CommandHandler(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override void Handle(
Command command) {
var job = Mapper.Map<Job>(command);
Context.Add(job);
Context.SaveChanges();
return job.Id;
}
public sealed class Query :
IRequest<View> {
}
public sealed class QueryHandler :
QueryHandlerBase<Query, Projection, View> {
public QueryHandler(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override Projection GetProjection(
Query query) {
var countries = Context.Countries.OrderByDescending(
c => c.Name).ProjectTo<SelectListGroup>(MapperConfig).Future();
var projection = base.GetProjection(query);
projection.CsrsSelectListItems = Context.Employees.Where(
e => e.IsActive).OrderBy(
e => e.Name).ProjectTo<SelectListItem>(MapperConfig).Future();
projection.StatesSelectListItems = Context.States.Where(
s => s.IsActive).OrderBy(
s => s.Name).ProjectTo<SelectListItem>(MapperConfig, new {
countries
}).Future();
projection.StatusesSelectListItems = Context.Statuses.Where(
s => s.IsActive).OrderBy(
s => s.Name).ProjectionTo<SelectListItem>(MapperConfig).Future();
projection.TypesSelectListItems = Context.Types.Where(
t => t.IsActive).OrderBy(
t => t.Name).ProjectTo<SelectListItem>(MapperConfig).Future();
return projection;
}
}
public sealed class Projection :
ProjectionBase {
public QueryFutureEnumerable<SelectListItem> CsrsSelectListItems { get; set; }
public QueryFutureEnumerable<SelectListItem> StatesSelectListItems { get; set; }
public QueryFutureEnumerable<SelectListItem> StatusesSelectListItems { get; set; }
public QueryFutureEnumerable<SelectListItem> TypesSelectListItems { get; set; }
}
public sealed class View :
ViewBase {
public IList<SelectListItem> CsrsSelectListItems { get; set; }
public Command Job { get; } = new Command();
public IList<SelectListItem> StatesSelectListItems { get; set; }
public IList<SelectListItem> StatusesSelectListItems { get; set; }
public IList<SelectListItem> TypesSelectListItems { get; set; }
}
}
}
Edit
public sealed class Edit {
public sealed class Command :
IRequest<bool> {
public int? CsrId { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public int? StateId { get; set; }
public int? StatusId { get; set; }
public int? TypeId { get; set; }
}
public sealed class CommandHandler :
HandlerBase<Command, bool> {
public CommandHandler(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override void Handle(
Command command) {
var job = Context.Jobs.SingleOrDefault(
j => j.Id == command.Id);
if (job is null) {
return false;
}
Mapper.Map(command, job);
Context.SaveChanges();
return true;
}
public sealed class Query :
IRequest<View> {
public int Id { get; set; }
}
public sealed class QueryHandler :
QueryHandlerBase<Query, Projection, View> {
public QueryHandler(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override Projection GetProjection(
Query query) {
var countries = Context.Countries.OrderByDescending(
c => c.Name).ProjectTo<SelectListGroup>(MapperConfig).Future();
var projection = base.GetProjection(query);
projection.CsrsSelectListItems = Context.Employees.Where(
e => e.IsActive).OrderBy(
e => e.Name).ProjectTo<SelectListItem>(MapperConfig).Future();
projection.Job = Context.Jobs.Where(
j => j.Id == query.Id).ProjectTo<Command>(MapperConfig).DeferredSingle().FutureValue();
projection.StatesSelectListItems = Context.States.Where(
s => s.IsActive).OrderBy(
s => s.Name).ProjectTo<SelectListItem>(MapperConfig, new {
countries
}).Future();
projection.StatusesSelectListItems = Context.Statuses.Where(
s => s.IsActive).OrderBy(
s => s.Name).ProjectionTo<SelectListItem>(MapperConfig).Future();
projection.TypesSelectListItems = Context.Types.Where(
t => t.IsActive).OrderBy(
t => t.Name).ProjectTo<SelectListItem>(MapperConfig).Future();
return projection;
}
}
public sealed class Projection :
ProjectionBase {
public QueryFutureEnumerable<SelectListItem> CsrsSelectListItems { get; set; }
public QueryFutureValue<Command> Job { get; set; }
public QueryFutureEnumerable<SelectListItem> StatesSelectListItems { get; set; }
public QueryFutureEnumerable<SelectListItem> StatusesSelectListItems { get; set; }
public QueryFutureEnumerable<SelectListItem> TypesSelectListItems { get; set; }
}
public sealed class View :
ViewBase {
public IList<SelectListItem> CsrsSelectListItems { get; set; }
public Command Job { get; set; }
public IList<SelectListItem> StatesSelectListItems { get; set; }
public IList<SelectListItem> StatusesSelectListItems { get; set; }
public IList<SelectListItem> TypesSelectListItems { get; set; }
}
}
}
List
public sealed class List {
public sealed class Query :
IRequest<View> {
}
public sealed class QueryHandler :
QueryHandlerBase<Query, Projection, View> {
public QueryHandler(
MyDbContext context,
IMapper mapper) :
base(context, mapper) {
}
protected override Projection GetProjection(
Query query) {
var projection = base.GetProjection(query);
projection.Jobs = Context.Jobs.OrderBy(
j => j.Name).ProjectTo<JobProjectionView>(MapperConfig).Future();
return projection;
}
}
public sealed class Projection :
ProjectionBase {
public QueryFutureEnumerable<JobProjectionView> Jobs { get; set; }
}
public sealed class View :
ViewBase {
public IList<JobProjectionView> Jobs { get; set; }
}
public sealed class JobProjectionView {
public string CsrName { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string StateName { get; set; }
public string StatusName { get; set; }
public string TypeName { get; set; }
}
}
The Add
and Edit
slices are quite similar but there are subtle differences that change the projection requirements. I don't use the Normalize()
method in this example, but it exists for the few times when you have to manually intervene when mapping from a projection to a view. In the linked video I go into a bit more detail by taking a starter MVC app and morphing it to use the Projection-View pattern.
Concluding the Pattern
That pretty much covers the Projection-View pattern. All I've done is slightly expanded on Jimmy's Vertical Slices architecture and split the projections and views into different models. In my project at work, I have probably 200 or more slices using this pattern so I'm quite confident that it's proven itself. I do have a series planned in the near future where I demonstrate this pattern as well as other techniques in a more functional and realistic example.
I hope I've done a good job explaining the Projection-View pattern and how it can be beneficial to you. I would appreciate it if you watched the linked video and, if you think it was helpful, please leave a like on it. Thanks!
Source Code
The source code can be found here.