在 API 中从延迟/延迟加载转换为急切加载的首选方法?
我已经研究一个 API(它包装了某种 Web 服务)有一段时间了,它的功能已经很完整了。
我最初将该 API 设计为自始至终都是惰性/延迟加载的;考虑到使用 Web 服务固有的延迟,如果您只对可用数据的一小部分感兴趣,那么这是非常有意义的。然而,我没有考虑到一些用例,在这些用例中,急切加载对于 API 的使用者来说会更容易。
所以,我的问题是:您希望看到一个主要是延迟加载的 API 公开一种获取类的急切加载版本的机制吗?
我倾向于显式
强制转换,但是Eager.AsEager(SomeDelayLoadingObject)
的一些内容如果更冗长的话似乎也很自然。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上最终做的是创建我想要急切加载的类的浅表副本,这些类没有功能代码但具有所有相同的属性。
然后,我定义了两个隐式转换:lazy->eager 和 eager->lazy。两次转换都复制了所有属性;从而在需要时触发任何加载。
虽然我不认为这是一个完美的解决方案,但它使得急切地加载简单的更改类型;无需更改代码。
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.
您的 API 正在包装 Web 服务,因此您的 API 或多或少充当代理。我会将加载行为放在代理本身或某种代理上下文上,而不是放在类上。
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.