简体   繁体   English

Web应用程序中的方法应该是公共的还是内部的?

[英]Should methods in a web app be public or internal?

Within the scope of a web application, what are the implications of declaring a method's accessibility to be public vs. internal ? 在Web应用程序的范围内,声明方法的可访问性是public internal的含义是什么?

Web applications typically contain methods used only within the application itself. Web应用程序通常包含仅在应用程序本身内使用的方法。 For example, an ecommerce web site may contain a method to retrieve a user's order history. 例如,电子商务网站可能包含检索用户订单历史记录的方法。 Along the lines of: 沿着:

public List<Orders> RetrieveOrders() {
    //code goes here
}

If this method was contained within a class library, the significance of public and internal access modifiers is fairly straightforward; 如果此方法包含在类库中,则publicinternal访问修饰符的重要性相当简单; public methods would be accessible outside of the library itself, internal methods would not. 公共方法可以在图书馆本身之外访问,内部方法则不会。

Is there any practical difference when it comes to web applications? 在Web应用程序方面有什么实际区别吗?

EDIT: My question has been suggested to be a duplicate of What is the difference between Public, Private, Protected, and Nothing? 编辑:我的问题被建议是重复的公共,私人,受保护和没有什么区别? . I am not asking for an explanation of access modifiers. 我不是要求解释访问修饰符。 I am asking whether there is any significance for access modifiers on methods within a web site specifically. 我在询问网站中的方法访问修饰符是否有任何重要意义。

In general public should be limited to externally visible methods - this includes public API and methods that must be exposed dues to technical restrictions (ie classes for ASP.Net pages). 一般来说, public应该限于外部可见的方法 - 这包括公共API和必须暴露的技术限制的方法(即ASP.Net页面的类)。

In case of web application there is generally no "public API" as such libraries generally are not expected to be consumed by external users. 在Web应用程序的情况下,通常没有“公共API”,因为这些库通常不会被外部用户使用。

So mostly there is no practical differences between internal and public for web application development. 因此,对于Web应用程序开发而言, internalpublic之间通常没有实际差异。 You gain some minor convenience for using only public as you no longer need to have InternalsVisibleTo attributes for unit tests. 由于您不再需要使用InternalsVisibleTo属性进行单元测试,因此您只需使用public获得一些小的便利。

More discussions: public vs. internal methods on an internal class , internal vs public in c# 更多讨论: 内部类的公共与内部方法 ,c#中的内部与公共 方法

Side note: ASP.Net MVC web site is a class library - so all discussions about access modifiers related to class libraries applies to ASP.Net MVC/WebAPI sites. 旁注:ASP.Net MVC网站是一个类库 - 因此所有关于类库的访问修饰符的讨论都适用于ASP.Net MVC / WebAPI站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM