为什么要“提取接口”?内部而不是公共?
Visual Studio 包含一个名为“Extract Interface”的重构功能,该功能可根据类实现生成接口。
提取的接口默认为Internal。问题是,我们最终将几乎所有这些都更改为Public
。
有谁知道为什么它默认是内部的?更好的是,有没有办法将其自定义为默认为 Public
?
Visual Studio includes a refactoring function called "Extract Interface" that generates an interface based on a class implementation.
The extracted interfaces are Internal by default. Problem is, we end up changing nearly all of them to Public
.
Does anyone know why it's Internal by default? Better yet, is there a way to customize this to default to Public
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您更改界面的 Visual Studio 模板,它可能会起作用(我没有尝试过,但假设这应该起作用)。
对于 Visual Studio 2008,模板存储在
这个答案。
It might work if you change the Visual Studio template for interfaces (I haven't tried that but assume this should work).
For Visual Studio 2008 the template is stored at
This is described in more detail in this answer.
我没有参考资料,但我对为什么它默认是内部的有一个大胆的猜测。
假设您有 3 个项目/程序集:log4net(第 3 方 API)、MyApp.Util 和 MyApp.Web(Web 项目)。 Web 引用了 Util,后者引用了 log4net。 Web不引用 log4net,并且您希望保持这种方式。
在 DAL 内部,假设您有一个内部类,并且其成员之一引用了 log4net 中定义的类型。它可以是方法的返回类型或参数类型之一,或者属性的类型。
假设您从上述类中提取了一个接口,包括上述引用 log4net 的成员。那么,如果您将该成员设为公共(公共接口的一部分)并引用实现它的类型,那么您就需要 Web 项目引用 log4net。
通过将接口设为内部,Web 可能会继续不了解 log4net。
I don't have a reference, but I have a wild guess as to why it is internal by default.
Let's say you have 3 projects / assemblies: log4net (a 3rd party API), MyApp.Util, and MyApp.Web (a Web project). Web references Util, which references log4net. Web does not reference log4net, and you want to keep it that way.
Inside of DAL, say you have an internal class, and one of its members references a type defined in log4net. It could be the return type or one of the parameter types of a method, or the type of a property.
Let's say you extract an interface from the aforementioned class, including the aforementioned member that references log4net. Well, if you make that member public (part of a public interface) and reference a type that implements it, you then require that the Web project reference log4net.
By making the interface internal, Web may continue to be ignorant of log4net.