Skip to main content

Posts

Showing posts with the label c#

Report generation in .Net Core API

  In this tutorial we will learn about quick way to generate reports using .net core api. When we work in companies, we may face a situation where client ask to generate reports, and he is not ready to spend money in purchasing third party libraries to get reports. Apart from this he want to work to done in same day or he has some urgency. In this situation this tutorial will helpful where we need to generate reports in single day without spending and wasting time in research. Let’s Start. a. We will create .Net core API. b. Create Endpoint for Download file c. Make a call to database and fetch data. d. Create report in html format and send html file back to user as response. Consider we connect to database and get below data table Consider code below using Microsoft.AspNetCore.Mvc; using System.Data; using System.Text; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 namespace DownloadFile.Controllers { [ ...

Fixing Multiple Switch Case Problem in C#

 I n this Chapter we will see how we can fix growing switch case which cause code maintainability issue In our development projects we may have below situation where switch cases increase with time and getting difficult to maintain Let’s understand problem using System; class Program { static void ProcessOrder ( string orderType ) { switch (orderType) { case "NEW_ORDER" : Console.WriteLine( "Processing new order." ); // New order logic here break ; case "CANCEL_ORDER" : Console.WriteLine( "Cancelling order." ); // Cancel order logic here break ; case "SHIPPING" : Console.WriteLine( "Shipping order." ); // Shipping logic here break ; case "DELIVERED" : Console.WriteLine( "Ord...

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