Why are “Extracted Interfaces” Internal rather tha

2019-07-15 12:39发布

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?

2条回答
Lonely孤独者°
2楼-- · 2019-07-15 13:26

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.

查看更多
Melony?
3楼-- · 2019-07-15 13:29

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.

查看更多
登录 后发表回答