What is the best way to wrap a third party C # class

I am starting with dependency injection and have a hard time running through some 3rd party library classes. For example, I have an EPPlus library in my project that has an ExcelRange class that does not implement the interface. Since I use this library, I find that my code is clearly dependent and cannot properly unit test some parts of the code.

So my question is what is a good way to use Dependency Injection with 3rd party library classes.

+3


source to share


1 answer


My solution for this scenario is to create another class and interface to wrap your third party library. In your shell, create those functions with the same name as you that you used in your third party library. Only create functions that have value with your code and gradually if you need other functions add them to your wrapper. Now, for testing purposes, you can cheat / stub your wrapper interface without using your third party library. Use your wrapper to inject to another class that needs this service.

You can start with simple code and deploy as your knowledge grows:

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 an amazing job for your addiction. You can use 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 a thing or two about Gang of Four (GOF) to improve this sample. Take your sample as a starting point.

Wrapping your third party library gives you the following benefits:

  • This eliminates the scattered and direct use of your third batch of Lib.
  • It encapsulates some complexity in your third party lib.
  • It's easy to track and maintain your third party library through your wrapper.
  • Unit testing will now be easier thanks to the use of a wrapper.
  • And dependency injection will help you deal with cross-cutting issues.

Several disadvantages:

  • Boring and introduce method duplication.
  • Imagine creating new models. It depends. If your third party library only asking for a few parameters like (int, string, boolean) don't bother with the models.
  • A design pattern can be difficult to apply at first, but it will give you an edge in the long run.
+5


source







All Articles