HttpApplicationState outside of the Web Project (Class Library)

988 views Asked by At

I've got an ASP.net web application that relies on a separate DLL class library project for its business logic. However, I'd like to use the global HttpApplicationState in the DLL project. But that seems to be a web project-specific implementation.

Certainly, I can have constructors (or methods) of particular classes take the HttpApplicationState as a parameter, but I'm wondering if there's a more eloquent way of doing this. I realize I'm creating some design issues, but I'd really like to be able to utilize global stuff in my business logic.

2

There are 2 answers

2
John Saunders On BEST ANSWER

You should try to separate the concerns between the web application and the class libraries. The class libraries should have as little knowledge as possible about the fact that a web application is calling them. In particular, they should not be using Session state or Application state. In fact, they should preferably have no reference to System.Web.dll!

Have the web application pass to the class library the pieces of Application state that the class library needs. The class library should have no knowledge of the fact that the data came from Application state.

This will also make it much easier to unit test the class library, as it will be possible to call it from a unit test framework.

0
Yuriy Galanter On

Using HttpContext.Current.Application you should be able to access HttpApplicationState in the outside classes.

Update

But I agree with John Saunders - business logic should not be aware of where it is called from.