Last post Feb 05, 2015 12:14 PM by VJKishan
Feb 05, 2015 12:30 AM|VJKishan|LINK
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.
Feb 05, 2015 03:53 AM|AidyF|LINK
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.
Feb 05, 2015 04:34 AM|VJKishan|LINK
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.
Feb 05, 2015 09:45 AM|AidyF|LINK
The advantage of static methods is easier code
MyBusinessClass b = new MyBusinessClass();
var result = b.MyNonStaticMethod();
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.
Feb 05, 2015 12:14 PM|VJKishan|LINK