What are the desirable situation (real life exampl

2019-06-17 02:17发布

I just want to understand the purpose that static method serves and what are the desirable situation where i can create static methods except some would say that static methods are used for creating helper.

Consider i have 1 website that will be used in my company only like Human resource management system like websites.

Now after Admin login in to the system admin will see the list of employees.so the method is simple which does nothing more than fetching all details of employees from employee table and will display them on the web site and this method will be define in business access layer like this in .net:

 public class EmployeeBal
    {
        public List<Employee> GetAllEmployees()
        {
                 return Select * from Employee
        }
     }

This is how i would call this method from my application.For Eg(.aspx page or mvc controller etc....)

var employeeBal= new EmployeeBal();
employeeBal.GetAllEmployees();

So my question is should i create this method as static method or non static method??

Note:This is just an example of method and this method is in my business access layer.

Consider i have 1 ecommerce website where on the home page i am displaying some list of products and on visit of that website every users can see that list of products.

so my function would be same as above define in Business acess layer:

public class ProductBal
    {
        public List<Product> DisplayProductonHomePage()
        {
                 return Select * from Products
        }
     }

So my question would be same like whether to create this method as static method or non-static method and what will happen if more than 10 users at same time simultaneously access this website then what will be the behaviour/implications of this method???

Will this method will serve the purpose of this each user if we declare this method as static??

Can anybody answer this question with briefly explaining every scenario???

7条回答
Evening l夕情丶
2楼-- · 2019-06-17 03:00

It's fine to have multiple threads executing the same static method, as long as the method does not access static state such as field or properties. In that case, the shared objects stored in the fields/properties must themselves be thread safe. The data access parts of .Net are not designed to be thread safe.

As soon as you start considering aspects such as managing a database connection that can be reused for several queries during the execution of a single web request, you should consider if static is the best approach. Since you cannot store the connection in a static field as explained above, you will have to pass it as a parameter to each static method. On the other hand, if you pass the connection to a constructor and store it in a (non-static) field, you can access it from multiple non-static methods of that instance, which will IMO be easier to manage.

This is quite a big topic however, and in general the management of class dependencies is quite tricky to get right in OOP. Some programmers prefer to delegate this task to an "Inversion of Control"-library. There are many available for .Net such as Microsoft Unity, StructureMap, AutoFac, etc.

查看更多
登录 后发表回答