Question

I am working on a website with 2 sub projects. The sub projects are referenced like this:
=> my website has a reference to project Business Layer
=> project Business Layer has a reference to project Data Layer

Visitors are required to log in. There information is saved in DB. In my DB I have different configuration information that is needed to make the website calculate the right information.

Example:
User A logs in into website and configuration information is stored in the object name : UserInfo. The UserInfo class is also a part of my Business Layer.

After that I always have to create my object from cookie or Session and call my Business Layer object like this:
BL.CalculatePrice(UserInfo.RegionID)
BL.CalculateTotal(UserInfo.RegionID)

Is there a way to keep the state of the "user" object, between webpage changes, in my Business Layer project without the need to send the reference (UserInfo.RegionID) with each call from the website?

Was it helpful?

Solution

You could use various things

  1. Store the info in the session
  2. Make a static collection, and dispose of the info manually after a given time
  3. Same as 2. but use something like MemoryCache with an expiration so you don't have to handle expiration manually
  4. Same as 3 but using the http cache

Here are some namespaces/ideas for you to check out

  1. System.Web.HttpContext.Current.Session
  2. private static Dictionary<userId, UserInfo> sessionInfo
  3. System.Runtime.Caching.MemoryCache.Default
  4. System.Web.HttpContext.Current.Cache

Personally i'd opt for the MemoryCache (it's newer and sort of replaces the httpCache), i use this in a project to implement a semi-generic class that uses the MemoryCache as a cache layer between the BLL & DAL.

Or the static Dictionary: create UserInfo, put it in the Dictionary, get it again, Update a value, and later remove it (same scenario with the built in cache versions).

The advantage of MemoryCache/Dictionary is that they don't need a reference to HttpContext and can thus be used outside of a web context. But that doesn't mean you can't use HttpContext instead (it's easier). You just have to weight the pros/cons of using that in a dal/bll or not.

For MemoryCache be sure to check out: http://msdn.microsoft.com/en-us/library/dd941875.aspx it's important to set these to keep your server from using too much memory

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top