locked
Declare as static all Methods not using Instance Fields RRS feed

  • Question

  • User1578452026 posted

    Hi All,

    We have a n tier (UI, Biz layer , Data Layer ) architecture ASP.Net application.In the data layer,Biz Layer methods are declared as static . When we ran a tool to check the standards we get Declare as static all Methods not using Instance Fields  

    Reason : Our UI Layer methods ( Calling methods are not declared as static ). If I try to make this static , it says the ASP.Net controls to be declared as static ( not accessible).

    We would like to know 

    1. Should we be using static methods in first place in Biz, Data Layer

    2. If yes How to resolve the above mentioned issue.

    Regards

    VJ

    Thursday, February 5, 2015 12:30 AM

Answers

  • User-760709272 posted

    The advantage of static methods is easier code

    // non-static
    
    MyBusinessClass b = new MyBusinessClass();
    var result = b.MyNonStaticMethod();
    
    // static
    
    var result = MyBusinessClass.MyStaticMethod();

    and a closely related advantage is less resources as you're not creating and destroying your business class each time you want to access its methods.

    For code behind your methods have to be non-static and if any tool is telling you otherwise then stop using the tool.  Or at least configure it so that it ignores your code-behind scenarios.

    If you make your business methods static then you can still use those static methods from your non-static code-behind methods, so maybe the tool is saying you should be calling static methods from your code behind rather than saying your code behind methods themselves should be static.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 5, 2015 9:45 AM

All replies

  • User-760709272 posted

    1 "It depends".  Are you using dependency injection or unit testing?  If you are using neither then you can declare your business methods as static.  If you're using DI or unit testing then you probably can't declare them as static.

    2 I don't really understand your issue RE the UI layer, but you can't declare your controls to be static if that is what you are asking.  Or if your problem is that you have static methods in your code-behind and you can't access your web controls inside them, then that is by design, you can't access non-static variables from inside a static method, so any code that accesses controls can't be marked as static.

    http://forums.asp.net/t/1934540.aspx?Static+vs+Non+Static+methods

    Thursday, February 5, 2015 3:53 AM
  • User1578452026 posted

    Thanks a lot.

    One query :

    I don't have dependency injection or unit testing code.  But still can I opt for Instance fields. I mean what's adv I get because of static methods.

    Regarding your #2

    My Code Behind is currently non static. This non static method call static method in Biz Layer. Now we have to run a tool for checking the coding stds. This tool says my code behind method is suppose to be static.

    Now If I change my code behind to static , I cannot access the controls.

    Hope you get me.

    Thursday, February 5, 2015 4:34 AM
  • User-760709272 posted

    The advantage of static methods is easier code

    // non-static
    
    MyBusinessClass b = new MyBusinessClass();
    var result = b.MyNonStaticMethod();
    
    // static
    
    var result = MyBusinessClass.MyStaticMethod();

    and a closely related advantage is less resources as you're not creating and destroying your business class each time you want to access its methods.

    For code behind your methods have to be non-static and if any tool is telling you otherwise then stop using the tool.  Or at least configure it so that it ignores your code-behind scenarios.

    If you make your business methods static then you can still use those static methods from your non-static code-behind methods, so maybe the tool is saying you should be calling static methods from your code behind rather than saying your code behind methods themselves should be static.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 5, 2015 9:45 AM
  • User1578452026 posted

    Thanks

    Thursday, February 5, 2015 12:14 PM