Reputation: 6865
I wonder using design principle rule that named DRY(Do not repeat yourself) on CQRS.
I have 2 type of Geometric table named WaterPipes, GasPipes.
public GasPipe{ id, Name, Length, .... }
public WaterPipe{ id, Name, Length .... }
So I am new at using CQRS. I want to create a query to get total length of them.
Database query will be similar.
Select Sum(Length) From GasPipes,
Select Sum(Length) From WaterPipes
So now can I create one Query?
public class PipeLengthQuery { }
public class PipeLengthQueryHandler {
if(water) Select Sum(Length) From WaterPipes
if(gaz) Select Sum(Length) From GasPipes
}
Or separate query types:
public class WaterPipeLengthQuery { }
public class GasPipeLengthQuery { }
public class GasPipeLengthQueryHandler { }
public class WaterPipeLengthQueryHandler { }
Which of it best?
Upvotes: 3
Views: 284
Reputation: 43718
Well, if the separate query types would lead to an explosion of classes, I guess you could try to find a more generic way.
It's hard to tell you exactly how it should be done, but having if
statements within the query handlers is probably not right. It violates the Open-closed principle and would lead to a good deal of code duplication if you were to introduce new queries, such as PipeDiameterQuery
.
What you could do is have a class that encapsulates the actual schema of your statistical read model for every type of pipe and have your query handlers work off those schemas.
The client would pass what type of pipe he wants statistics for and the query handler's could look like (pseudo-code):
schema = PipeStatisticalSchema.fromPipeType(clientSuppliedPipeType);
SELECT SUM(Length) FROM schema.Table
Preferably, it would be nice if the schema
would be resolved already before reaching the query handler, so that it just get's injected in it.
Upvotes: 1
Reputation: 572
If all these data should be represent in one view, then create one query, otherwise separate it.
Upvotes: 0