Posted on February 23, 2025
| 10 minutes
| 2003 words
| Ravindra Devrani
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.
Posted on February 23, 2025
| 2 minutes
| 405 words
| Ravindra Devrani
Higher-order functions (HOF) are functions that can take a function as an argument or return a function or both. In C#, higher-order functions are achieved using delegates, lambda expressions and expression trees.
Example 1: Using a Delegate
// Declare a delegate that takes an int and returns an intpublicdelegateint IntOperation(int x);
publicclassProgram{
// Higher-order function that takes a delegate as an argumentpublicstaticint PerformOperation(intvalue, IntOperation operation)
{
return operation(value);
}
publicstaticvoid Main()
{
// Create a delegate instance that points to the Square method IntOperation square = x => x \* x;
// Pass the delegate to the higher-order functionint result = PerformOperation(5, square);
Console.WriteLine(result); // Output: 25 }
}
Example 2 : Using lambda expression
// Higher-order function that takes a lambda expression as an argumentpublicstaticint PerformOperation(intvalue, Func<int, int\> operation)
{
return operation(value);
}
// main method// Define a lambda expression that squares a numberFunc<int, int> square = x => x * x;
// Pass the lambda expression to the higher-order functionint result = PerformOperation(5, square);
Example 3: Function as a Return Value
publicclassProgram{
// Higher-order function that returns a functionpublicstatic Func<int, int\> GetOperation(bool isSquare)
{
if (isSquare)
{
return x => x \* x;
}
else {
return x => x + x;
}
}
publicstaticvoid Main()
{
// Get a function that squares a number Func<int, int> operation = GetOperation(true);
// Use the returned functionint result = operation(5);
Console.WriteLine(result); // Output: 25 }
}
In c#, higher order functions are everywhere. If one have used LINQ, must have used HO functions. Collection’s Where() is the good example.
Posted on February 23, 2025
| 6 minutes
| 1236 words
| Ravindra Devrani
I assume you have some knowledge of C# properties. If not, here’s a quick definition: A property is a class member that provides a flexible way to read, write, or compute the value of a private field. Don’t worry if you’re new to properties; we’ll be using them in this blog, and I’ll explain them as we go along.
1. Properties with getter and setter
publicclassPerson{
publicstring Name { get;set; }
publicint Age { get; set;}
}
Generally, we define properties like this. The Person class has two properties Name and Age, each with getters and setters. This enables you to set and read their values.
Posted on February 23, 2025
| 3 minutes
| 498 words
| Ravindra Devrani
The Command Pattern is a behavioral design pattern that turns a request into a stand-alone object containing all the information about the request. This conversion allows you to parameterize methods with different requests, queue requests, and support undoable operations.
In simpler terms, the Command Pattern encapsulates a request as an object, thereby allowing users to decouple the sender (invoker) from the object that performs the action (receiver).
Key Concepts
Command: An object that encapsulates a request or action.
Posted on February 23, 2025
| 4 minutes
| 726 words
| Ravindra Devrani
The Adapter pattern is a structural design pattern that allows two incompatible interfaces to work together by converting the interface of one class into an interface expected by the clients. In other words, it acts as a bridge between two incompatible interfaces, enabling them to communicate with each other.
Adapters in real world?
For a phone that does not support a 3.5 mm audio jack, you can use a USB-C to 3.5 mm audio adapter. The phone has only a USB-C port, so the adapter converts the USB-C connection into a 3.5 mm audio jack, allowing you to use traditional wired headphones with your phone. The adapter bridges the gap between the old headphone interface and the new phone design.
Posted on February 23, 2025
| 4 minutes
| 660 words
| Ravindra Devrani
The Facade pattern is a structural design pattern that provides a simplified interface to a complex system of classes or interfaces.
Let’s understand it with example. Imagine a home automation system.
Let’s implement it programmatically.
publicclassFacadePatternTestDrive{
publicstaticvoid Main(string[] args)
{
ILights lights = new Lights();
IAirConditioner airConditioner = new AirConditioner();
ISecuritySystem securitySystem = new SecuritySystem();
// When leaving home lights.TurnOff();
airConditioner.TurnOff();
securitySystem.Arm();
Console.WriteLine("You have left home.");
// When entering home lights.TurnOn();
airConditioner.TurnOn();
securitySystem.Disarm();
Console.WriteLine("Welcome home!");
}
}
Note: AirConditioner, SecuritySystem and Lights class is not present in the above example for the sake of simplicity. We will write those classes when we implement the facade pattern.
Posted on February 23, 2025
| 3 minutes
| 491 words
| Ravindra Devrani
The Decorator pattern is a structural pattern that enables you to wrap an object with additional behaviors or responsibilities without changing its external interface. It’s like adding layers of decorations to a gift — you can add or remove decorations without altering the gift itself.
Example:
Let’s say, we have a book whose base price is 500 INR. Some buyers need a hard cover book which costs some extra money, some need a signed copy which also add some additional cost, some needs the both hard cover and a signed copy which adds more cost. May be in future we want to remove the option of hard cover book or may be we want to add some extra features to it. How do we make our system more flexible, that is the big question.
Posted on February 23, 2025
| 3 minutes
| 569 words
| Ravindra Devrani
The Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
In a simple words, it is like a publisher and subscriber pattern. When publisher publishes something, all it’s subscriber gets notified.
One Example could be, when we subscribe someone’s mailing list we get notified whenever the publisher publishes something.
Posted on February 23, 2025
| 4 minutes
| 753 words
| Ravindra Devrani
The Abstract Factory pattern is a creational design pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows a client to create objects from a variety of related classes without needing to know the specifics of their implementations.
GUI Factory Example
Let’s say, we need to create GUI components for different themes in our application. Each theme includes a consistent set of components such as buttons and checkboxes. We want to ensure that the creation of these components is consistent and interchangeable.