为什么要使用“提取的接口”内部的而不是公共的? [英] Why are "Extracted Interfaces" Internal rather than Public?

查看:68
本文介绍了为什么要使用“提取的接口”内部的而不是公共的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio包含一个称为提取接口的重构函数,该函数根据类实现生成一个接口。



提取的接口默认为内部。问题是,我们最终几乎将它们全部更改为 Public



有人知道默认情况下为什么是内部吗?更好的是,有没有一种方法可以将其自定义为默认为 Public

解决方案

我没有引用,但是对于默认情况下为什么是内部引用,我有一个疯狂的猜测。假设您有3个项目/程序集:log4net(第三方API),MyApp.Util和MyApp.Web(一个Web项目)。 Web引用了Util,后者引用了log4net。 Web不会引用log4net,并且您希望保持这种方式。



在DAL内,您说有一个内部类,还有一个其成员引用了在log4net中定义的类型。可以是返回类型,也可以是方法的参数类型之一,也可以是属性的类型。



假设您从上述类中提取了一个接口,包括引用log4net的上述成员。好吧,如果您将该成员设为公共(公共接口的一部分)并引用实现该成员的类型,则您要求该Web项目引用log4net。



通过将界面设置为内部,Web可能仍然对log4net一无所知。


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?

解决方案

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.

这篇关于为什么要使用“提取的接口”内部的而不是公共的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆