0

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.

Csharp
  • 2,916
  • 16
  • 50
  • 77
Tim C
  • 47
  • 1
  • 7

2 Answers2

3

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.

Yuriy Galanter
  • 38,833
  • 15
  • 69
  • 136
2

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • I'm looking to implement an Object Pool Pattern, and was really hoping to rely on the application state for some of the mechanization of it...I want to hold and reuse database connections between pages. Currently, my business logic objects initialize database connections, and as the client goes from page to page, those connections are reinitialized, which is creating some problems. So, ideally I'd like my BLL to be able to use some sort of pool to keep those connections from getting removed by the GC. Is there another pattern I could use to accomplish this? – Tim C Jul 19 '13 at 19:06
  • It's not the pattern that's the issue - it's the persistence store. Why not just use `static` data (with proper locking) to store the objects. Also, most database clients have connection pooling, so you may not even need to do your own. – John Saunders Jul 19 '13 at 20:09