Skip to main content

9 posts tagged with ".NET"

View All Tags

Who calls Dispose in .NET and how Finalization works in .NET

· 8 min read
Adnan Rafiq
VP of Technology

Dispose Myth's - Two Questions

Read the below code to answer the questions that follow the code.

General Myth's - Two Questions

PrintFileContent();
Console.ReadKey();

static void PrintFileContent()
{
//Notice that I have not wrapped the DisposableStream object in a using block and neither I am calling Dispose method.
var disposableStream = new DisposableStream("file");//Assume that file exists in the current directory.
disposableStream.PrintAllLines();
Console.WriteLine("I am done with the stream.");
}

public class DisposableStream(string fileName) : IDisposable
{
private readonly FileStream _fileStream = new(fileName, FileMode.Open);

public void PrintAllLines()
{
using var streamReader = new StreamReader(_fileStream);
while (streamReader.ReadLine() is { } line)
{
Console.WriteLine(line);
}
}

public void Dispose()
{
_fileStream.Dispose();
Console.WriteLine("DisposableStream dispose was called");
}
}
  1. Will the Dispose method be called on the DisposableStream object when the PrintFileContent method completes, since we are not wrapping it in a using block and neither calling the Dispose method explicitly?
  2. If the Dispose method is not called, how will the FileStream object be collected by the Runtime?

What, Why and How of Adapter Pattern in C#

· 6 min read
Adnan Rafiq
VP of Technology

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.

Cache Aside Pattern using C#

· 4 min read
Adnan Rafiq
VP of Technology

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.

Template Method Pattern using C#

· 5 min read
Adnan Rafiq
VP of Technology

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.

Chaos Engineering Experiments with Gremlin, Octopus and PowerShell

· 4 min read
Adnan Rafiq
VP of Technology
Image of Hello

Image by @isaacmsmith

What is Chaos Engineering?

Chaos Engineering is about testing & increasing your system resilience.

  • Resilience Definitions on merriam-webster.com
    • the capability of a strained body to recover its size and shape after deformation caused especially by compressive stress
    • an ability to recover from or adjust easily to misfortune or change

We test our system by intentionally causing failure in parts, such as saturating the host's CPU. During the failure, we measure the time to recovery and other metrics you would want to collect.

Tuples in C# 10 and Memory Mental Model in .NET

· 12 min read
Adnan Rafiq
VP of Technology
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.

An Introduction to Accessing data from RDBMS in .NET

· 4 min read
Adnan Rafiq
VP of Technology
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