Posts

Showing posts from March, 2025

πŸš€ Understanding AddTransient, AddScoped, and AddSingleton in C# Dependency Injection

  When working with Dependency Injection (DI) in .NET, you’ll come across three main ways to register services: ✅ AddTransient() ✅ AddScoped() ✅ AddSingleton() But what do they mean? Let’s break them down with simple analogies! ☕ Imagine a Coffee Shop Scenario You're running a coffee shop and serving customers. Your baristas (services) can be hired in different ways: Lifetime Type What It Means Coffee Shop Analogy ☕ AddTransient() New instance every time A new barista is hired for each customer order . AddScoped() One instance per request A barista is assigned for an entire customer visit . AddSingleton() One instance for the entire application One permanent barista serves all customers forever . Now, let’s look at them in technical terms. πŸ”Ή 1️⃣ AddTransient – Always Fresh (New Instance per Request) services.AddTransient<ICoffeeService, CoffeeService>(); πŸ›  How It Works? Every time you request ICoffeeService , a new instance is created. Best for lightweight, stateless ...

πŸ› ️ Understanding Dependency Injection in C#: A Simple Guide for Beginners

  πŸ€” What is Dependency Injection? Let’s break it down with an everyday example. 🎯 Real-World Scenario: Ordering Pizza πŸ• Imagine you love pizza, and every time you want one, you go to a specific pizza shop ( hardcoded dependency ). public class Person { private PizzaShop _pizzaShop = new PizzaShop(); // Direct dependency public void OrderPizza () { _pizzaShop.MakePizza(); } } πŸ›‘ Problem: You are tied to one pizza shop (class PizzaShop ). What if you want to order from a different pizza shop? What if the pizza shop closes? ✅ Solution: Dependency Injection (DI) Instead of hardcoding the pizza shop, let's allow any pizza provider to be used. public interface IPizzaProvider { void MakePizza () ; } public class DominoPizza : IPizzaProvider { public void MakePizza () => Console.WriteLine( "Making Pizza from Domino's..." ); } public class Person { private readonly IPizzaProvider _pizzaProvider; publi...

πŸš€ Dependency Injection in Azure Functions: A Simple Guide for Beginners

  πŸ“Œ What is Dependency Injection (DI)? Imagine you run a coffee shop ☕. You need coffee beans , milk , and a barista to make coffee. Instead of growing beans and milking cows yourself ( hardcoded dependencies 😡‍πŸ’«), you outsource them to suppliers ( Dependency Injection πŸ’‘). ✅ DI allows us to request dependencies instead of creating them manually. πŸ”Ή Why Use DI in Azure Functions? Azure Functions are serverless , meaning they scale automatically . Without DI: ❌ You manually create objects. ❌ Hard-to-maintain, repetitive code. ❌ No flexibility if requirements change. With DI: ✅ Services are created once and reused efficiently. ✅ Cleaner, more maintainable code. ✅ Works well in enterprise applications . πŸ›  How to Implement Dependency Injection in Azure Functions Step 1️⃣: Install Required Packages Your Azure Function App needs some extra tools: dotnet add package Microsoft.Azure.Functions.Extensions dotnet add package Microsoft.Extensions.DependencyInjection Step 2️⃣: Creat...

πŸš€ Say Goodbye to If-Else in C#: Use Design Patterns for Cleaner Code!

  πŸ™‹‍♂️ Why Do Developers Use Too Many If-Else Conditions? If you've ever written C# code, you've probably used if-else statements like this: public void ProcessLead ( string leadSource ) { if (leadSource == "Website" ) { Console.WriteLine( "Processing Website Lead..." ); } else if (leadSource == "Referral" ) { Console.WriteLine( "Processing Referral Lead..." ); } else if (leadSource == "Event" ) { Console.WriteLine( "Processing Event Lead..." ); } else { Console.WriteLine( "Unknown Lead Source" ); } } πŸ”΄ What's Wrong with This? Hard to Maintain : Every time a new lead type is added, you need to modify the method. Violates Open-Closed Principle (OCP) : The method is not open for extension but open for modification . Not Scalable : Imagine having 10+ conditions —the code becomes unmanageable! ✅ Solution 1: Strategy P...

Integrating Dynamics 365 CRM with MuleSoft Using a Synchronous C# Plugin

  πŸ”— πŸš€ Step 1: Set Up Your C# Plugin in Dynamics 365 CRM 1️⃣ Create a New C# Plugin Project in Visual Studio Open Visual Studio → Create a Class Library (.NET Framework) Project Install the Microsoft.CrmSdk.CoreAssemblies NuGet package Add a new class called LeadCreatePlugin.cs πŸš€ Step 2: Write the Synchronous C# Plugin Code πŸ”Ή Plugin Code to Capture New Leads & Send Data to MuleSoft using System; using System.ServiceModel; using Microsoft.Xrm.Sdk; using System.Net.Http; using System.Text; public class LeadCreatePlugin : IPlugin { public void Execute ( IServiceProvider serviceProvider ) { // Get the execution context IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService( typeof (IPluginExecutionContext)); // Run the plugin only when a lead is created if (context.MessageName.ToLower() != "create" ) return ; // Retrieve the Lead entity data if (context.InputParamet...

How to Integrate Dynamics 365 CRM with MuleSoft – A Beginner’s Guide

  πŸ”—  πŸ’‘ Why Do We Need Integration? Imagine you work at a company where the Sales Team uses Dynamics 365 CRM to manage leads, and the Finance Team uses an ERP system (like SAP) for invoicing. πŸ”Ή The problem? Both systems don’t talk to each other! πŸ”Ή The solution? MuleSoft! 🎯 MuleSoft acts like a bridge πŸŒ‰ between different systems, helping them communicate smoothly without manual work. πŸ“Œ What Will We Do? In this guide, we will: ✅ Connect Dynamics 365 CRM with MuleSoft ✅ Fetch Lead Data from CRM ✅ Send the Data to an ERP System (or another app like Salesforce, Database, etc.) πŸš€ Step-by-Step Integration Guide Step 1️⃣ – Register an App in Azure AD for CRM Access To allow MuleSoft to access CRM data , we need to register an app in Azure Active Directory . πŸ”Ή Steps to Register the App 1️⃣ Log in to Azure Portal → Go to Azure Active Directory 2️⃣ Click App Registrations → New Registration Name: MuleSoft-CRM-Integration Supported Account Type: Choose Single Tenant Re...

Understanding Auto-Numbering in a Multi-Transaction System

Image
  Introduction Auto-numbering is a common requirement in business applications, ensuring each record (such as an invoice, order, or customer ID) gets a unique number. However, when multiple users or processes try to generate numbers at the same time, conflicts can arise. This blog explains how a system ensures safe and sequential auto-numbering when multiple transactions happen simultaneously. What Does the Diagram Represent? The diagram below illustrates how two different transactions (Event Pipeline 1 & Event Pipeline 2) interact with the Auto-Number Table to generate unique numbers while maintaining data consistency. Each process follows a structured approach: Initiate Transaction – A request is made to create a new account with an auto-number. Lock the Auto-Number Table – The system temporarily prevents other processes from modifying the number. Read the Latest Number – The system retrieves the most recent number (e.g., 7). Increment the Number – It updates the table...

Automating Unique Number Generation in Dynamics 365 Using Plugins

Introduction When working with Dynamics 365, generating unique and sequential numbers for records such as invoices, quotes, or orders is a common requirement. Instead of manually assigning these numbers, we can use an Auto-Numbering Plugin to automate the process. In this blog, we’ll break down a C# plugin that implements this functionality in an easy-to-understand way. Why Do We Need Auto-Numbering? Manual numbering is prone to errors, duplication, and inefficiency. Automating this ensures: Unique numbering for each record Improved data consistency A structured format (e.g., INV-1001, INV-1002 ) How Does the Plugin Work? The plugin follows a structured approach to generate and assign unique numbers: Retrieve the Auto-Number Configuration Record Lock the record to prevent conflicts Increment the current number and update it Assign the new number to the created entity Let’s break down the code into simple steps. Understanding the Code Complete C# Code for Auto-Numbering Plugin...

Why is Delegated Permission Required When Creating an Azure Application User?

  🎭 A Simple Real-Life Example Imagine you work in a big office , and you need access to a locked room . There are two ways you can enter: 1️⃣ You walk in with your boss (delegated permission) – Your boss has access, and you are allowed in only while they are with you . 2️⃣ You get your own key (application permission) – You can enter anytime, even when your boss is not there. This is exactly how permissions work in Azure! πŸ”Ή Understanding Delegated Permission in Azure When you create an Azure Application User , it needs permission to access resources like Dynamics 365 or APIs . πŸ”‘ Delegated Permission → The app acts on behalf of a real user (requires an active user session). πŸ—️ Application Permission → The app has full access to the resource, even if no user is logged in. πŸ”Ž Key Differences Feature Delegated Permission Application Permission Acts as a user? ✅ Yes ❌ No Requires login? ✅ Yes ❌ No Follows user’s access? ✅ Yes (limited access) ❌ No (full access) Security risk...

Dynamics CRM Impersonation – Explained Simply!

  πŸ€” What is Impersonation in Dynamics 365? Imagine you work in a big company , and the CEO asks their assistant to send an email on their behalf . The assistant does the work, but the email appears to come directly from the CEO . This is exactly how impersonation works in Dynamics 365 CRM ! πŸš€ 🎯 Why Do We Need Impersonation? Impersonation allows a user (or system process) to perform actions on behalf of another user without needing their credentials. ✅ Security – No need to share passwords. ✅ Audit Trail – CRM logs actions under the right user. ✅ Automation – Background processes can work as real users. ✅ External Integrations – Third-party apps can act on behalf of users. πŸ“Œ Real-World Example: Customer Service Team Imagine a customer submits a complaint in CRM . Instead of a system-created record, CRM logs the issue under the right customer service agent . ✔ The system impersonates the agent. ✔ The complaint is stored as if the agent entered it manually . ✔ This keeps ...

Understanding Azure OAuth with a Simple Example

  OAuth can seem complex, but let’s break it down with a real-world scenario :         Scenario: Watching a Movie in a Theater Imagine you want to watch a movie in a theater. There’s a security process before you can enter. This is similar to how Azure OAuth works!   Movie Theater Example 🎬 Azure OAuth Equivalent πŸ” You (User) πŸ§‘‍πŸ’» End-User, App, or Client Security Guard 🚨 API or Protected Resource Ticket Counter 🎟️ Azure AD (Authentication Provider) ID Proof πŸ†” Username/Password or MFA Ticket 🎫 Access Token (JWT Token) Expired Ticket ⏳ Expired Token (Needs Refresh)      How Does This Work? 1️⃣ You (User) want to enter a movie theater → (You want to access an Azure-protected resource like an API or app.) 2️⃣ The Security Guard (API) checks tickets at the entrance → (The API verifies if you have permission.) 3️⃣ Before entering, you need a ticket (Access Token). 4️⃣ To get the ticket, you visit the Ticket Counter (Azure AD) and...