CQRS stands for Command Query Responsibility Segregation and the basic rules are:
Commands - Perform (side effects) actions on the system and don't return values.
Queries - Return values but can't touch the system's state.
Idealy, you don't mix them. Hence you can say, commands are dangerous, and
queries are safe.
Now, ruby doesn't have a specific type to say: this method (command) returns nothing, like void in C and other languages. So, the question is: How do you state that a particular method returns nothing?
Based on comments from the ruby mailing list, people are using these approaches;
1. Return self.
2. Return nil.
3. Implicit return (which in general, ends up being nil).
If I'd have to pick one, I'd go with the last one, but I also like to throw
another option into the mix....
Why don't create a class for this particular use case? Let's say, Nothing!
module Nothing
class Nothing
end
def nothing
@nothing ||= Nothing.new
end
end
class CommandQueryApi
include Nothing
# By using the nothing helper, is clear that the method returns, nothing!
def cmd_foo
# Do something
nothing
end
end
I 've used this approach a couple of times, and at least to me and my team, when you look at the code, is pretty clear that the method returns, well, nothing ;)
Not sure is this is the ruby way, but has being working so far.
Commands - Perform (side effects) actions on the system and don't return values.
Queries - Return values but can't touch the system's state.
Idealy, you don't mix them. Hence you can say, commands are dangerous, and
queries are safe.
Now, ruby doesn't have a specific type to say: this method (command) returns nothing, like void in C and other languages. So, the question is: How do you state that a particular method returns nothing?
Based on comments from the ruby mailing list, people are using these approaches;
1. Return self.
2. Return nil.
3. Implicit return (which in general, ends up being nil).
If I'd have to pick one, I'd go with the last one, but I also like to throw
another option into the mix....
Why don't create a class for this particular use case? Let's say, Nothing!
module Nothing
class Nothing
end
def nothing
@nothing ||= Nothing.new
end
end
class CommandQueryApi
include Nothing
# By using the nothing helper, is clear that the method returns, nothing!
def cmd_foo
# Do something
nothing
end
end
I 've used this approach a couple of times, and at least to me and my team, when you look at the code, is pretty clear that the method returns, well, nothing ;)
Not sure is this is the ruby way, but has being working so far.
Nice article, thanks for the information. You give me some idea's. I will bookmark for next reference.
ReplyDeleteLearn more Cloud Contact Centers
Wow interesting! !!first i was read this article i was amaze,hope many readers you may help.. Goodluck!!
ReplyDeleteHorseshoe Bay Real Estate
thanks for this really informative
ReplyDeleteBlackCloud
Thanks for sharing wonderful information of giving best information. Its more useful and more helpful. Great doing keep sharing.
ReplyDeletewait till you see Shoretel Resellers
Lovely! It's helpful.
ReplyDeleteVery interesting. I'll share this information.
ReplyDeleteI hope to see more of it.
ReplyDeleteCool!
ReplyDeleteUnderstanding software is a genius thing.
ReplyDeleteJust got an idea. Very helpful post.
ReplyDelete