Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best way to wrap 3rd party class c# [closed]

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.

like image 475
Farukh Avatar asked Jul 21 '15 04:07

Farukh


1 Answers

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:

  • It eliminate scattered and direct usage of your Third Party Lib.
  • It encapsulate some complexity in your third party lib.
  • It is easy to track and maintain your third party lib through your wrapper.
  • Unit testing will be easy now through the use of wrapper.
  • And dependency injection will help you for cross cutting concern.

Few Disadvantages :

  • Tedious and introduce duplication of methods.
  • Introduce the creation of new models - It depends, If your third party lib only asking few parameters like (int, string, boolean) don't bother for models.
  • It might be difficult at first to apply Design Pattern but it will give you advantage in the long run.
like image 140
jtabuloc Avatar answered Sep 21 '22 23:09

jtabuloc