When does it make sense to use static methods

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?
 
J

Jon Skeet [C# MVP]

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.
 
I

Ignacio Machin \( .NET/ C# MVP \)

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?
 
G

Guest

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
 
T

Thirsty Traveler

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?
 
T

Thirsty Traveler

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top