DI is a simple concept where you inject and use the services that you need, making your application highly efficient and lightweight.
As already discussed in our previous article, there are a lot of benefits of DI approach like,
- Separation of concern
- Independently deployable unit
- Easily Testable units
- High performance
- Easy maintenance
Here below I would like to inject the IBusinessLayer and ILogger object through Forms object.
Add ConfigureServices() method
Please add below ConfigureServices() method. This method can be used for registering all the required services within Service collection.
Let’s now add Form1 which is currently my master Form and has the UI logic. Please register Form1 to above as services.
Adding DI Container
Please update the Main() method as below.
We shall be injecting ILogger and IBusinessLayer object is as below,
Here we are assuming Form1 is the master form that holds other forms together.
In the above code, we added Form, logging ILogger, and IBusinessLayer interface to services collection and built the ServiceProvider for the required services. With that, both the logger object and business object instance can be DI via Constructor injection as and when required in other layers.
Add IBusinessObject and ILogger using DI to Forms App
Complete sample code is as below,
Let’s execute the application, we shall see all layers executed with logging.
Today we learned an easy approach of adding the Dependency Injection (DI) in newly supported Windows forms applications in .NET Core 3.0 and above. We understood with few custom changes as mentioned above, we can very much leverage DI in Desktop or Forms application and can create maintainable and performative Win Form or Desktop applications.