DEV Community

Cover image for Dapper & CQRS

Dapper & CQRS

Rick Hopkins on August 11, 2021

Introduction In 2016 the company I work for launched a major rewrite of our main software suite. We are an education company and so the ...
Collapse
 
suicodave profile image
Dave Suico

Great work!

However, I'm wondering why do queries have to be wrapped in a facade and then in a service? See I've been writing an app using a project template they called Clean Architecture recently and then I learned that queries and commands are called in controllers directly and no more wrappers around. Your thoughts on this matter is greatly appreciated!

Collapse
 
rickhopkins profile image
Rick Hopkins Melodic Development

Thought I had responded to this, but must have forgot. So we wrap them in a facades for a few reasons. First, we find it useful to group like queries and commands related to either a business area or data entity. Second, for testing we find it easier to mock up without having to directly new up a query. Third, we use separate database contexts for read and write to our database. In our services we often create complex transactions to perform multiple operations all in one transaction against the same database context. Also, related to the second reason, when doing these bigger transactions calling multiple queries or commands its nice to have them all available in one place instead of having to bring in each query separately. Facades probably aren't necessary, but we've found benefits to having them in regard to the reasons I've listed. There are probably more benefits I'm not thinking of right at the moment. A draw back is that it is just one more piece in the pipeline to create and remember to piece together. We also have debates on our team all the time about what queries and commands should be grouped together into a facade and what makes sense. Hope this helps.