I am separating my query and command on service side like this:
public class ProductCommandService{
void AddProduct(Product product);
}
public interface ProductQueryService{
Product GetProduct(Guid id);
Product[] GetAllProducts();
}
Command Query Separation accepts that a method should change state or return a result. There is no problem.
public class ProductController: ApiController{
private interface ProductCommandService commandService;
private interface ProductQueryService queryService;
[HttpPost]
public ActionResult Create(Product product){
commandService.AddProduct(product);
return ???
}
[HttpGet]
public Product GetProduct(Guid id){
return commandService.GetProduct(id);
}
[HttpGet]
public Product[] GetAllProducts(){
return queryService.GetAllProducts();
}
}
I am applying command query separation on service side but not applying in controller class. Because user may want to see created product result. But commandService works in Create Controller Action metod and does not return created product.
What will we return to user? AllProducts? Will CQS applly about application lifecycle?
In such scenario I usually go with generating new entity Ids on the client. Like this:
This way you are also following POST-REDIRECT-GET rule which you should do even when not using CQRS.
EDITED: Sorry, did not notice that you are building an API, not MVC application. In this case I would return a URL to newly created Product: