Where to declare a class

I have a hand full of methods that interact with an instance of a class. Right now, I initialized it on page loading (disconnecting it from the session). The code is similar to this (simplified for example):

protected void Page_Load(object sender, EventArgs e) { if(!isPostBack) { MyClass myClass = (MyClass)Session["myClass"]; myMethod1(myClass); myMethod1(myClass); } } private void myMethod1(MyClass class) { } private void myMethod2(MyClass class) { } 

This is the right way to do this, or it would be better to declare it "globaly" on the page behind the class, rather than passing it from method to method.

Edit

I assume that my real question is: is there a lack of performance for passing it to methods (because I assume that it is reinitialized every time) against declaring it as an open class inside the code behind the page class. If its declared publication in the page code is behind it, I assume that it is initialized once when the page is requested.

+4
source share
7 answers

This is a question about the scope and lifetime of a variable. The variable "region" is the text of the program into which you can refer to the variable without any qualification, and the "life expectancy" is the duration for which the variable is valid.

In general, it is best to limit the scope of variables as much as possible. This greatly facilitates the explanation of the code.

In the above code example, there are three variables, and each of them is tied to the individual method in which they are displayed. This is a good thing. Assuming the method names are accurate, you can immediately see everything that happens to this value.

In contrast, if you increase the scope of a variable by making it a field on the page, then all methods on this page will have access to the variable. It would be difficult to determine where it is created, how it is mutated and what it is used for. This makes future application support difficult.

Therefore, my recommendation is to continue to do what you have done here when possible.

EDIT

In response to the clarification of the question:

Performance is inconclusive. You are talking about nanoseconds on a web server. Web applications are limited by network or Internet traffic. There will be no noticeable difference in performance with any of these options where the variable goes.

In general, performance is a matter of algorithms and data structures over large sets of objects. In such situations, when there are no large data structures, a person does not see any difference.

Code must be designed first to be correct, understandable, and maintained. Once this is done, performance will only be a problem if it proves to be a problem.

Other editing

If MyClass is a class , not a struct , then the three variables in the code that are of this type represent only the size of one link (32 or 64 bits, depending on the platform). The object to which they all belong is created only once in the above code example.

+3
source

It depends more on the coding style and access restrictions, when you declare it as the level of the page participant, it, of course, is not global, but only a local instance of the page executable.

You might want to consider other options if there are general interactions with the class in the application, for example, creating a session wrapper object that will handle the placement / link to the class in the session and automatic casting so that you can do something along the lines from DoSomethingCool(MySessionWrapper.MyClass) etc., and you donโ€™t have to worry about inconsistent session references for multiple classes.

+1
source

I usually use properties to make this look cleaner:

 protected MyClass Copy { get { return Session["MyClass"] as MyClass; } set { Session["MyClass"] = value; } } 

Now you can simply use it as needed in any of your codeโ€™s methods for

I also have an extension method that allows me to avoid checking for zero all the time.

 public bool NotNull(HttpSessionState s, string key) { return s[key] != null; } 

Now you can simply call this when checking for zero in a session:

 Session.NotNull("MyClass") 
+1
source

There should be no problem passing the instance as a parameter to your methods. Have you found any performance issues in your application so far? If so, they are probably in your database.

Passing objects around as parameters does not copy the entire object to another object. Only the link to the object is transmitted. There is still only one instance of myClass in memory.

+1
source

If a particular instance of MyClass is session specific, then yes, using the session, this is the way to go. Should he be in the session in detail.

0
source

In general, globals are best avoided as they are accessible to everyone, which means high adherence. This is better on this page: http://c2.com/cgi/wiki?GlobalVariablesAreBad

Indeed, I would see what my method does [1,2]. If all they do is access and manage the state of MyClass, then they probably belong to MyClass ...

0
source

You could do it that way.

 public class MyPage { private MyClass myClass; protected void Page_Load(object sender, EventArgs e) { if(!isPostBack) { myClass = (MyClass)Session["myClass"]; myMethod1(); myMethod1(); } } private void myMethod1() { // use myClass here } private void myMethod2() { // use myClass here } } 

The problem that I see is that myClass only assigned when it is PostBack, so it seems like "is it assigned or not assigned now?" If you always assigned myClass , this tactic would make more sense to me.

0
source

Source: https://habr.com/ru/post/1342894/


All Articles