Question

I've been working on an API (which wraps a web-service of sorts) for a while now, and its just about feature complete.

I initially designed this API to be lazy/delay-loaded throughout; which makes perfect sense if you're only interested in a small subset of the available data given the latency inherent in consuming a web-service. However, I failed to consider a few use cases where eager loading would be much easier on a consumer of the API.

So, my question is: How would you like to see an API that is predominately lazy-loading expose a mechanism for getting eagerly-loaded versions of classes?

I'm leaning towards an explicit cast, but something along the lines of Eager.AsEager(SomeDelayLoadingObject) also seems natural if more verbose.

Was it helpful?

Solution

What I actually ended up doing was creating a shallow copy of the classes I wanted to be eagerly loaded, classes with no functional code but with all the same properties.

I then defined two implicit casts, from lazy->eager and eager->lazy. Both casts copied all properties; thereby triggering any loading if it was needed.

While I don't think this is a perfect solution, it makes eagerly loading simply changing types; no code changes are needed.

OTHER TIPS

Your API is wrapping a webservice, so your API is more or less acting as a proxy. I would put the loading behavior on the proxy itself, or on some kind of a proxy context, rather than on the classes.

// Basic: Every operation through the proxy uses Eager loading
using (var proxy = new ApiProxy(Loading.Eager))
{
  var result = proxy.DoSomething();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top