Using Implementation Factories to Register Dependencies in ASP.Net Core Dependency Injection

Using Implementation Factories to Register Dependencies in ASP.Net Core Dependency Injection

Automatic dependency creation is always preferred in ASP.Net Core Dependency Injection. But when you work with legacy code or other libraries, you many not always have that option. Implementation Factories in ASP.Net Core Dependency Injection offers more control over how the dependency is crated. For example, when you use Builders and Factories to create objects of the dependencies, you need to use some other option than automatic dependency creation. Let’s look at how we can use Implementation Factories in ASP.Net Core Dependency Injection

Using Implementation Factories to Register Dependency

For this example, let’s take the same e-commerce website example we took for the previous article on Registering and using multiple implementations. I have done some minor back-end changes to the services I have registered. I have an OrderDiscountProcessor that is responsible for processing the discount for the order. Then I have a DiscountBuilder that is responsible for building a Discount object that describes the discount amount and the conditions for that discount to be applicable. The OrderDiscountProcessor is dependent on the Discount object to be injected in to calculate the discount for the order. Simple enough, right?

The problem we have in this example is to registering Discount class as a dependency for IDiscountProcessor. The Discount object needs to be constructed using the extensions methods provided by the DiscountBuilder. So, in this scenario to register Discount as a dependency we need to use Implementation Factories.

The methods that are available in the IServiceCollection that is used to register dependencies allows you to define a delegate that is responsible for constructing the instance of the dependency we want to register. Not only methods like AddScoped(), AddSingleton() extension methods in IServiceCollection,  methods like TryAddScoped() and even if you are using ServiceDescriptor class directly with Static methods or helper methods they all support the use of delegate for dependency construction.

In this example, I have used AddScoped() method to register the Discount dependency.

gist:kasunkv/1dd9a95a7f98c55ca3147165e7e997cf#services.cs

Now I have the dependency registered, In my OrderDiscountProcessor implementation, I can depend on the Discount object injected in, to calculate the discount for a given order.

gist:kasunkv/e1faa6612430a4c78beeee730576a57f#OrderDiscountProcessor.cs

Summary

In this short article we looked in to how to use Implementation Factories when you need more control over how the dependencies are instantiated especially when you have no control over the dependency you are relaying upon. This is useful when you work with legacy code or working with third party libraries that are less flexible. The simple sample application used to demonstrate this usage is available for download with this article or you can find the source code on GitHub under the following repository

You Might Also Like
Comments