I am starting with dependency injection, and having hard time obstacting some of the third party library classes. For example I have EPPlus library in my project which has ExcelRange class which doesn't implement interface. Since I am using this library I am finding my code being explicitly dependent and can't properly unit test some parts of the code.
So my question is what is good way to use Dependency Injection with 3rd party libraries classes.
My resolution for this scenario is to create another class and interface as wrapper to your third party library. In your wrapper, create those functions same name as what you have used in you third party library. Create only those functions that has value with your code and little by little if you need another functions add it up in your wrapper. Now, for testing purposes you can mock/stub your wrapper interface without using your third party library. Use your wrapper to inject to other class that need this service.
You can start with simple code and expand it as your knowledge grow :
public interface IWrapperService
{
Method(Dto model);
Dto MethodProcess(Dto model);
}
public class WrapperService : IWrapperService
{
private readonly ThirdPartyLib _thirdPartyLib;
public WrapperService(ThirdPartyLib thirdPartyLib)
{
_thirdPartyLib = thirdPartyLib;
}
// Create your model - Dto
// Dto will help you in your logic process
//
public void Method(Dto model)
{
//extract some properties in you model that only needed in your third party library
_thirdPartyLib.Method(parameter needed);
}
public Dto MethodProcess(Dto model)
{
//extract some properties in you model that only needed in your third party library
ThirdPartyReturn value = _thirdPartyLib.MethodProcess(parameter needed);
// Do the mapping
var model = new Dto
{
property1 = value.property1 // Do the necessary convertion if needed.
.
.
}
return model;
}
.
.
.
}
public interface IOtherClass
{
...
}
public class OtherClass : IOtherClass
{
private readonly IWrapperService _wrapperService;
public void OtherClass(IWrapperService wrapperService)
{
_wrapperService= wrapperService;
}
.
.
}
For dependency injection you can use Microsoft unity. It will do the amazing job for your dependency. You can used it like this :
var unity = new UnityContainer();
// This is how you will inject your ThirdPartyLib
// You can also do it this way - unity.RegisterType<ThirdPartyLib>() but of course we need to limit the usage of your ThirdPartyLib in
// our wrapper. We will not allowing directly access to Third Party Lib rather than wrapperService.
unity.RegisterType<IWrapperService, WrapperService>(new InjectionConstructor(new ThirdPartyLib()));
unity.RegisterType<IOtherClass, OtherClass>();
I agreed with @Alexei Levenkov, you need to read some stuff about Gang of Four (GOF) to improve this sample. Make my sample as your starting point.
Wrapping your Third party library gives you the following advantages:
Few Disadvantages :
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With