views:

57

answers:

2

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?

A: 

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.

0xA3
This works for changing new Interfaces, Classes, and such but not for changing the output from the Refactor tool's "Extract Interface" function. :( Thanks though.
Rox Wen
@Rox Wen: Ok, I see. Another thing that came to my mind: VS also has a built-in code snippet for creating new interfaces. Maybe this one is used and you can modify that? The location for that one is somewhere under "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#\interface.snippet".
0xA3
A: 

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.

apollodude217
Have you tried this? Is that why you marked the answer CW?
John Saunders
@John - I have _basically_ tried this. I'm looking at an example now where Web references DAL references NHibernate, but Web does not reference (and cannot use) NHibernate. If Web references, say, a type that implements NHibernate.ISession, then it will no longer build because it requires the NHibernate reference. I.e., you may break a build somewhere if you make an existing type implement a public interface because all inheritance in C# (in .NET?) is public.
apollodude217
@John - I marked it CW for more authoritative answers as to the motive, and so others can edit for correctness. Was that a bad idea? :( Note to self: RTM.
apollodude217