When does it make sense to use static methods

  • Thread starter Thread starter Thirsty Traveler
  • Start date Start date
T

Thirsty Traveler

I am still a little confused on when it makes sense to use static methods.
For example, if we have a web page that calls a business logic layer that
calls a data access layer, should the method calls be static if no data is
stored as state in the components?
 
Thirsty Traveler said:
I am still a little confused on when it makes sense to use static methods.
For example, if we have a web page that calls a business logic layer that
calls a data access layer, should the method calls be static if no data is
stored as state in the components?

That's basically it - if it doesn't operate on the state of any
particular object, it makes sense to be static.
 
Him

Thirsty Traveler said:
I am still a little confused on when it makes sense to use static methods.
For example, if we have a web page that calls a business logic layer that
calls a data access layer, should the method calls be static if no data is
stored as state in the components?

Yes, a method should be static IF it does not depend of any of the state
info of the instance and is bounded to the class itself.

Btw, when you refer to components that does not store status, r u talking
about your business or data layer?
 
I think conceptually, the key question to ask is "could my system ever use
more that one of these objects?". If it's a "Customer" class, obviously the
answer would be yes, and you wouldn't want static there.

However if you take a look at something like the ADO.NET v2 "SqlHelper"
class from the Application Blocks, every method in that is static. In fact,
the SqlParameterCache in it has to be static, since it's caching your
SqlParameters for you.

Hope that helps.
Peter
 
Both the bll and dal layers are stateless.

Ignacio Machin ( .NET/ C# MVP ) said:
Him



Yes, a method should be static IF it does not depend of any of the state
info of the instance and is bounded to the class itself.

Btw, when you refer to components that does not store status, r u talking
about your business or data layer?
 
We do store objeects such as this in session state on the web server. The
BLL and DAL layers are stateless and load balanced.

I am wondering about threading. If there are multiple concurrent users
logged into the web app, will they be single threaded through the bll/dal
layers?
 
Back
Top