Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Best way to store lookup variables?

Reply
Thread Tools

Best way to store lookup variables?

 
 
john_c
Guest
Posts: n/a
 
      02-20-2007
I'd like to store information for roles in Session variables. RoleA
has a specific set of values, RoleB has a specific set and so one.
When I access values for RoleA, it looks like this:

Session[Utils.RoleA_MBcount] = "30"
Session[Utils.RoleA_BandwidthLimit] = "1000"
Session[Utils.RoleA_FeatureBcountLimit] = "5"
Session[Utils.RoleA_FeatureCcountLimit] = "10"

and similar for the other roles. Those values are initially loaded
from a database. Each role is actually a purchased plan. Similar to
various hosting plans that give you more the more you pay. Once the
roles are initialized into Session variables, I just access the
Session variable to check user limits.

Each of the above properties in Utils is a static string reference.
They're just nice ways to give me intellisense and avoid hard coding
strings everywhere. The problem is that I'll have to hit the Utils.cs
file everytime I want to use these lookup values. If there are four
roles and each one has 10 properties that could be a lot of hits
(especially with more roles) depending on how grouped the lookups are
through out the app.

Is this a concern or is there a better way?

Thanks,
John

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      02-20-2007
"john_c" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> The problem is that I'll have to hit the Utils.cs file everytime I want to
> use these lookup values.


In fact, you don't... The Utils.cs file, like all your other C# code files,
is compiled up into your web app's DLL when you rebuild the app, which is
loaded into memory when the app starts...

> Is this a concern or is there a better way?


I wouldn't have thought it was a concern...

As for a better way, that's a little difficult to say... There are certainly
other ways - you could use one of the Collection objects (e.g. ArrayList) or
Generics objects (e.g. Dictionary) if you're using ASP.NET v2, or you could
hold each element separately in Session - if there are any performance
differences, we're probably looking at nanoseconds...


 
Reply With Quote
 
 
 
 
john_c
Guest
Posts: n/a
 
      02-20-2007
On Feb 20, 3:34 pm, "Mark Rae" <(E-Mail Removed)> wrote:
> "john_c" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
> > The problem is that I'll have to hit the Utils.cs file everytime I want to
> > use these lookup values.

>
> In fact, you don't... The Utils.cs file, like all your other C# code files,
> is compiled up into your web app's DLL when you rebuild the app, which is
> loaded into memory when the app starts...


You're right Mark. Thanks. I guess the "static" accessor threw me
off. Do these static properties stay in memory for the length of the
user session or app session?

>
> > Is this a concern or is there a better way?

>
> I wouldn't have thought it was a concern...
>
> As for a better way, that's a little difficult to say... There are certainly
> other ways - you could use one of the Collection objects (e.g. ArrayList) or
> Generics objects (e.g. Dictionary) if you're using ASP.NET v2, or you could
> hold each element separately in Session - if there are any performance
> differences, we're probably looking at nanoseconds...


I am using ASP.NET 2.0. A collection could be used but I think a
better way is to group each role by static class in the Utils class,
which is also static. This just gives a nice intellisense layout. So
you would have

Session[Utils.Roles.RoleA.MBcount]
Session[Utils.Roles.RoleA.BandwidthLimit]
Session[Utils.Roles.RoleA.FeatureBcountLimit]
Session[Utils.Roles.RoleA.FeatureCcountLimit]

 
Reply With Quote
 
john_c
Guest
Posts: n/a
 
      02-20-2007
Also, there will be corresponding Application cache for each plan.
The user Session values are compared against it. This means the
database only needs to be hit when the app loads and for each new user
(session).

 
Reply With Quote
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      02-21-2007
John,
Do you really need it as a session variable? Will it be the same
values for each session? If so then you're going to end up with a lot of
useless memory usage. You could put the values into the cache object. You
could do a check every so often, such as when a request starts, and check to
see if the cache is empty. If so then load the object/array/etc. through
some function and put it into the cache. This would give you a good bit of
control as well since you could determine how much time should occur to
cause the items to fall out of the cache.

--

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

"john_c" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'd like to store information for roles in Session variables. RoleA
> has a specific set of values, RoleB has a specific set and so one.
> When I access values for RoleA, it looks like this:
>
> Session[Utils.RoleA_MBcount] = "30"
> Session[Utils.RoleA_BandwidthLimit] = "1000"
> Session[Utils.RoleA_FeatureBcountLimit] = "5"
> Session[Utils.RoleA_FeatureCcountLimit] = "10"
>
> and similar for the other roles. Those values are initially loaded
> from a database. Each role is actually a purchased plan. Similar to
> various hosting plans that give you more the more you pay. Once the
> roles are initialized into Session variables, I just access the
> Session variable to check user limits.
>
> Each of the above properties in Utils is a static string reference.
> They're just nice ways to give me intellisense and avoid hard coding
> strings everywhere. The problem is that I'll have to hit the Utils.cs
> file everytime I want to use these lookup values. If there are four
> roles and each one has 10 properties that could be a lot of hits
> (especially with more roles) depending on how grouped the lookups are
> through out the app.
>
> Is this a concern or is there a better way?
>
> Thanks,
> John
>



 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-21-2007
Hi there John and Mark,

You could go even further and create a base page class with properties you
want. this approach gives you possiblity to get already casted and defaulted
values with great support from intelli sense:

-- begin rolebasepage.cs code --

public class RoleBasePage : System.Web.UI.Page
{
public RoleBasePage() : base()
{
}

private RoleAProperties roleA;
public RoleAProperties RoleA
{
get
{
if (this.roleA == null)
this.roleA = new RoleAProperties(Session);
return this.roleA;
}
}

public sealed class RoleAProperties
{
private System.Web.SessionState.HttpSessionState session;

public RoleAProperties(System.Web.SessionState.HttpSessio nState session)
{
if (session == null)
throw new NullReferenceException("session");
this.session = session;
}

public int MBCount
{
get
{
object value = this.session["RoleAMBCount"];
return value == null ? 0 /* default value */ : (int) value;
}
set
{
this.session["RoleAMBCount"] = value;
}
}
}
}
-- end rolebasepage.cs --

-- begin any aspx page code behind --
public partial class MyAnyPage : RoleBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (RoleA.MBCount < 10)
{
// do something -
// not enough MB allowance to approve the invoice
// or whatsoever
}
}
}

-- end any aspx page code behind --

Milosz


"Mark Rae" wrote:

> "john_c" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
> > The problem is that I'll have to hit the Utils.cs file everytime I want to
> > use these lookup values.

>
> In fact, you don't... The Utils.cs file, like all your other C# code files,
> is compiled up into your web app's DLL when you rebuild the app, which is
> loaded into memory when the app starts...
>
> > Is this a concern or is there a better way?

>
> I wouldn't have thought it was a concern...
>
> As for a better way, that's a little difficult to say... There are certainly
> other ways - you could use one of the Collection objects (e.g. ArrayList) or
> Generics objects (e.g. Dictionary) if you're using ASP.NET v2, or you could
> hold each element separately in Session - if there are any performance
> differences, we're probably looking at nanoseconds...
>
>
>

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      02-21-2007
"john_c" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...

> You're right Mark. Thanks. I guess the "static" accessor threw me
> off. Do these static properties stay in memory for the length of the
> user session or app session?


See the other replies - you need to be *very* careful with statics in
ASP.NET... They can very easily become common across all sessions which,
depending on what they're used for, could be the absolute last thing you
want...


 
Reply With Quote
 
john_c
Guest
Posts: n/a
 
      02-21-2007
Hi Milosz,

I like this approach but could you do it without inheritence?

 
Reply With Quote
 
john_c
Guest
Posts: n/a
 
      02-21-2007
No, it doesn't need to be in a session variable. I just need to load
the cache with values for each plan. Then I can check the User object
to find out which role/plan the user is in. From there I have the
values for the plan associated with the user in the Cache and can take
action to display something or not. But how could I cleanly
associate "yes, I know the user's role" with "in the Cache are all of
the values for that role/plan". I need some sort of constuct that
allows me to put those two things together initially. Then when I
need to check the MegaBytes of space a user has, I can just say
User.MBcount or something similar.

Thanks.

On Feb 20, 4:22 pm, "Mark Fitzpatrick" <(E-Mail Removed)> wrote:
> John,
> Do you really need it as a session variable? Will it be the same
> values for each session? If so then you're going to end up with a lot of
> useless memory usage. You could put the values into the cache object. You
> could do a check every so often, such as when a request starts, and check to
> see if the cache is empty. If so then load the object/array/etc. through
> some function and put it into the cache. This would give you a good bit of
> control as well since you could determine how much time should occur to
> cause the items to fall out of the cache.


 
Reply With Quote
 
=?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=
Guest
Posts: n/a
 
      02-21-2007
Yes, but ypu need to code quite a lot and you need to spend some time to
familirize youself with custom proofile providers. i'll perape a simple
example but tomorrow (wed late afternoon CET)

Regards

--
Milosz


"john_c" wrote:

> Hi Milosz,
>
> I like this approach but could you do it without inheritence?
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to do a lookup tree kelvSYC C++ 3 03-06-2006 06:39 AM
Best way to store a time? Tarun Mistry ASP .Net 1 02-22-2006 12:33 PM
Best way to store a large number of files? heather.fraser@gmail.com Java 13 10-10-2005 01:11 PM
Best way to load/store web site settings in database Max ASP .Net 2 12-08-2003 10:39 PM
best way to store dig.photo just_a_girl41 Digital Photography 1 08-22-2003 12:25 PM



Advertisments