Domain driven design- checking rule on fetch data

Viewed 80

I used CQRS pattern with DDD,
I have some queries written using Dapper.
before fetching data I need to checking some rules.
my main question: this rules must be handle with Domain Model? (that Domain Model fetched from the Repository)?
for example:
I want to fetch all payments transactions of an specific "financial group", before fetching data, must be check that current user be one of members the "financial group".
now this checking must be handle with my domain model ("financial group")?
then in my query handler(CQRS pattern) I must be inject my Repository for get domain model? I thought must be load domain model just for the Commands? and for the Queries must not load domain model.

1 Answers

I'll first address an issue about you saying that you "must do" something because some pattern tells you to. The right question to ask here is "should I do it"? To answer that, you yourself must evaluate the pros and cons of the approach.

As you are using Dapper and doing all this on the query side, it would be difficult and unnecessary to use a repository tied to a specific aggregate root.

Based on my understanding of your model, it could be as simple as something like this:

Validate that the user performing the query is a member of a specific financial group (authorization, done in code)

If the user can have access to the specific financial group -> Perform a query with dapper, something like:

SELECT *
FROM PaymentTransactionsReadTable pt
WHERE pt.FinancialGroup = @FinancialGroupParameter

If no, return a non-authorized result, exception, etc.

Remember that the query you are performing with Dapper can span multiple aggregates, do joins, cross apply's, etc as it is in a sense your QUERY from CQRS.

Related