I am working to design an object which would abstract away the use of a WebAPI. I want the developer to be able to use these objects as if they are using a traditional API.
My core problem is how to abstract the interactions with the WebAPI. I currently use an Interface which is initialized in each element to provide basic calls. I am wondering if the element itself should implement the interface or if it should be an object within my class.
For now I am using the new Lazy object to restrict requests to their first use. I understand that this will require a more recent version of .Net.
public class AFElement : LazyPI.BaseObject
{
private AFElementTemplate _Template;
private Lazy<AFElement> _Parent;
private Lazy<IEnumerable<AFElement>> _Children;
private Lazy<IEnumerable<AFAttribute>> _Attributes;
private IAFElement _ElementLoader;
#region "Properties"
public AFElementTemplate Template
{
get
{
return _Template;
}
}
public AFElement Parent
{
get
{
return _Parent.Value;
}
}
public IEnumerable<AFElement> Children
{
get
{
return _Children.Value;
}
}
public IEnumerable<AFAttribute> Attributes
{
get
{
return _Attributes.Value;
}
}
#endregion
#region "Constructors"
private AFElement()
{
Initialize();
}
private void Initialize()
{
string parentPath = Path.Substring(0, Path.LastIndexOf('\\'));
//Load Template
//Load Parent
_Parent = new Lazy<AFElement>(() =>
{
AFElement ele = _ElementLoader.FindByPath(parentPath);
return ele;
}, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
_Attributes = new Lazy<IEnumerable<AFAttribute>>(() =>
{
return _ElementLoader.GetAttributes(this.ID).Cast<AFAttribute>().ToList();
}, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
_Children = new Lazy<IEnumerable<AFElement>>(() =>
{
return _ElementLoader.GetElements(this.ID);
}, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
}
// Initialized all basic references
//Strips element name from path to get parent path
#endregion
}