确定IDisposable应该扩展接口还是在实现该接口的类上实现 [英] Determining if IDisposable should extend an interface or be implemented on a class implementing said interface

查看:63
本文介绍了确定IDisposable应该扩展接口还是在实现该接口的类上实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定我应该使用IDisposable扩展我的接口之一,还是应该在实现我的接口的类上实现IDisposable?



我的接口没有除了一个特定的实现之外,需要处理所有外部资源。我的选择似乎是:



1)在要求所有实现都实现Dispose的接口上实现IDisposable,即使只有一个空方法也是如此。



-或-



2)仅在需要处理资源的类上实现IDisposable。这将导致使用问题,因为我的对象是从工厂创建的,因此所有上游代码都针对该接口工作。由于该接口未绑定到IDisposable,因此使用不会看到Dispose方法。但是,我可以将工厂结果转换为实现;



关于最佳做法的任何想法吗?

解决方案

如果您希望调用者只能与接口进行交互,而不能与实现进行交互,则希望接口扩展 IDisposable 。如果不是,他们将需要检查值是否为IDisposable ,以查看是否需要处理它。



<如果负责处理该对象的对象知道具体的实现,并且只有使用该接口的对象(但不负责处理该对象)被引用,则考虑第二种选择



第一个选项的一个很好的例子是 IEnumerator 。许多 IEnumerator 对象在处置后不需要执行任何操作,但是有些则不需要,因此接口扩展了 IDisposable 因为负责该对象创建/生命周期的对象将(或应该)永远不了解底层实现。



第二个示例是像需要比较的 IComparer 许多对象是可抛弃的,但是通过接口使用对象的代码段不负责创建对象/ lifecycle,因此它不需要知道该类型是否是一次性的。


How can I determine if I should extend one of my interfaces with IDisposable or implement IDisposable on a class that implements my interface?

I have an interface that does not need to dispose of any external resources, except for one particular implementation. My options seem to be:

1) Implement IDisposable on the interface requiring all of the implementations to implement Dispose, even if only an empty method.

-or-

2) Implement IDisposable on only the classes that have resources needing to be disposed. This will cause problems with "using" because my object is created from a factory and therefore all upstream code works against the interface. Since the interface is not bound to IDisposable, "using" does not see the Dispose method. However, I could cast the factory result to the implementation; however, that then makes the consumer aware of the implementation, defeating the purpose of interfaces.

Any ideas as to best practices?

解决方案

If you expect callers to only be able to interact with the interface, and never the implementation, then you want to have the interface extend IDisposable. If not, they'll need to check if the value is IDisposable anyway to see if it needs to be disposed.

If the object responsible for disposing of the object knows of the concrete implementation, and it is only ever objects that are given references to it (but aren't responsible for disposing of it) that use the interface, then consider the second option.

A good example of the first option is IEnumerator. Many IEnumerator objects don't need to do anything when they're disposed, but some do, and so the interface extends IDisposable because the object responsible for the creation/lifecycle of that object will (or should) never have knowledge of the underlying implementation.

An example of the second would be something like IComparer many objects that need to be compared are disposable, but the sections of code using an object through the interface aren't responsible for it's creation/lifecycle, so it needs no knowledge of whether or not that type is disposable.

这篇关于确定IDisposable应该扩展接口还是在实现该接口的类上实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆