Building Microservices Through Event-Driven Architecture, Part 3: Presenters, Views, and Controllers

DZone 's Guide to

Building Microservices Through Event-Driven Architecture, Part 3: Presenters, Views, and Controllers

Find out how to implement the command side of Presenters, Views, and Controllers when building microservices with event-driven architecture.

· Microservices Zone ·
Free Resource

single lego

Learn how to build your microservices through event-driven architecture.
You may also like: Building Microservices With Event-Driven Architecture, Part 1: Application-Specific Business Rules

During this journey, I will implement the command side of Presenters, Views, and Controllers

In this step, I will implement the presentation. Here, the presentation is not the user interface but the Web API.

It is also possible to implement the repository first, before the presentation, the order does not matter.
Note that it will be necessary to configure the dependency injection later on.

IUnitOfWork and ISpeechRepository are injected into RegisterSpeechUseCase.

Therefore, IUnitOfWork and ISpeechRepository must be instantiated to get an instance of IRegisterSpeechUseCase.

code screenshot

Like this:

code screenshot

Since I am doing a mock of IRegisterSpeechUseCase, I still do not need a concrete implementation of IUnitOfWork and ISpeechRepository.

So I will configure the dependency injections when my repository is implemented and ready to use.
This is useful when one team is working on the presentation and another team is working on the repository. In this case, every team can advance on his side without waiting for other teams.

The presentation belongs to the interface adapter in the clean architecture.

Interface Adapters

The software in this layer is a set of adapters that convert data from the format most convenient for the use cases and entities to the format most convenient for some external agency such as the Database or the Web. It is this layer, for example, that will wholly contain the MVC architecture of a GUI. The Presenters, Views, and Controllers all belong in here.

The models are likely just data structures that are passed from the controllers to the use cases, and then back from the use cases to the presenters and views. I have to create an HttpPost action that accepts a dto, which contains information about the speech I want to create.

If this information is correct, I call the handle function of my use-case and I pass it as an argument this dto converted into command. If information into the dto is not correct I return BadRequest or 400. Then, use-case executes, and if everything is ok, I return 200 otherwise, I return 500.

My first test will be Register Speech With Invalid ModelState Return BadRequest.

The next step is to create a SpeechForCreationDto class and a SpeechController class with a post-action.

SpeechForCreationDto SpeechController.

My first test passes.

My second test will verify that _registerSpeechUseCase.Handle(command) is called only once and everything is alright.

To make the previous test pass, I have to call _registerSpeechUseCase.Handle like this.

The last test is to handle InternalServerError (500) if an exception occurred.

I prefer to create a middleware to catch errors globally if an exception is thrown I proceed as follow:

  • Log the StackTrace logger.LogError($"Something went wrong: {ex.StackTrace}").
  • Return Internal Server Error with StatusCode = 500.

I'm using a single try/catch block in the entire application. It is also possible to have no try/catch by doing the following.

Here is the unit test of ExceptionMiddleware.

Code Coverage of presentation is 63%.

What is not tested are startup class and Programm class and ExceptionMiddleware.MoveNext() function.

I can add more tests to cover them, but I can also leave them like that because they will be covered by the integration tests.

The code source is available here RegisterSpeechPresentation

Further Reading

Building Microservices Through Event-Driven Architecture, Part 2: Domain Objects and Business Rules

Building Microservices Using Spring Boot and Docker — Part 1

Spring Boot Microservices: Small Services, Big Results [Tutorials and Articles]

microservices ,tutorial ,event-driven ,event-driven architecture ,presenters ,views ,controllers

Published at DZone with permission of Gora LEYE , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}