接口和版本控制
我正在设计一个新系统,我有很多接口,它们会随着系统的时间而增长。 命名此接口的最佳实践是什么
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
接口的目的是定义类型必须实现的抽象模式。
最好的实现方式是:
通过创建同一接口的多个版本,您不会以代码可重用性或可扩展设计的形式获得任何东西。
The purpose of an interface is to define an abstract pattern that at type must implement.
It would be better implement as:
You do not gain anything in the form of code reusability or scalable design by creating multiple versions of the same interface.
理想情况下,您不应该经常更改界面(如果有的话)。 如果您确实需要更改接口,您应该重新考虑其用途并查看原始名称是否仍然适用。
如果您仍然觉得界面会发生变化,并且界面变化很小(添加项目)并且您可以控制整个代码库,那么您应该只修改界面并修复所有编译错误。
如果您的更改是接口使用方式的更改,那么您需要创建一个单独的接口(很可能使用不同的名称)来支持该替代使用模式。
即使您最终创建了 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.
我认为你过度滥用接口。
迈耶和马丁告诉我们:“对扩展开放,但对修改关闭!”
然后 Cwalina(等人)重申:
从框架设计指南......
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...
我同意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.
我不知道为什么人们对你的帖子投反对票。 我认为良好的命名指南非常非常重要。
如果您需要保持与上一个版本的兼容性。 同一接口的版本考虑使用继承。
如果您需要引入新版本的界面,请考虑以下规则:
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: