views:

490

answers:

6

I am working on maintaining an ASP.NET website, and I've noticed the business layer and other supporting libraries make heavy use of HttpContext.Current.Session. This makes it hard to keep track of session variables, to determine what they're used for and why they even exist.

Is it considered bad practice to use the session in the business layer? And would it be wise to start moving all code that uses the session into the code-behind?

+1  A: 

Yes - the BL should not have any knowledge about the Session. Its a dependency that you don't need.

Brian
+1  A: 

make a class that is an indirection, in which case on the web it may return values from HttpContext.Current.Session, and in other areas would resolve that from somewhere else. IE have an interface ISessionStore and have concrete classes WebSessionStore and WindowsFormsSessionStore, etc.

this will make your code easier to test and also gives you expansion paths when say, you now want x business logic to run in a windows service where it can run x piece of code every y minutes.

Darren Kopp
+3  A: 

I follow this rule - any class in System.Web namespace (javax.servlet package in Java) should not be present in your business layer.

Chetan Sastry
+5  A: 

It's almost never a good idea. There's lots of reasons, but here's a couple:

  • you'll never be able to use business layer code in anything other than ASP.NET
  • Unit Testing becomes much more of a pain or even impossible.

We ran into huge headaches with this exact same situation when we started to build services that utilized common business layer code.

Ryan Brunner
+1  A: 

In my opinion it is bad practice.

It makes it pretty hard to dissociate that business layer from the environment. If you expect to unit test the thing for example, you're out of luck.

One way to take care of that simply would be to insulate this into an abstraction for now, so that you can pass a "state cache" around and not refer to HttpContext. That will take you at least to some degree of abstraction. Another more interesting question is, why does the business layer need to refer to that?

Denis Troller
That's a good point about unit testing. As to why the business layer needs HttpContext: information about the logged-in user, like their role and permissions, are kept in the session, which can affect what the business logic does. I'll try abstracting away the state and passing it around - that sounds like a good solution.
tspauld
The problem with that is that "Current user" is a pervasive concept, and you will be carrying it through layers of objects that don't need it. Sounds like a good reason to use an IOC, but that's a completely different ball game, and not a simple modification...
Denis Troller
+1  A: 

its always better to have a centralized Cache/Session manager which encapsulates the complete interaction with session/cache or whatever persistence method you use. having your BL to interact with sessions is definitely a very bad practice and in a way defeats the purpose of the tiered architecture altogether.

Vikram