Skip to main content

31 posts tagged with "C#"

View All Tags

· 3 min read
Adnan Rafiq
Start and Finish Image

Image by awcreativeut

CORS are best described on MDN

Cross-Origin Resource Sharing (CORS) is an HTTP-header based mechanism that allows a server to indicate any origins (domain, scheme, or port) other than its own from which a browser should permit loading resources. CORS also relies on a mechanism by which browsers make a "preflight" request to the server hosting the cross-origin resource, in order to check that the server will permit the actual request. In that preflight, the browser sends headers that indicate the HTTP method and headers that will be used in the actual request.

How to configure CORS in .NET6 API?

CORS in .NET6 API can be configured using CORS policies.

· 3 min read
Adnan Rafiq
An image of alphabets
Image by @linharex

Problem

When the application expects Unicode characters as input from the user, it is best to normalize it before storing it in the database, especially when you plan to use the information for comparison.

Suppose the application asks the user to upload a file with the same name as their first name, which contains the character é. If you validate the file name using the string comparison (===) operator or comparing length, it will fail if different Unicode code points represent the input.

You validate the client-side and server-side using C# as a best practice. It would be best to normalize the string before comparing; otherwise, the validation will fail either at the server or client side.

Browser's behavior is different for the Unicode characters; some do the normalization, and some do not. I recently had to fix an issue where string comparison without normalization only failed when the user uploaded the file using Chrome or Firefox on Mac. One such example is on here.

Normalize unicode strings for correct comparison

Some unicode character like ñ can be represented by using one code point (\u00F1) or two code points (\u006E\u0303). Such characters visually looks exactly the same but will have different string length. Thus string equality comparison and length tests will fail. This MDN and .NET article(s) describe it beautifully. If you are expecting unicode characters as an input from the user, store it after normalizing.

· 6 min read
Adnan Rafiq

What is an Adapter Pattern?

The Adapter Pattern connects two incompatible objects by exposing an interface compatible with the Client. The object refers to a class, web service, REST API, process, or physical device depending upon your context.

Consider a C# Web Application displaying Weather Updates on its landing by utilizing the third-party REST API.

In this application, there are three participating objects in the Adapter Pattern:

  1. A Weather REST API (Adaptee) has the functionality of weather updates. The Weather REST API only understands JSON.
  2. The C# MVC Web Application (Client) displays weather updates on its landing page using the Weather Web Service.
  3. The Adapter enables the Client to utilize the Adaptee functionality. It does that by doing two things:
    1. It converts the Client's input to the format acceptable to Adaptee i.e. JSON to C# Object(s).
    2. It converts the Adaptee's output to the format acceptable to the Client i.e. C# Object(s) to JSON.

· 15 min read
Adnan Rafiq
Start and Finish Image

Image by awcreativeut

tip

I would appreciate if you can subscribe to my YouTube Channel.

I know it's a big ask, but it will help me to keep writing and producing awesome content for you.

The word Host will repeatedly appear in the post so let's briefly understand what it means?

What is Host?

The Host is a container which offers rich built-in services such as Dependency Injection, Configuration, Logging, Host Services and others. The NET 6 offers Generic DefaultHost which can be configured to handle the activities as per your use case. Two major variations of the Host are:

  • Console Host - CLI based applications.
  • Web Host - Web API & Applications.

Think of it as Airbnb Host who keeps the property ready to serve when the guests arrive. The property offers a different set of services and allows you to bring your own services. The lifetime of such services depends upon the contract, which the Host controls.

Basic Host Example : Create, configure, build, and run the Host
var host = Host.CreateDefaultBuilder(args) //WebHost.CreateDefaultBuilder(args)  
.ConfigureLogging( (context, builder) => builder.AddConsole())
.Build(); // Build the host, as per configurations.

await host.RunAsync();

What is Hosted Service?

A service that performs the work in the background mostly does not offer an interface to interact. In technical terms, any reference type object which implements the IHostedService interface is a background/hosted/worker service.

Terms such as Worker, Windows Service, and Background Task refer to HostedService based on context. In Windows Server, Widows Service is how you deploy a Hosted Service. Background Task or Hosted service runs as part of .NET Web Host, and it runs in the same operating system process.

· 4 min read
Adnan Rafiq

What is Cache Aside Pattern?

It enables you to improve application performance by reading the data from the cache-store (Redis, Memory Cache) instead of the persistent store (database) or an integration service.

· 5 min read
Adnan Rafiq

What is Template Method Pattern?

Template Method Pattern executes multiple same steps in the same order and allows consumers to change the behavior of the steps.

“Implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.” Elements of Reusable Object-Oriented Software.

· 12 min read
Adnan Rafiq
Image of Hello

Image by @helloitsammiel

What are Tuples?

A Tuple is a container to hold multiple values which provide readable syntax when used appropriately. Tuples are available from C# 7 and later versions. There are two types of Tuples:

  1. Value Type
  2. Reference Type

Naturally, you might ask, what is the difference between Value Type and Reference Type?

The .NET runtime manages the memory for your application. It uses two distinct places to store data in memory, known as Stack and Heap. Any Value or Reference Type can end up either on Heap or Stack purely depending upon its usage.

Mental Model : Draw two different shapes in your head. One is fast & small, and the other is big & efficiently managed. Value Types are for small & superfast (Stack), and Reference Types for big & efficient (Heap).

I will not use Stack or Heap in rest of the post. Instead, I want you build a mental model based on two distinct memory regions.

· 4 min read
Adnan Rafiq
Image of Hello

Image by @abrizgalov

Overview

Consider developing an application that requires you to store and retrieve data and display it on UI. You will likely need two things:

  1. Relational Data Management System (RDBMS) allows you to store and retrieve data permanently on disk(s) with specific promises.
  2. .NET is cross-platform, which allows you to develop different types of applications (web, console, mobile) using multiple languages (C#, F#).

Data Access in this context means making these two things (.NET & RDBMS) talk with each other. Users will interact with UI which is built using the .NET platform, which is going to learn how to talk with the database in its language (SQL).

.NET offers two different approaches to achieve data access?

  • EF Core - An OR/M.
  • .NET Native abstractions - without an OR/M