vendredi 18 mai 2018

CQS pattern vs output variable

The Command Query Separation pattern states that it should be obvious whether a method is a command or a query (I have the simple principle in mind, not the CQRS with Event Sourcing etc.)

A command therefore has to be void, whereas a query has to return a value.

public interface IRepository<T>
{
    void Create(Guid id, T item);
    int GetHumanReadableId(Guid id);
    // other members
}

This is discussed for example in this Mark Seemann's post: http://blog.ploeh.dk/2014/08/11/cqs-versus-server-generated-ids/

I am wondering where does an output variable approach land? Does it violate the CQS?

What I have in mind is:

public interface IRepository<T>
{
    void Create(T item, out int humanReadableId);    
    // other members
}

This seems to keep the method void, thus indicating a command, while still allowing 'getting' some output from it without separate explicit query - after all, additional explicit query like int GetHumanReadableId(Guid id); means 1) more code to write and maintain, 2) additional database call.

Aucun commentaire:

Enregistrer un commentaire