Sin categoría

Azure Durable Functions

Durable functions are an extension of Azure Functions that allows Azure Functions to be stateful which mean it allows us to keep track of the context of the application even after the function finished the execution or stopped for some reason allowing you to create workflows easily.

No alt text provided for this image
Continue reading
Sin categoría

HostedServices .Net Core

Hosted services in .net are classes that implement the IHostedService interface and run background tasks.

These tasks can be started as a part of the host’s lifecycle. That means that they start when the application starts.

The hosted services are useful for performing background tasks such as scheduling, long-running operations, or recurring tasks that need to run independently of the main application.

Continue reading
dotnet core

The how and why of Integration Tests .Net core 5 XUnit

The purpose of this post is to provide information and answer questions about integration tests. It covers topics such as the importance of integration tests, how to create and set up an XUnit project, how to recreate and seed a database, how to run integration tests in order, and how to mock calls to external APIs so that the integration tests do not rely on the availability of external systems.

Integration Tests

Integration tests are a type of software testing that verifies the interaction and communication between different components in a system. They are designed to test the integration of different parts of a system.

Why integration tests are important?

Integration tests are important because they test how different parts of a system work together and verify that components in the application, such as different classes or modules, work together as expected. This is different from unit tests, which test individual components in isolation.

Integration tests are particularly important when you are building a complex application with many different components that need to work together. By writing integration tests, you can verify that the different parts of the application are communicating and interacting correctly and that the application as a whole is working as intended.

The test pyramid

The test pyramid refers to the idea that, when writing tests, it is generally more efficient and effective to have a larger number of low-level unit tests and a smaller number of high-level end-to-end tests. This is because low-level unit tests are typically easier to write, work with, and maintain than high-level end-to-end tests. In addition, low-level unit tests are usually faster to execute than higher-level intergrain or end-to-end tests, which may require setting up a server or the entire system to run.

Integration tests, which are placed in the middle of the pyramid, cover some of the same areas as unit tests and some of the same areas as end-to-end tests. They are generally used to test the integration of different components or systems covering both the happy and unhappy paths. Unit tests, on the other hand, include tests for niche cases that integration tests may not cover.

Continue reading
dotnet core

Efficiently Modifying Lists: To Remove or Create a New One? .Net Core 5

When you have a big list and you need to remove items from that list.

Is it better to remove the items from that list or is it better to create a new list?

When you remove an item from a list, the list will shift all the elements after the removed item to the left to fill the gap. For example, if you have a list [1, 2, 3, 4, 5] and you remove the item at index 2 (which is the value 3), the list will become [1, 2, 4, 5]. The element at index 3 (which was previously the value 4) will now be at index 2, and so on.

This operation can be expensive, especially if the list is large and you are removing multiple items. It is usually more efficient to create a new list and add only the items that you want to keep, rather than removing items from an existing list.

On the other hand, creating a new list involves allocating a new block of memory and copying the elements that you want to keep into the new list. This can also be a time-consuming operation, but it is usually faster than removing items from a list, especially if you are only removing a small number of items.

Let’s take an example. I am reading a 10 MB Excel file that has 46 columns and 54,000 rows.

In the first method, I am passing the List of items and apply some filters. Based on those filters I am creating two lists. One list with valid records and another one with invalid records

Continue reading
Sin categoría

Factory and Strategy pattern in real case scenario with .Net core 5

Some time ago I got a new requirement. When analyzing the requirement I realized that I would need some design patterns.

When I was first introduced to design patterns I got some trouble understanding them because of the lack of real-life scenarios.

This post contains the usage of the Factory and Strategy pattern in a real-life scenario to create a clean and maintainable code by following the SOLID principles in a real-life scenario.

As a note, it is important to denote that the application follows the CQRS architectural pattern. (You will see the usage of Mediatr library along with some Commands, Queries, and Handlers)

This post is under construction. To read the complete post go to my LinkedIn article
https://www.linkedin.com/pulse/factory-strategy-pattern-real-case-scenario-net-core-5-mata-viejo/

Sin categoría

How to handle Validations on CQRS implementation with MediatR .Net core 5

When implementing CQRS using MediatR it is possible to implement pipelines behaviors.

These pipelines allow to catch all commands and queries and gives the ability to execute some logic before and after the Commands and Queries Handlers are executed.

The Validation pipeline behavior

One of the many pipelines that can be added is the validation pipeline behavior which adds logic to validate the properties in the Commands and Queries, resulting in that only valid requests will be passed to the Commands and Queries Handlers.




How to implement Validation Pipeline Behaviour?

The easiest approach is to use FluentValidation. Fluent validation is a library that allows writing easy and complex validations for the Queries and Commands.
The next example shows how to create a Validator.
1.- Implementes AbstractValidator from FluentValidation and add the object to be validated as the parameter.
2.- Write the rules for each one of the parameters of the command or query object.
Continue reading
Sin categoría

Implementing Event-Driven in .Net Core 5

The event-driven is an architecture that uses events to communicate changes of states to applications that are interested in different events.

This type of architecture decouples services by using Service buses. This allows the services involved to be scaled, updated, and deployed independently.

This post will explain how to implement an event-driven architecture with MassTransit (a library to configure and send events messages to a Message broker) and RabbitMQ (a message broker that handles events messages).

Continue reading
Sin categoría

Auto-Generating Angular web client services with Swashbuckle in .Net Core 5

When you are working with Angular and APIs it is very common that models need to be replicated in Angular and then services need to be created in order to call those APIs.

This repetitive job has to be done every time a new model is being created and each time a model is updated in the APIs a manual update has to be done on the Angular model.

Thanks to Swagger, which is the implementation of the Open API specification that comes by default when creating new web APIs in .Net core 5, it is possible to auto-generate client services and models in Angular.

Continue reading