Hi Folks! In this post I would like to share my learnings on Unity Container which is one the IoC Container Frameworks. We will see how to configure Unity container both in older version and newer version. Why both? Because there is a slight change in configuration. So, let’s start.
Consider below are the projects in a Visual Studio solution.
unity container demo projects
Here each of the projects references interface project.(This is just a demo example to understand dependency injection using configuration)

Unity v5.2.1 installed in Entities class library.
Manage Nuget Packages - Unity Container

IDataProvider.cs

DataProvider.cs (OracleDataProvider)

DataProvider.cs (SQLDataProvider)

To avoid hard reference to OracleDataProvider and SQLDataProvider in DIFrameworkDemo, I have used xcopy in Post-build event command line of the DataProvider class libraries.

App.config DIFrameworkDemo

DataProviderFactory.cs

Note: Make sure you update your config if you upgrade to 5.2.1 or newer version otherwise you will get below error.

An unhandled exception of type ‘System.Configuration.ConfigurationErrorsException’ occurred in System.Configuration.dll
Additional information: An error occurred creating the configuration section handler for unity: Could not load file or assembly ‘Microsoft.Practices.Unity.Configuration’ or one of its dependencies. The system cannot find the file specified.

This happens because Microsoft.Practices.Unity.Configuration.UnityConfigurationSection class is moved to Unity.Configuration.dll.

Hope this helps.

References:
https://msdn.microsoft.com/en-us/library/ff647202.aspx
https://msdn.microsoft.com/en-us/library/ff660914(v=pandp.20).aspx
https://msdn.microsoft.com/en-us/library/dn223671(v=pandp.30).aspx

LEAVE A REPLY

Please enter your comment!
Please enter your name here

*