Property Injection in C#

Introduction

In a programmer’s world, Dependency Injection (DI) frameworks offer two ways to inject concrete classes into objects: constructor injection and property injection. In the case of property injection, the dependency injector calls a property after the object is instantiated, passing the dependency into the object to be saved and utilized later. In this article, I will demonstrate property injection using the Unity container. Unity is an open source lightweight and extensible IoC container for .NET applications supported by Microsoft.

Property Injection

First, we have to create a project in Visual Studio to use Unity container. It can be any type of Visual Studio project, such as a class library, a console, a Web, and windows or any other C# or VB.NET project. To demonstrate, I have created a Console Application by clicking New Project from the Start page of Visual Studio. It will open the New Project popup, as shown in Figure 1. I have named the project ‘PrjPropertyInjection’ and clicked OK. This will create a new console application project.

New Visual Studio Project
Figure 1: New Visual Studio Project

Next, I have installed Unity Framework to use dependency injection in the project. To install Unity, right-click the project node in the Solution Explorer and select Manage NuGet Packages, as shown in Figure 2. Now, we can search for Unity from the browse tab of NuGet. Enter “unity” in the search box; it will list all the libraries or plug-ins that contain the word “unity.” Now, click the Install button in the right panel.

Install NuGet Packages
Figure 2: Install NuGet Packages

Performing Property Injection

To explain perform property injection using Unity container, consider the following example of Interfaces and classes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PrjPropertyInjection
{
   public interface ICar
   {
      int Run();
   }
}

As you can see in the following sample classes, the Driver class is dependent on a property of type ICar. So, we need to set an object of a class that implements ICar to the Car property using the Unity container.

Property injection in the Unity container can be implemented in two ways:

  • Using the [Dependency] attribute
  • Using run-time configuration

The dependency attribute for the property injection tells the Unity container which property to inject. So, we need to decorate the dependent properties with the [Dependency] attribute, as shown in the following Driver class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;

namespace PrjPropertyInjection
{
   public class Maruti : ICar
   {
      private int _miles = 0;

      public int Run()
      {
         return ++_miles;
      }
   }

   public class Honda : ICar
   {
      private int _miles = 0;

      public int Run()
      {
         return ++_miles;
      }
   }

   public class Mahindra : ICar
   {
      private int _miles = 0;

      public int Run()
      {
         return ++_miles;
      }

   }


   public class Driver
   {

      [Dependency]
      public ICar Car { get; set; }

      public Driver(ICar car)
      {

      }

      public void RunCar()
      {
         Console.WriteLine("Running {0} - {1} mile ",
            this.Car.GetType().Name, this.Car.Run());
      }
   }
}

For run-time configuration, the Unity container allows us to configure a property injection with the RegisterType() method if a method is not marked with the [Dependency] attribute. You can pass an object of the InjectionProperty class in the RegisterType() method to specify a property name and a parameter value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;


namespace PrjPropertyInjection
{
   class Program
   {
      static void Main(string[] args)
      {
         // Without Unity Container
         Driver driver = new Driver(new Maruti());
         driver.RunCar();
         Console.Read();

         // With Unity Container
         IUnityContainer container = new UnityContainer();
         container.RegisterType<ICar, Maruti>();
         container.RegisterType<ICar, Honda>(("LuxuryCar"));
         Driver drv = container.Resolve<Driver>();
         drv.RunCar();

         Driver driver2 = container.Resolve<Driver>();
         driver2.RunCar();

      }
   }
}

In the preceding example, container.RegisterType<driver> registers the Driver class by passing an object of InjectionProperty that specifies the property name “Car” and the Maruti object as a value.

Conclusion

That was all about Property Injection using the Unity container. I hope this article was helpful! That’s all for today; happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read