It is often a good practice to limit the visibility of third party solutions to the most of your application. Most of the time you will create your own wrapper around a third party library to limit this kind of exposure. Wrapping is similar to the adapter pattern (but in reverse).
One problem I recently encountered, was to wrap some children of a base class but to keep a relation between the wrapped models, I needed the ability of grouping them.
Third party example:
Let’s consider that our external library provide us with two classes that derive from a base class:
public abstract class TPModel
{
public abstract string Name { get; set; }
}
public class TPModelChild1 : TPModel
{
public override string Name { get; set; }
public string ExtraProperty { get; set; }
}
public class TPModelChild2 : TPModel
{
public override string Name { get; set; }
public string ExtraProperty { get; set; }
}
Wrapper Interface:
Our wrapper interface is pretty basic, it just returns the third party instance:
public interface IWrapper where TOriginal : class
{
TOriginal Get();
}
Wrapped Models:
To keep a relation between the wrapped models, we implement the interface twice. Once for the child and once for the parent:
public class WrapperChildOne : IWrapper, IWrapper
{
public string Name { get; set; }
public string ExtraProperty { get; set; }
public TPModelChild1 Get()
{
return new TPModelChild1
{
Name = this.Name,
ExtraProperty = this.ExtraProperty
};
}
TPModel IWrapper.Get()
{
return this.Get();
}
}
public class WrapperChildTwo : IWrapper, IWrapper
{
public string Name { get; set; }
public string ExtraProperty { get; set; }
public TPModelChild2 Get()
{
return new TPModelChild2
{
Name = this.Name,
ExtraProperty = this.ExtraProperty
};
}
TPModel IWrapper.Get()
{
return this.Get();
}
}
Wrapped Relation:
Because we have a common interface between our wrapped models, we can now group them:
WrapperChildOne adapterChildOne = new WrapperChildOne { Name = "aa", ExtraProperty = "bb" };
WrapperChildTwo adapterChildTwo = new WrapperChildTwo { Name = "aa", ExtraProperty = "bb" };
List<IWrapper> listOfWrappers = new List<IWrapper>
{
adapterChildOne,
adapterChildTwo
};







