Search code examples
c#coding-styleseparation-of-concernscommand-query-separation

Command Query Separation violation


What do you think about

 if(!DoSomething()) return;

In Clean Code this is viewed as violation of Command Query Separation. But how can we understand if something in command DoSomething() went wrong? What about sql command (ex: void Delete(Table))? How can we know if that table existed?

Thanks.


Solution

  • I agree with the comments from rObiwahn that you should check CanDoSomething before issuing a command of DoSomething. In a pure CQRS environment, DoSomething would not return anything and if anything prevented Something from happening (not due to an exception, but a race condition or something else changing between CanDoSomething and DoSomething), your domain would issue a DoSomethingWasInvalid event (or something like that) which would allow your application to eventually become consistent.

    It may sound complex, but it really becomes pretty simple once you start breaking down the logic into small chunks and allow your application to embrace eventual consistency.

    There are a lot of good resources on the DDD/CQRS group in google groups. A question like 'How do you tell the sender that a command failed?' is similar to your question a bit. People like Udi Dahan, Greg Young, Rinat Abdullin and others monitor that group and provide some really great answers. I'd recommend checking that out every now and then, too.

    Hope this helps!