Skip to main content

Posts

                                                                                      Delegates in C# You want to pass a function as a Parameter ? How does C# handles a callback function or an event handler ? The answer of all these questions is one i.e. a Delegate. Delegate is a reference type datatype that defines a method prototype. We can define a varibale of delegate just like any other datatype that can refer to any method with the same prototype i,e, signature.  Define a Delegate => Set a Target Method => Invoke  eg: public delegate void MyFirstDelegate(string str); // Declaration Note: declaration can be done outside or even inside the class. static void MethodA(string str) { Console.Writeline(str); } //Setting up Target Method 1) MyFirstDelegate del = new  MyFirstDelegate(MethodA);  2) MyFirstDelegate del= MethodA; 3) MyFirstDelegate del = (string msg) => Console.Writeline(msg); //Invoke  del.Invoke("Hello World!"); del("Hello World");
Recent posts

Database Factory Design Pattern C#

Many developers has confusion on how to write database code in application. I will discuss several approaches here and try to provide explanation on each approach. Approach 1 : Easiest approach which is very straight forward which will work absolutely fine. The issues we will get to manage the project in terms of software principles. Like if you going to change Database provider, you would need to change using statements and other issue like mixing Database code with business logic etc. I will not go in detail here, I am assuming you already have an understanding of design principles. I am just exploring different approaches to achieve the database connectivity. using System.Collections.Generic; using System.Data.SqlClient; namespace DatabaseFactory {     class Program     {         static void Main(string[] args)         {             var employees = SomeDabOperation();         }         private static List<Employee> SomeDabOperation()         {             u

Dependency Injection and Inversion Of Control C# - Par 2 (Implementation)

Before going through this post, I suggest you to go through my previous post on basic concepts in Dependency injection and Inversion of control. Demo on product. This demo is based on a traditional implementation approach: We have a Product class and it has a method which takes the discount and based on some business logic it applies the discount on the product. Product service will interact with database using ProductRepository. If you will notice ProductService has dependency on 2 concrete classes i.e. Discount and ProductRepository new Discount(); new ProductRepository(); We can hence say that ProductService is tightly coupled because of these 2 dependencies. We may require to use different discount based on several factors such as Product type, Some Festival or any other factor. Based on different discounts we may need to change ProductService class also in future you may change the database from SQL to Oracle, Again it may require ProductService changes which is viol

Dependency Injection and Inversion Of Control C# - Par 1 (Concept )

Dependency Injection is one of the SOLI" D " principle which helps to decouple the code by making sure that we should depend on abstractions not in implementation of concrete classes. The concept is Inversion of control. Inversion Of Control It was considered as a programming style used to invert the flow of control. In Procedural language a code that consumes dependent code is in control of the process i.e.  It knows what exactly what method to use and sometimes most like what the implementation is. To achieve some goal one class need to consume some other class now question is the consumer class really needs to know about dependency on dependent class ? It is enough for a consumer class to know the properties, behavior etc. of the dependent class rather than knowing which is the dependent class having those properties, behavior etc.. I mean , we can abstract the behavior which is required to use by a consumer in the dependent class and allowing a consumer

Command Design Pattern in C#

Command Design Pattern in C# Command Design Pattern is a type of Behavioral Design Pattern. Behavioral Design Pattern :   It's about object communication, their responsibilities and how they communicate each other. There might be a situation where we want to encapsulate required information in a Object to perform some task and the task can be perform many times or whenever its required. The command design pattern is the solution. It also gives you easy way to implement Undo() that can just undo multiple command. Implementation : Typically Implementation of Command Pattern is divided into 4 parts. Command:   That executes an action. Receiver:  Objects that receive the action from the Command. Invoker:  Invoke the Commands to execute their actions. The Invoker may be a queue that holds commands for future execution, or hold such commands which can be used by different application/machine can be used to execute commands multiple times or can be used to undo the comma