LingForCC / MyTodoList

5 stars 1 forks source link

Have a try to use CQRS arch design? #23

Open kafka-yu opened 5 years ago

kafka-yu commented 5 years ago

Currently, we use Service/Repository architecture, it is ok for now and for a short long time.

But considering for long-term development and features growing, perhaps it could not handle them very well. Let's have a try to use CQRS (https://martinfowler.com/bliki/CQRS.html) design pattern? Then we separate query and operation into their own bus- IQueryBus, and ICommandBus. In API controller, we send a query to IQueryBus, it returns data we want. we send a command to ICommandBus, which will handle CUD actions, and then return the response.

Take Task controller for example: 1.[GET] /api/task/1

var query = new GetTaskQuery() { 
 Id = 1,
 UserId = currentUserId, // just need to modify the query when we need to filter by userid,
};
var res = await _queryBus.SendAsync(query);
  1. [POST] /api/task { name: 'buy books' }
var createTaskCmd = new CreateTaskCommand() { 
 Name = name,
};
 await _commandBus.SendAsync(createTaskCmd );

var res = createTaskCmd.Result;
LingForCC commented 5 years ago

Does it mean that we don't need a Task class?

kafka-yu commented 5 years ago

No need TaskService class, still need Task class

LingForCC commented 5 years ago

I think we can try it out. But I think there is no need to refactor the current code to this model at the moment. We can apply this paradigm in the next controller or model we are going to implement. What do you think?

kafka-yu commented 5 years ago

Yeah that’s try it in next stage