Skip to main content

Posts

Fixing Multiple If-Else problem with Chain of Responsibility Pattern

  Situation will become unmanageable if we have lot of if else condition in our code, Lets understand problem and then we will see it’s Solution. Problem with Multiple If Conditions Using multiple if-else conditions to handle different cases can lead to code that is hard to read, maintain, and extend. Here’s a simple example using multiple if conditions in a leave management system. We will implement below Business Logic for Leave management system a. IF Leave < = 3, then Team Lead will approve b. IF Leave < =5, then Project Manager will approve c. IF Leave > 5, then HR Manager will approve Request first go to team lead to check if Request can be handled by  Team lead  else it will go to  Project Manager  else request will go to  HR manager Example with Multiple If Conditions public class LeaveRequest { public string EmployeeName { get ; set ; } public int NumberOfDays { get ; set ; } public string Reason { get ; set ; } } publi...

Best practice Generic List Filter using Func in C#

  In this Chapter we will learn about Func<x,y> delegate and its real time implementations and will see how we can create centralize functions to filter generic list. Basic Definition Func delegates offer a straightforward approach to defining and utilising methods by passing them as parameters and returning them as results. Func<>  is a delegate type for methods that return a value. we can have up to 16 parameters, with the return type always being the last type parameter. It’s useful for passing methods as arguments or working with lambda expressions in a clean and concise way. Syntax Consider below syntax, it has two parts left and right. Right side of lamba expression is return value and Left side of lamba expression is input parameters. Example:  This is simple code using System; class Program { static void Main () { // A Func delegate that adds two integers Func< int , int , int > add = (x, y) => x + y; int ...