Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 applications typically contain methods used only within the application itself. 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; 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?

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.

like image 331
Shai Cohen Avatar asked Jan 04 '17 02:01

Shai Cohen


People also ask

Should I use internal or public?

internal is useful when you want to declare a member or type inside a DLL, not outside that. Normally, when you declare a member as public , you can access that from other DLLs. But, if you need to declare something to be public just inside your class library, you can declare it as internal .

What is the difference between a public and internal class in C#?

Access modifiers in C# are used to specify the scope of accessibility of a member of a class or type of the class itself. For example, a public class is accessible to everyone without any restrictions, while an internal class may be accessible to the assembly only.

What is difference between internal and private in C#?

protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly. private protected: The type or member can be accessed by types derived from the class that are declared within its containing assembly.

What does internal mean C#?

Internal, in C#, is a keyword used to declare the accessibility of a type or type member such that the access is limited to the assembly in which it is declared. An internal modifier is used to prevent the use of a public modifier, which allows access to other assemblies wherever necessary.


1 Answers

In general public should be limited to externally visible methods - this includes public API and methods that must be exposed dues to technical restrictions (i.e. classes for ASP.Net pages).

In case of web application there is generally no "public API" as such libraries generally are not expected to be consumed by external users.

So mostly there is no practical differences between internal and public for web application development. You gain some minor convenience for using only public as you no longer need to have InternalsVisibleTo attributes for unit tests.

More discussions: public vs. internal methods on an internal class, internal vs public in 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.

like image 69
Alexei Levenkov Avatar answered Sep 20 '22 20:09

Alexei Levenkov