mardi 30 juin 2015

How to share external library interfaces between wrapper classes?

I want to wrap an external library so that other projects in the solution only have a reference to my wrapper code, and have no knowledge of the external library. My problem is with communicating the internals of the wrapper classes and interfaces between the other wrappers.

Say I have for example, the External Library:

namespace ExternalLibrary
{
    public interface IExtAlpha
    {
            ...
    }

    public interface IExtBeta
    {
        int DoTheThing(IExtAlpha extAlpha);
    }
}

And I create a Wrapper Project with appropriate interfaces:

namespace WrapperLibrary
{
    public interface IWrapExtAlpha
    {
            ...
    }

    public interface IWrapExtBeta
    {
        int DoTheThing(IWrapExtAlpha wrapExtAlpha);
    }
}

And a Wrapper Core project (containing the wrapper implementations)

namespace WrapperLibraryCore
{   
    public interface ICoreWrapExtAlpha : IWrapExtAlpha
    {
        ExternalLibrary.IExtAlpha ExtAlpha { get; }
    }

    public class WrapExtBeta : IWrapExtBeta
    {
        private readonly IExtBeta _extBeta;

        public WrapExtBeta(IExtBeta extBeta)
        {
            _extBeta = extBeta;
        }

        public int DoTheThing(IWrapExtAlpha wrapExtAlpha)
        {
            // The cast down to the ICoreWrapExtAlpha is where I'm concerned.
            var extAlpha = ((ICoreWrapExtAlpha)wrapExtAlpha).ExtAlpha;
            return _extBeta.DoTheThing(extAlpha);
        }
    }
}

What is the correct way to communicate the ExternalLibrary.IExtAlpha, wrapped by the IWrapExtAlpha, to the IWrapExtBeta wrapped by the WrapExtBeta class, when the IWrapExtAlpha cannot expose that IExtAlpha interface publically?

The solution I have so far is to down cast the interface (which feels wrong) to an interface that can share the external interface to other classes within that core library, with the requirement that any implementation of IWrapExtAlpha must actually implement the ICoreWrapExtAlpha instead. Is there a more idiomatic method?

Aucun commentaire:

Enregistrer un commentaire