Skip to content

Instantly share code, notes, and snippets.

@appkr
Forked from kellabyte/CQS_and_CQRS.cs
Created July 1, 2017 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appkr/722948636606d9deda89aabedf31a426 to your computer and use it in GitHub Desktop.
Save appkr/722948636606d9deda89aabedf31a426 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()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment