Unit Testing in Dotnet Core With Nsubstitute

As the name suggesting , Unit testing is a software testing where smallest units of the application such as methods are tested in the isolation, so that we can ensure our software is working as expected.

Commonly used testing frameworks

  • MSTest
  • nUnit
  • xUnit

Mocking frameworks

Mocking framework is a library which allows us to mock the objects. For example, a PeopleController is injected with the IPersonRepository. While testing the PeopleController, we need the IPersonRepository. Mock frameworks comes to rescue in that situation. With the help of mock frameworks we can mock the IPersonRepository and mimic it’s behavior. Some popular mocking libraries are:

[Read More]

Containerizing Dotnet App With PostgreSql Using Docker Compose

Containerizing A .NET App With Postgres Using Docker Compose

In this tutorial, we are going to containerize the .NET Web API application with docker and postgres. I am assuming you are familiar with docker. At least, you should have some understandings of how docker works. However, I have covered all the steps needed to create a docker container for your application, but I am not going to cover the theoretical concepts of docker.

[Read More]

Containerizing Dotnet App With Sql Server Using Docker Compose

running dotnet core app in docker with postgres

In this tutorial, we are going to containerize the .NET Web API application with docker. I am assuming you are familiar with docker. At least, you should have some understandings of how docker works. However, I have covered all the steps needed to create a docker container for your application, but I am not going to cover the theoretical concepts of docker.

🔨Tools needed

  • Visual Studio Code (Free)
  • .Net 8.0 SDK (Free)
  • Docker desktop (Free)

🧑‍💻Tech used

  • .Net 8.0 Web APIs (controller APIs)
  • Ms SQL Server (within a container)
  • Docker compose

🍵Note: I am using windows 11 operating system.

[Read More]

.Net Core API CRUD With PostgreSql

aspnet core apis crud

When we create an application with .NET, we tend to use the Microsoft tech stack like Visual Studio IDE, Microsoft SQL Server, Windows Operating System, and Azure. However, things have changed since the introduction of .NET Core. We are no longer bound to a specific operating system and database.

In this blog post, we will learn how to create a Web API CRUD (Create, Read, Update, Delete) application using .NET Core and a Postgres database.

[Read More]

Image Upload CRUD Operations in .NET Core WebAPIs

how to upload images in .net core web api

Upload images in .net core apis

In this article, we will learn how to upload/update/delete/read images in .net core APIs. I hate to give long and unnecessary intros, So let’s come to the point’s.

💻 You can get the code from this github repo.

What is the logic behind it? 🤔

When you upload the image (or any file), we will generate the unique image name with it’s extension (eg. uniquename.png). Save the image name in the database and save the file with the same name inside the project. In production apps, you can not simply upload files to the server, you have to give permission to that folder. Or you can use other file storage service, where you will save your files.

[Read More]

Separating the DI Setup From Program.cs file

Separating the DI Setup From Program.cs file

Let’s take a look at the program.cs file below.

using InventoryMgt.Api.Middlewares;
using InventoryMgt.Data.Repositories;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddTransient<ICategoryRepository, CategoryRepository>();
builder.Services.AddTransient<IProductRepository, ProductRepository>();
builder.Services.AddTransient<IPurchaseRepository, PurchaseRepository>();
builder.Services.AddTransient<IStockRepository, StockRepository>();
builder.Services.AddTransient<ISaleRepository, SaleRepository>();
builder.Services.AddTransient<ExceptionMiddleware>();
builder.Services.AddCors(options =>
{
 options.AddDefaultPolicy(policy =>
 {
 policy.WithOrigins("*").AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("X-Pagination");
 });
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
 app.UseSwagger();
 app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseCors();
app.UseAuthorization();
app.ConfigureExceptionMiddleware();
app.MapControllers();

app.Run();

It is a regular program.cs file. Since we use the dependency injection in our application, we have to register lots of service in our program.cs file. For example:

[Read More]

Fluent Validation in Dotnet Core

fluent validation in asp.net core

Fluent validation

Fluent validation is a third party library for validating your models in .NET . It is totally free.

📢 📝 Last Updated: 21-August-2024

Why fluent validation?

If you already have used data annotation for validation, then you must be aware of validation in .NET. So you might be thinking why do we need fluent validation.

Fluent validation helps you to separate validation logic from your models. That makes you code clean. If you have complex validation logic, then you want to define it separately rather than making your model unreadable.

[Read More]

Uploading Images in Blazor Server

upload files in blazor server

In this blog post we are going to learn how to upload files in the blazor server application. I am going to upload images in this tutorial but you can upload any file (i have created reusable code).

💻Source Code: https://github.com/rd003/BlazorFile/

High level overview

We will upload images to a folder of a local machine ( on a production you have to use cloud storage) with a unique name (e.g. sd$3abccc3$1.png ), that name is going to save in database.

[Read More]

Easiest Way to Handle Csv Files in Csharp

how to read and write to csv files in c#

In this tutorial we will se how to read and write data to csv file. It is pretty much easy if you have some external library for that. We are definitely going to use a library and that will be CsvHelper.

You can also check the video version of this tutorial.

First and foremost, create a c# console application in .net core. After that we need to install a nuget package, which is**CsvHelper**

[Read More]

Catch Exception Globally in Dotnet Core Apis

Catch Exception Globally in Dotnet Core Apis

There are to ways of catching exception in .net core, one is using try/catch and other one is catching them globally. Although there is nothing wrong with try/catch blog, it is an elegant way of exception handling. Although you can also catch exceptions globally, If you want to log errors in one place. There are various approaches to achieve this, but we will create the custom middleware to achieve it.

[Read More]