Skip to content

Instantly share code, notes, and snippets.

@kellabyte
Created March 3, 2012 03:19
Show Gist options
  • Save kellabyte/1964094 to your computer and use it in GitHub Desktop.
Save kellabyte/1964094 to your computer and use it in GitHub Desktop.
CQS_and_CQRS
// 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()
}
@jbrains
Copy link

jbrains commented Aug 22, 2012

Interfaces, surely! (Couldn't resist.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment