vendredi 13 avril 2018

Commands vs. Queries when modifying and returning data

I'm having a bit of a design conundrum. In our project, we have Commands and Queries. In general, Commands are methods that alter data in some way on the database. Queries are methods that return data but don't alter it.

Matching this, our project has Command and Query classes, along with CommandHandler and QueryHandler classes. So our data layer ends up being a series of these, and it's held up pretty well for the most part in separating which database operations modify the database and which don't.

This seems pretty straightforward.

However, what happens when you need to alter data somehow, then return something? I'm not entirely sure where it fits in to the Command/Query dichotomy. Should I be creating a Query for this, or a Command?

Is there a best practice for this, or even a commonly-accepted method of handling it?

Examples

Command

INSERT INTO MyTable VALUES (Id, SomeValue, SomeOtherValue, CreatedDate)

Query

SELECT Id, SomeValue FROM MyTable

Combined

INSERT INTO MyTable VALUES (Id, SomeValue, SomeOtherValue, CreatedDate);
SELECT TOP 1 Id FROM MyTable ORDER BY CreatedDate DESC;

Aucun commentaire:

Enregistrer un commentaire