Controllers
In the controller, you will find the PostController class in which has two methods first is Get () and the second is SavePost() method. You can see the Save method is implemented as a command, and the second one the Get method is implemented as a query.
The Code of the controller class is below.
[ApiController]
public class PostsController :ControllerBase
{
private readonlyIQueriesService _queries;
private readonlyICommandService _commands;
public PostsController(IQueriesService queries, ICommandService commands)
{
_queries = queries ?? throw new ArgumentNullException(nameof(queries));
_commands = commands ?? throw new ArgumentNullException(nameof(commands));
}
// GET api/values
[HttpGet]
public asyncTask Get()
{
return (await _queries.GetAllPostId()).ToList();
}
// POST api/values
[HttpPost]
public void SavePost([FromBody] SavePostDto value)
{
_commands.SavePost(value.Title, value.Body);
}
}
As you see, we build with constructor the query service and command service are done in this controller. Initially first we configure ASP.NET core dependency injection framework to handle all related situations.
In this controller the Get method calls the query service and savepost method calls the command service.As we know a query is do operation only Read-Only and other side command is do action which is modifies the state of system.
Now let's see how this service are implemented.