Welcome to my blog, about anything .NET related, I hope you enjoy your stay here
Auto Registering dependencies with Scrutor
Dependency injection (DI) software design pattern is one of the most used ones and requires no introduction, but if you need any check out MDNS article about this topic. To achieve Inversion of Control we can use DI Containers, in which we define services and the abstractions that they fulfill so that they can be injected (created) at runtime. .NET Core and .NET5+ has a simple container built-in that allows registering dependencies one by one, but the application can quickly outgrow this approach, plus it creates an additional responsibility of remembering to register our new service each time we create one....
EF Core Implementing Fuzzy Search
Fuzzy search allows finding strings that match the pattern approximately. It is a feature needed in almost every app, but it can be a little problematic to implement. We will focus on EFCore PostgreSql provider as it is free and seems to be the most popular choice nowadays (for me at least). First, our EFCore project needs this package: PostgreSQL.FuzzyStringMatch Then, we need to register it with our DbContext, so let’s navigate to Startup....
.NET Project Structure
For almost all of my projects, I follow this simple project structure and it works great, so let’s go over it from the bottom. Host The entry project for your solution, in most cases it will be an API Host (for HTTP communication), but with this approach adding a new way of hosting your app is as easy as adding a new project and hooking up into your application. It should reference the Infrastructure project and register all it’s entry points as needed (app....
EF Core Automatic Creation and Modification Timestamp
For auditing purposes, we might want our entities to have creation and/or modification timestamps (we may also extend it with some additional data, like user ID). It can be a pain to keep in mind to always set the appropriate columns, but we can make EFCore do it for us. First we need to create some sort of interface for it, like: ICreationAudited.cs or IAuditedEntity.cs. public interface IAuditedEntity { DateTimeOffset CreationDateTime { get; set; } DateTimeOffset?...
EF Core Registering Model Configurations By Convention
When our model configuration is stored all in one file it becomes tedious to edit it as the application grows. Even, if developers agree to stick to some kind of ordering it is bound to blur and fade away as one of the developers inevitably adds one configuration in the wrong place. How can we prevent it? The answer to our problem is: IEntityTypeConfiguration<T>. By implementing the interface you can configure the model for each class by implementing the interface’s only method void Configure(EntityTypeBuilder<T> builder)....
EF Core Implementing Soft Delete
Soft deleting is an easy way of deleting data without actually removing it from the database. Instead of performing DELETE on the database we mark it as deleted and filter it out by default on the application side. We want our soft delete mechanism to function seamlessly with EFCore, after all, it’s just the implementation detail, so we need to intercept all DbContext Remove calls. So, to achieve this we need two parts:...