Posts

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; } ...

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC

Image
Are you ready to enhance your ASP.NET Core MVC project with the power of Identity? In this comprehensive guide, we'll walk you through the process of setting up and customizing the Identity system in your web application. By the end of this tutorial, you'll be able to implement secure user authentication and authorization for your ASP.NET Core project. Step 1: Create a New ASP.NET Core Web App Begin by opening Visual Studio and selecting "ASP.NET Core Web App (Model-View-Controller)" as your project template. Follow these steps: 1. Open Visual Studio and create a new project, selecting "ASP.NET Core Web App (Model-View-Controller)". 2. In the "Configure your project" window, enter the project name and choose the project's location. 3. In the "Additional Information" window, select the framework and choose "Individual Accounts" as the Authentication type. 4. Click "Create" to generate your project....

Improving Code Readability in C# with Digit Separators

Image
What Are Digit Separators? Digit separators were introduced in C# 7.0. When dealing with large numbers, digit separators help enhance the readability of your code. By using underscore (_) as a separator within groups of numbers, you can significantly improve code readability. Why Use Digit Separators? Consider the scenario where you need to work with the number 1000000000. Without digit separators, reading this number can be challenging. However, by using digit separators and representing the number as 1_000_000_000, it becomes much easier to read one billion. Examples of Digit Separators Let's explore a few examples to understand how digit separators function: int totalPopulation = 7_900_000_000 ; double distanceToMoon = 384_400 . 0 ; long nationalDebt = 28_000_000_000_000 ; As evident, adding digit separators in between groups of digits has no impact on the numerical value itself. It only improves human readability. Conclusion The introduction of d...