Posts

What Are Aggregates in Domain-Driven Design (DDD)?

Image
  What Are Aggregates in Domain-Driven Design (DDD)? Have you ever wondered how to keep your business rules safe when multiple objects work together? In this blog, we'll learn what Aggregates are in Domain-Driven Design, why they are important, and we'll use a simple online shopping example to make everything crystal clear. Let's get started! What is an Aggregate? An Aggregate is a group of related objects that should be treated as a single unit. Think of it as a boundary around objects that need to stay consistent together. Inside an Aggregate, there can be: Entities Value Objects And one special entity called the Aggregate Root. The Aggregate Root controls access to everything inside the Aggregate. Simple Real-Life Example Imagine you're placing an order on an e-commerce website. An order contains: Order information Order items Shipping address Visually, it looks like this: Order Order Items Shipping Address In DDD, all of these can form a single Aggregate. The Order ...

Value Objects in Domain-Driven Design (DDD) | .NET Explained with Examples

Image
Value Objects in Domain-Driven Design (DDD) | .NET Explained with Examples 1

Simple Memory Cache in ASP.NET Core

Image
In this blog post, we'll explore how to use in-memory caching in ASP.NET Core. Caching can highly enhance the performance of your web application by reducing the number of times data needs to be retrieved or computed. We'll use a simple example to illustrate how you can implement in-memory caching in your ASP.NET Core application. Setting Up the Project First, let's set up our ASP.NET Core project. We'll start with the HomeController class, which will handle our web requests. Here's the code for our HomeController: public class HomeController : Controller {     private readonly ILogger<HomeController> _logger;     private readonly IMemoryCache _memoryCache;     public HomeController(ILogger<HomeController> logger, IMemoryCache memoryCache)     {         _logger = logger;         _memoryCache = memoryCache;     }     public IActionResult Index()   ...

Building a Background Task Scheduler in ASP.NET Core: Step-by-Step Guide

Image
Introduction: In many web applications, there arises a need to perform certain tasks in the background at regular intervals, such as sending emails, updating caches, or fetching data from external sources. ASP.NET Core provides a easy way to achieve this through hosted services. In this blog, will walk through the process of creating a simple background task scheduler using ASP.NET Core's hosted services feature. Step 1: Creating a New ASP.NET Core Web Application Launch Visual Studio. Click on "Create a new project". In the "Create a new project" window, select "ASP.NET Core Web Application" and click Next. Project Name: Enter SampleBackgroundService. Location: Choose a location to save your project. Click Next. Step 2: Creating the Background Service Create a new class named MyBackgroundService.cs in your project and implement the background task logic. This class should inherit from BackgroundService and override the ExecuteAsync method...

Localization in ASP.NET Core MVC with Example

Image
Using Query Strings for Request Culture and Simplifying Localization with IStringLocalizer Interface Let’s see how to implement Localization in ASP.NET Core MVC. In the first example, I am going to use the IStringLocalizer Interface in the controller to implement Localization. Additionally, when the user passes the desired language through the query string, it will display content in that language. First, let’s configure the settings in the program.cs file. builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");  The AddLocalization method adds the localization service to the service container. I have added the supported cultures here. I have added two cultures: en for English and nl for Dutch. In the RequestCultureProviders , I have included QueryStringRequestCultureProvider() . This allows the system to determine the culture information from the query string. UseRequestLocalization initializes a RequestLocalizationOptions o...

Database First Approach in Entity Framework Core: A Step-by-Step Guide

Image
Introduction: In this blog, I'll walk you through the process of setting up an Entity Framework Core project with a focus on Microsoft SQL Server as the database provider. Setting Up the Database: To begin, we created two tables, namely "Students" and "Teachers," in the "SampleDB" database. This forms the foundation for our data model. Creating the MVC Project: Next, we initiated a new MVC project using Visual Studio. Installing Necessary Packages: In the Package Manager Console, execute the following commands to install the essential packages for Entity Framework Core: Install-Package Microsoft.EntityFrameworkCore.SqlServer Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design Install-Package Microsoft.EntityFrameworkCore.Tools Microsoft.EntityFrameworkCore.SqlServer: Installs the required package to enable Entity Framework Core with Microsoft SQL Server as th...

Simplifying Code with Null Propagation in C#

Image
Code readability, maintenance, and reducing the risk of unexpected crashes. Earlier, developers used nested null checks to navigate through complex object structures, ensuring that each step along the way is not null. But WWith the introduction of Null Propagation in C#, a cleaner and simpler syntax appears, streamlining the process of handling null references. In this blog post, we'll explore Null Propagation using a simple code snippet. Before Null Propagation: Let's consider a scenario where we have a Person object and want to retrieve the city from their address. Here's how it might have been done traditionally: Person ? person = null ; string ? city = null ; if (person != null && person.Address != null ) { city = person.Address.City; } ...