如何解释不同的执行需要? [英] how to account for disparate implementation needs?

查看:77
本文介绍了如何解释不同的执行需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有两个具体类的接口。一个具体的需要实施的IDisposable 。如果接口进行修订,以实施的IDisposable 一类的利益还是应该接口的用户必须执行运行时检查可处理?

Suppose I have an interface with two concrete classes. One concrete needs to implement IDisposable. Should the interface be amended to implement IDisposable for the benefit of one class or should the consumer of the interface have to perform runtime checks for disposability?

我想接口应该修改,因为它是一个简单的变化(特别是如果它的一个新的接口),但我还可以看到可能违反Liskov的在改变设计以适应特定的实现(特别是如果其他类或类都抛出不支持的异常)

I assume the interface should be amended as it is a simple change (especially if its a new interface) but I also can see a possible violation of liskov in changing a design to suit a particular implementation (especially if the other class or classes have to throw not supported exceptions)

推荐答案

我找到了答案,而在依赖注入阅读马克·塞曼的书。 IDisposable的接口上自动是漏的抽象,因为IDisposable的只是一个实现细节。也就是说,不是所有的接口是抽象的,因此 - 编程严格接口的名字 - 会出现在那里的接口必须实现IDisposable的情况。

I found the answer while reading Mark Seemann's book on Dependency Injection. IDisposable on an interface is automatically a leaky abstraction since IDisposable is an implementation detail ONLY. That said, not all interfaces are abstractions and thus - in the name of programming strictly to interfaces - there will be situations where interfaces must implement IDisposable.

虽然它更喜欢一个具体实现IDisposable比一个接口,在这两种情况下的解决方案是创建在资源上的粗粒抽象。那么每个抽象方法的实现将创建和配置资源,减轻的做同样的负担消费者。我喜欢这种方法,因为它减少了对消费者的一部分,生命周期管理的复杂性(这确实应该是没话说,特别是在DI)

Though it is much preferred that a concrete implement IDisposable than an interface, in both cases the solution is to create a coarse-grained abstraction over the resource. Each method implementation of the abstraction would then create and dispose the resource, relieving the consumer of the burden of doing the same. I like this approach because it reduces the complexity of lifetime management on the part of a consumer (which really should be none, especially in DI.)

为了实现DI在上述情况下,你可能需要注入一个工厂,让每个方法实例依赖特设的。

In order to implement DI in the above scenario, you will probably need to inject a factory, allowing each method to instantiate the dependency ad-hoc.

这篇关于如何解释不同的执行需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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