Why are “Extracted Interfaces” Internal rather than Public?

孤人 提交于 2020-02-04 05:32:05

问题


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?


回答1:


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

"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates
    \CSharp\Code\1033\Interface.zip"

This is described in more detail in this answer.




回答2:


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.



来源:https://stackoverflow.com/questions/3024199/why-are-extracted-interfaces-internal-rather-than-public

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!