Dependency Injection (DI) is a design pattern which supports the development of loosely coupled code, and it’s also one of the SOLID principles (Dependency Inversion Principle). On this article, I will explain how we can use Dependency Injection and what is the difference between the three services lifetimes that can be used in .NET Core applications.
“Dependency Injection (DI) is a technique that provides dependencies to a class, thereby achieving dependency inversion. Dependencies are passed (injected) to a client that needs it.”
DI is used a lot in .NET Core applications, and also the own framework brings it natively. …
In this article we are going to see how to create the unit tests for the Repository classes in the Infrastructure layer, using an In-Memory database.
The Repository classes are responsible for handle the data into the database. There are at least three ways to create unit tests for these classes, one way is using a real database, which I do not recommend because the tests must be isolated from each other, and to do that it would be necessary to create a new database for each test, but this will make the tests run very slow. Another way is mocking the DbContext, which I also do not recommend. The third way, and the best way in my opinion, is using an In-Memory database provider, this means that it will fully run in memory, which is a lot faster than real database access, and allow us to run these tests in each build. …
A new feature that is released with the new version of the Entity Framework Core is the possibility to create a Many-to-Many relationship without explicitly mapping the join table. On this article, we are going to see how we can create a Many-to-Many relationship using Entity Framework Core 5.
On the previous versions of EF Core, to create a many-to-many relationship, it’s necessary to have a third entity. For example, to create a many-to-many relationship between the entity Actor and the entity Movie, it is necessary to have three entities: Actor, Movie, and ActorMovie. On the article “Entity Framework Core Relationships with Fluent API”, I explain about how you can create this kind of relationship (using EF Core 3.1) …
In this article we are going to see how to create the unit tests for the Controllers in the API layer. To create these unit tests we are also going to use the xUnit and Moq framework.
Creating the Unit Test project for the API layer
Let’s start creating a new xUnit project for the API layer, similar to the one we created for the domain layer in the previous article (you can read the previous article clicking here). Open the solution with Visual Studio, and create a new xUnit project and add it on the “test” folder:
In this article we are going to see how we can create the unit tests for the Services classes in the Domain layer, using xUnit framework.
The three most commons unit test frameworks for .NET are:
The goal of the unit test, it’s to verify each unit from the software, in isolation, to guarantee that each unit of the code is doing exactly what it should be doing. …
Having automated tests on the application gives us a lot of benefits, even so, it’s not uncommon to see software that does not have tests ou hear someone saying that tests are not necessary. Tests are used in many kinds of industries, like cars, medicines, and others. The products are constantly tested by the companies to ensure that the product or service that they are offering are safe and work correctly. In software industries, this should not be different.
The software should have automated tests to ensure the good quality and warranty that the software is doing exactly what it was planned to do. Even so, is not rare to see people that don’t like to work with it. …
When we deal with casting, there are situations where an Exception can be thrown. For example, if the type of the object in memory does not match the cast, the runtime will throw a System.InvalidCastException. For example:
This code will compile but will throw an exception at runtime. To avoid this kind of error, we can use the
as operators which are provided by C# and can help us to perform casting operations in a more elegant way.
The is Operator
is operator is used to checking if the run-time type of an object is compatible with a given type or not. The result of the validation will return a boolean value (true or false), and the is operator never throws an exception. …
The Singleton design pattern is one of the creational pattern from ‘Gang of Four’ (GoF), and it ensures that a class only has one instance, and provide a global point of access to it. The Singleton pattern restricts a class to instantiate its multiple objects. The Singleton defines an Instance operation that lets clients access its unique instance, and also is responsible for creating and maintaining its own unique instance.
How Singleton works
This is the UML class diagram:
Entity Framework Core (EF Core) is an Object-Relational Mapping (ORM). It works between the application and the database. To explain how we can implement the relationships of our entities, I will be using the Code First approach (with a new database), which means that first I’m going to the create the entities classes, and the EF Core will create the database and the tables, based on these entities (EF Core maps the entities to the database).
We are going to see how we can create relationships between classes using EF Core and Fluent API. For demonstration purpose, I’ve created a Console Application using .NET …
ACID is a concept (and an acronym) that refers to the four properties of a transaction in a database system, which are: Atomicity, Consistency, Isolation and Durability. These properties ensure the accuracy and integrity of the data in the database, ensuring that the data does not become corrupt as a result of some failure, guaranteeing the validity of the data even when errors or failures occur.
The ACID properties allow us to write applications without considering the complexity of the environment where the application is executed. This is essential for processing transactions in databases. …