Wednesday, 8 June 2016

Dependency Injection (DI) and Inversion of Control (IOC)

Agenda
  • What is Inversion of Control (IOC)?
  • What is Dependency Injection (DI)?
  • Ways of achieving Dependency Injection
  • Advantages of implementing this principle
Inversion of Control 
We all have seen our college days, right?



In college, sometimes there are events being organized and sometime boring lectures :( . Just considering and remembering our college days let's try to relate the College and Events with Inversion of Control (IOC).

Inversion of Control

Suppose I have a class say College and another class say TechEvents. As you can see in the preceding figure there are many problems that may arise:
  1. Both classes are tightly coupled with each other. I cannot have a College without TechEvents because a TechEvents object is created in a College Constructor.
  2. If I make any changes to TechEvents, I need to compile or you can say update College too.
  3. College controls the creation of Events. College is aware of the single event that is organized. If there is any specific event to be organized like Weekend FootballEvent or PartyEvent, then there needs to make changes to a College class as College is directly referring to Events.
 
Now I need to solve this problem somehow else we would not be able to have any other Events ever in college.
The solution to this could be to shift the control of events organization to some other place.This we call Inversion of Control (IOC), inverting the control to some other entity instead of organizing the event in College directly itself.
What Inversion of control principle says?
Don't Call Me, we will call you
In other words, the Main class should not have a concrete implementation of an aggregated class, rather it should depend on abstraction of that class. The College class should depend on TechEvents class abstraction using an interface or abstract class.

Dependency Injection
IOC can be done using Dependency Injection (DI). It explains how to inject the concrete implementation into a class that is using abstraction, in other words an interface inside. The main idea of dependency injection is to reduce the coupling between classes and move the binding of abstraction and concrete implementation out of the dependent class.
In Simple words, DI is how one object know about other dependent object which is abstracted.
There are mainly 4 ways of achieving the Dependency Injection.



1. Injection via Constructor
This methodology is already discussed above, where the object of the concrete class is passed to the constructor of the dependent class.
  1. class College  
  2. {  
  3.         private IEvent _events;  
  4.         public College(IEvent ie)  
  5.         {  
  6.             _events = ie;  
  7.               
  8.         }  
  9.   
  10.        public void GetEvents()  
  11.         {  
  12.             this._events.LoadEventDetail();  
  13.         }  
  14.   
  15. }  
As you can see above, the event object is injected by the constructor keeping it loosely coupled. The College class will do his work and if it wants to get the events related details, it will call it in the constructor based on which event he wants to call.
  1. College coll = new College(new FootballEvent());  
Along with this advantage, another advantage is that if there are any changes in events or added some more events then College doesn't need to care about that.

2. Injection via Property

This is the most commonly used methodology where we inject the concrete class by creating a property whose type is of Interface.
  1. class College  
  2. {  
  3.         private IEvent _events;  
  4.         public IEvent MyEvent  
  5.         {  
  6.             set  
  7.             {  
  8.                 _events = value;  
  9.             }  
  10.         }  
  11. }  
As you can see above, the setter of the MyEvent property will take a concrete object and bind it to the interface. My class is loosely coupled from a concrete object. Now any changes to the any type of Event class will not affect my College class.
  1. College coll = new College();  
  2. coll.MyEvent = new FootballEvent();  
3. Injection via Method

In this methodology, the concrete class object is passed through the method parameter to the dependent class.
  1. class College  
  2. {  
  3.         private IEvent _events;  
  4.         public void GetEvent(IEvent myevent)  
  5.         {  
  6.             this._events = myevent;  
  7.               
  8.         }  
  9. }  
As you can see above, I have called the event of college using the GetEvents() method where the type of event is passed as a parameter that is of abstract type. This will help me to add or make changes to events without affecting the College, in other words both are decoupled. This is how I can call the method.
  1. College coll = new College();  
  2. coll.GetEvent(new FootballEvent());  
4. Injection via Service Locator

A service locator can act like a simple runtime mapper. This allows code to be added at run-time without re-compiling the application and in some cases without having to even restart it.

  1. class College  
  2.     {  
  3.         private IEvent _events = null;  
  4.         EventLocator el = new EventLocator();  
  5.         public College(int index)  
  6.         {  
  7.             this._events = el.LocateEvent(index);  
  8.         }  
  9.     }  
  10.   
  11.     class EventLocator  
  12.     {  
  13.         public IEvent LocateEvent(int index)  
  14.         {  
  15.             if (index == 1)  
  16.                 return new FootballEvent();  
  17.             else if (index == 2)  
  18.                 return new PartyEvent();  
  19.             else  
  20.                 return new TechEvents();  
  21.         }  
  22.    }  
In the code snippet above, you can see that there is an EventLocator class between the Events and College that helps us to locate the service without knowing the concrete type. I am just passing the index value in the constructor that in turn calls the third party to locate the event and return it to the constructor. Hence any changes to EventLocator will not affect the College class.
  1. College coll = new College(1);  
  2. coll.GetEvents();  
Advantages of implementing this principle
  • It helps in class decoupling.
  • Due to decoupling, the reusability of the code is increased.
  • Improved code maintainability and testing.

Wednesday, 11 May 2016

Singleton Design Pattern - C#

Singleton pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance. In this article, I would like share what is Singleton pattern and how is it work?

What is Singleton Pattern?

Singleton pattern is one of the simplest design patterns. This pattern ensures that a class has only one instance and provides a global point of access to it.

Singleton Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Singleton design pattern is given below:
The classes, and objects in the above UML class diagram are as follows:
  1. Singleton

    This is a class which is responsible for creating and maintaining its own unique instance.

C# - Implementation Code

  1. //eager initialization of singleton
  2. public class Singleton
  3. {
  4. private static Singleton instance = new Singleton();
  5. private Singleton() { }
  6.  
  7. public static Singleton GetInstance
  8. {
  9. get
  10. {
  11. return instance;
  12. }
  13. }
  14. }
  15.  
  16. ////lazy initialization of singleton
  17. public class Singleton
  18. {
  19. private static Singleton instance = null;
  20. private Singleton() { }
  21.  
  22. public static Singleton GetInstance
  23. {
  24. get
  25. {
  26. if (instance == null)
  27. instance = new Singleton();
  28.  
  29. return instance;
  30. }
  31. }
  32. }
  33.  
  34. ////Thread-safe (Double-checked Locking) initialization of singleton
  35. public class Singleton
  36. {
  37. private static Singleton instance = null;
  38. private Singleton() { }
  39. private static object lockThis = new object();
  40.  
  41. public static Singleton GetInstance
  42. {
  43. get
  44. {
  45. lock (lockThis)
  46. {
  47. if (instance == null)
  48. instance = new Singleton();
  49.  
  50. return instance;
  51. }
  52. }
  53. }
  54. }

Singleton Pattern - Example

Who is what?

The classes and objects in the above class diagram can be identified as follows:
  1. Singleton - Singleton class

C# - Sample Code

  1. /// <summary>
  2. /// The 'Singleton' class
  3. /// </summary>
  4. public class Singleton
  5. {
  6. // .NET guarantees thread safety for static initialization
  7. private static Singleton instance = null;
  8. private string Name{get;set;}
  9. private string IP{get;set;}
  10. private Singleton()
  11. {
  12. //To DO: Remove below line
  13. Console.WriteLine("Singleton Intance");
  14.  
  15. Name = "Server1";
  16. IP = "192.168.1.23";
  17. }
  18. // Lock synchronization object
  19. private static object syncLock = new object();
  20.  
  21. public static Singleton Instance
  22. {
  23. get
  24. {
  25. // Support multithreaded applications through
  26. // 'Double checked locking' pattern which (once
  27. // the instance exists) avoids locking each
  28. // time the method is invoked
  29. lock (syncLock)
  30. {
  31. if (Singleton.instance == null)
  32. Singleton.instance = new Singleton();
  33.  
  34. return Singleton.instance;
  35. }
  36. }
  37. }
  38.  
  39. public void Show()
  40. {
  41. Console.WriteLine("Server Information is : Name={0} & IP={1}", IP, Name);
  42. }
  43.  
  44. }
  45.  
  46. /// <summary>
  47. /// Singleton Pattern Demo
  48. /// </summary>
  49. ///
  50. class Program
  51. {
  52. static void Main(string[] args)
  53. {
  54. Singleton.Instance.Show();
  55. Singleton.Instance.Show();
  56.  
  57. Console.ReadKey();
  58. }
  59. }

Singleton Pattern Demo - Output

When to use it?

  1. Exactly one instance of a class is required.
  2. Controlled access to a single object is necessary

Recent Post

how to control duplicate order