我是否应该避免暴露 Lazy<T>公共 API 中的类? [英] Should I avoid exposing the Lazy<T> class in public API?
问题描述
在库的公共接口设计中,返回一个Lazy
在属性中,如果我想要延迟初始化?还是总是通过封装或其他技术隐藏Lazy
的使用是否更好?
In a design of the public interface of a library, is it legitimate to return an instance of Lazy<T>
in a property, if I want lazy initialization? Or is it better to always hide the usage of Lazy<T>
by means of encapsulation or other techniques?
推荐答案
对于以下内容,我假设您指的是惰性属性.
For the following I'm assumim you mean a Lazy Property.
这取决于界面的用途.
消费者知道它是懒惰的重要细节吗?或者它只是一个技术细节,不应该改变消费者的行为.
Is it an important detail that the consumer knows that it is lazy ? Or is it just a technical detail which should not change the behavior for the consumer.
如果你只有一个短暂的延迟,不能由消费者处理,那么我会倾向于隐藏懒惰,只直接暴露 T.
It you only have a short delay which must not be handled by the consumer then i would tend to haide the Lazy and only expose T directly.
如果消费者应该意识到并且可以适应这种行为,那么我会暴露lazy .
If the consumer should be aware and may adapt this behavior then i would expose the lazy .
但是考虑到这一点,我在大多数情况下宁愿公开一个方法,该方法表明代码可能有副作用或可能需要一段时间.
But thiking on this I would in most cases rather expose a method which indicates that the code may have side effects or may take a while.
这篇关于我是否应该避免暴露 Lazy<T>公共 API 中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!