Created
March 3, 2012 03:19
-
-
Save kellabyte/1964094 to your computer and use it in GitHub Desktop.
CQS_and_CQRS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CQS (Command-Query Separation) | |
// Bertrand Meyer devised the CQS principle | |
// It states that every method should either be a command that performs an action, or a | |
// query that returns data to the caller, but not both. In other words, asking a question | |
// should not change the answer. More formally, methods should return a value only if they | |
// are referentially transparent and hence possess no side effects. | |
// | |
// Example: | |
public class CustomerService | |
{ | |
// Commands | |
void MakeCustomerPreferred(CustomerId) | |
void ChangeCustomerLocale(CustomerId, NewLocale) | |
void CreateCustomer(Customer) | |
void EditCustomerDetails(CustomerDetails) | |
// Queries | |
Customer GetCustomer(CustomerId) | |
CustomerSet GetCustomersWithName(Name) | |
CustomerSet GetPreferredCustomers() | |
} | |
// CQRS (Command-Query Responsibility Segregation) | |
// Defined by Greg Young. | |
// CQRS is simply the creation of two objects where there was previously only one | |
// http://codebetter.com/gregyoung/2010/02/16/cqrs-task-based-uis-event-sourcing-agh/ | |
// | |
// Example: | |
public class CustomerWriteService | |
{ | |
// Commands | |
void MakeCustomerPreferred(CustomerId) | |
void ChangeCustomerLocale(CustomerId, NewLocale) | |
void CreateCustomer(Customer) | |
void EditCustomerDetails(CustomerDetails) | |
} | |
public class CustomerReadService | |
{ | |
// Queries | |
Customer GetCustomer(CustomerId) | |
CustomerSet GetCustomersWithName(Name) | |
CustomerSet GetPreferredCustomers() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interfaces, surely! (Couldn't resist.)