接口和版本控制

发布于 2024-07-06 00:15:48 字数 345 浏览 6 评论 0原文

我正在设计一个新系统,我有很多接口,它们会随着系统的时间而增长。 命名此接口的最佳实践是什么

ISomethingV01
ISomethingV02
etc

public interface ISomething{
      void method();
}

然后我必须添加方法 2,那么现在我该怎么做?

public interface ISomethingV2:ISomething{
      void method2();
}

或者同样的其他方式?

I am designing a new System and I have a lot of Interfaces that will grow over time with the system. What is the best practice to name this interfaces

ISomethingV01
ISomethingV02
etc

and I do this

public interface ISomething{
      void method();
}

then I have to add method 2 so now what I do?

public interface ISomethingV2:ISomething{
      void method2();
}

or same other way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

第几種人 2024-07-13 00:15:48

接口的目的是定义类型必须实现的抽象模式。

最好的实现方式是:

public interface ISomething

public class Something1 : ISomething
public class Something2 : ISomething

通过创建同一接口的多个版本,您不会以代码可重用性或可扩展设计的形式获得任何东西。

The purpose of an interface is to define an abstract pattern that at type must implement.

It would be better implement as:

public interface ISomething

public class Something1 : ISomething
public class Something2 : ISomething

You do not gain anything in the form of code reusability or scalable design by creating multiple versions of the same interface.

<逆流佳人身旁 2024-07-13 00:15:48

理想情况下,您不应该经常更改界面(如果有的话)。 如果您确实需要更改接口,您应该重新考虑其用途并查看原始名称是否仍然适用。

如果您仍然觉得界面会发生变化,并且界面变化很小(添加项目)并且您可以控制整个代码库,那么您应该只修改界面并修复所有编译错误。

如果您的更改是接口使用方式的更改,那么您需要创建一个单独的接口(很可能使用不同的名称)来支持该替代使用模式。

即使您最终创建了 ISomething、ISomething2 和 ISomething3,界面的使用者也将很难弄清楚这些界面之间的差异。 他们什么时候应该使用 ISomething2,什么时候应该使用 ISomething3? 然后你必须着手淘汰 ISomething 和 ISomething2 的过程。

Ideally, you shouldn't be changing your interfaces very often (if at all). If you do need to change an interface, you should reconsider its purpose and see if the original name still applies to it.

If you still feel that the interfaces will change, and the interfaces changes are small (adding items) and you have control of the whole code base, then you should just modify the interface and fix all the compilation errors.

If your change is a change in how the interface is to be used, then you need to create a separate interface (most likely with a different name) to support that alternative usage pattern.

Even if you end up creating ISomething, ISomething2 and ISomething3, the consumers of your interfaces will have a hard time figuring out what the differences are between the interfaces. When should they use ISomething2 and when should they use ISomething3? Then you have to go about the process of obsoleting ISomething and ISomething2.

姜生凉生 2024-07-13 00:15:48

我认为你过度滥用接口。

迈耶和马丁告诉我们:“对扩展开放,但对修改关闭!”

然后 Cwalina(等人)重申:

从框架设计指南......

一般来说,课程是首选
用于公开抽象的构造。
接口的主要缺点是
他们的灵活性远低于
课程涉及到允许
API 的演变。 一旦您发货
接口,其成员的集合是
永远固定。 任何补充
接口会破坏现有类型
实现接口。

类提供了更大的灵活性。
您可以将成员添加到以下类:
已经发货了。 只要
方法不是抽象的(即,只要
当您提供默认值时
该方法的实现),任何
现有的派生类继续
功能不变。

替代文本

I think you're overrusing interfaces.

Meyer and Martin told us: "Open for extension but closed for modification!"

and then Cwalina (et al) reiterated:

From Framework Design Guidelines...

In general, classes are the preferred
construct for exposing abstractions.
The main drawback of interfaces is
that they are much less flexible than
classes when it comes to allowing for
evolution of APIs. Once you ship an
interface, the set of its members is
fixed forever. Any additions to the
interface would break existing types
implementing the interface.

A class offers much more flexibility.
You can add members to classes that
have already shipped. As long as the
method is not abstract (i.e., as long
as you provide a default
implementation of the method), any
existing derived classes continue to
function unchanged.

alt text

一抹淡然 2024-07-13 00:15:48

我同意Garo Yeriazarian,改变界面是一个严肃的决定。 另外,如果您想推广新版本界面的使用,您应该将旧版本标记为已过时。 在 .NET 中,您可以添加 ObsoleteAttribute

I agree with Garo Yeriazarian, changing interface is a serious decision. Also, if you want to promote usage of new version of interface you should mark old version as obsolete. In .NET you can add ObsoleteAttribute.

倾听心声的旋律 2024-07-13 00:15:48

我不知道为什么人们对你的帖子投反对票。 我认为良好的命名指南非常非常重要。

如果您需要保持与上一个版本的兼容性。 同一接口的版本考虑使用继承。
如果您需要引入新版本的界面,请考虑以下规则:

尝试添加对您有意义的后缀
界面。 如果不可能的话
创建简洁的名称,考虑添加
版本号。

I don't know why people downvote your post. I think that good naming guidelines are very important.

If you need to maintain compatibility with prev. version of the same interface consider using inheritance.
If you need to introduce new version of interface consider following rule:

Try to add meaningful suffix to you
interface. If it's not possible to
create concise name, consider adding
version number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文