.net - What is best way to wrap 3rd party class c# -


i starting dependency injection, , having hard time obstacting of third party library classes. example have epplus library in project has excelrange class doesn't implement interface. since using library finding code being explicitly dependent , can't unit test parts of code.

so question way use dependency injection 3rd party libraries classes.

my resolution scenario create class , interface wrapper third party library. in wrapper, create functions same name have used in third party library. create functions has value code , little little if need functions add in wrapper. now, testing purposes can mock/stub wrapper interface without using third party library. use wrapper inject other class need service.

you can start simple code , expand 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 model - dto     // dto in logic process     //      public void method(dto model)     {            //extract properties in model needed in third party library          _thirdpartylib.method(parameter needed);     }      public dto methodprocess(dto model)     {            //extract properties in model needed in third party library          thirdpartyreturn value = _thirdpartylib.methodprocess(parameter needed);          // mapping         var model = new dto          {             property1 = value.property1 // 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 can use microsoft unity. amazing job dependency. can used :

var unity = new unitycontainer();  // how inject thirdpartylib // can way - unity.registertype<thirdpartylib>() of course need limit usage of thirdpartylib in  // our wrapper. not allowing directly access third party lib rather wrapperservice.  unity.registertype<iwrapperservice, wrapperservice>(new injectionconstructor(new thirdpartylib())); unity.registertype<iotherclass, otherclass>(); 

i agreed @alexei levenkov, need read stuff gang of 4 (gof) improve sample. make sample starting point.

wrapping third party library gives following advantages:

  • it eliminate scattered , direct usage of third party lib.
  • it encapsulate complexity in third party lib.
  • it easy track , maintain third party lib through wrapper.
  • unit testing easy through use of wrapper.
  • and dependency injection cross cutting concern.

few disadvantages :

  • tedious , introduce duplication of methods.
  • introduce creation of new models - depends, if third party lib asking few parameters (int, string, boolean) don't bother models.
  • it might difficult @ first apply design pattern give advantage in long run.

Comments