Property Injection In C#

The main intension of the object-oriented programming is to manipulate real world objects and one of the features is reusability. There are many ways to get reusability and one of the easiest ways is ‘Inversion of Control’ in architectural patterns especially.

There are many techniques to implement ‘Inversion of Control’, find the following image for more conceptual view.

Inversion of Control

Now let’s talk about Dependency Injection. Here also same, we can Implement DI (Dependency Injection) with help of constructor, Property, Interface and Interface. This resource deals only Property Injection.

What is Injection and why it is helpful?


Injection is a technique to change object behavior at runtime.

Property Injection

Inject object through property properties with help of get and set.

Get a sample project and add Interface components to it, for more information

IdataActions is an Interface for method declarations.

  1. public interface IdataActions   
  2. {  
  3.    void AddCustomerEntry(IdataProperties iDataProperties = null);  
  4. }  
IdataProperties for property declaration’s.
  1. public interface IdataProperties  
  2. {  
  3.    string customerName { getset;}  
  4.    int customerId { getset; }  
  5.    string customerAddress { getset; }  
  6. }  
IdataProperties

Now let’s add PropertyInjection.cs class to implement IdataProperties and set to property injection.

property injection
  1. public class PropertyInjection: IdataProperties  
  2. {  
  3.     public IdataProperties iDataProperties  
  4.     {  
  5.         get  
  6.         {  
  7.             return new PropertyInjection();  
  8.         }  
  9.     }  
  10.     public string customerName  
  11.     {  
  12.         get  
  13.         {  
  14.             return "CustomerName";  
  15.         }  
  16.         set  
  17.         {  
  18.             // throw new NotImplementedException();  
  19.         }  
  20.     }  
  21.     public int customerId  
  22.     {  
  23.         get  
  24.         {  
  25.             return 123;  
  26.         }  
  27.         set  
  28.         {  
  29.             // throw new NotImplementedException();  
  30.         }  
  31.     }  
  32.     public string customerAddress  
  33.     {  
  34.         get  
  35.         {  
  36.             return "India";  
  37.         }  
  38.         set  
  39.         {  
  40.             // throw new NotImplementedException();  
  41.         }  
  42.     }  
  43. }  
Finally, implementation like the following,
  1. class Program: IdataActions  
  2. {  
  3.     PropertyInjection propertyInjection = new PropertyInjection();  
  4.     public void AddCustomerEntry(IdataProperties iDataProperties = null)  
  5.     {  
  6.         if (iDataProperties == null)  
  7.         {  
  8.             // propertyInjection.iDataProperties : carried out your data, In other words this property   
  9.             // will generate readymate data to your Entry operations. It is very best suite to   
  10.             // Test Cases.  
  11.             iDataProperties = propertyInjection.iDataProperties;  
  12.             Console.WriteLine("customerId" + iDataProperties.customerId);  
  13.             Console.WriteLine("customerName" + iDataProperties.customerName);  
  14.             Console.WriteLine("customerAddress" + iDataProperties.customerAddress);  
  15.         }  
  16.         Console.WriteLine("Customer has been created");  
  17.     }  
  18.     static void Main(string[] args)  
  19.     {  
  20.         Program progrm = new Program();  
  21.         progrm.AddCustomerEntry();  
  22.         Console.Read();  
  23.     }  
  24. }  
Code

Run and verify. 

Up Next
    Ebook Download
    View all
    Learn
    View all