mercredi 10 février 2016

Do I need a wrapper for third party calls that return interface?

Consider I need to call a 3rd party API that looks like

public class ThirdPartyClass : IThirdPartyClass
{
    public IThirdPartyReturnObject ThirdPartyMethod()
    {
        //some code
    }
}

And I am calling it like:

public void MyClass
{    
    private IThirdPartyClass _thirdPartyObject;
    public MyClass(IThirdPartyClass thirdPartyObject)
    {
        _thirdPartyObject = thirdPartyObject;
    }

    public void MyFunction()
    {
        _thirdPartyObject.ThirdPartyMethod();
    }
}

Since the third party class is injected by an interface, I can mock the 3rd party object for unit tests.

Is this design better, or should I introduce a ThirdPartyClassWrapper that encapsulates just the calling of ThirdPartyMethod? Is introducing too many wrappers an anti-pattern?

Aucun commentaire:

Enregistrer un commentaire