473,661 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best way to store lookup variables?

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_MBc ount] = "30"
Session[Utils.RoleA_Ban dwidthLimit] = "1000"
Session[Utils.RoleA_Fea tureBcountLimit] = "5"
Session[Utils.RoleA_Fea tureCcountLimit] = "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

Feb 20 '07 #1
11 3022
"john_c" <jo***@bigstrin g.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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...
Feb 20 '07 #2
On Feb 20, 3:34 pm, "Mark Rae" <m...@markNOSPA Mrae.comwrote:
"john_c" <j...@bigstring .comwrote in message

news:11******** **************@ a75g2000cwd.goo glegroups.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.Rol eA.MBcount]
Session[Utils.Roles.Rol eA.BandwidthLim it]
Session[Utils.Roles.Rol eA.FeatureBcoun tLimit]
Session[Utils.Roles.Rol eA.FeatureCcoun tLimit]

Feb 20 '07 #3
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).

Feb 20 '07 #4
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" <jo***@bigstrin g.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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_MBc ount] = "30"
Session[Utils.RoleA_Ban dwidthLimit] = "1000"
Session[Utils.RoleA_Fea tureBcountLimit] = "5"
Session[Utils.RoleA_Fea tureCcountLimit] = "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

Feb 21 '07 #5
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.P age
{
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.Sess ionState.HttpSe ssionState session;

public RoleAProperties (System.Web.Ses sionState.HttpS essionState session)
{
if (session == null)
throw new NullReferenceEx ception("sessio n");
this.session = session;
}

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

-- begin any aspx page code behind --
public partial class MyAnyPage : RoleBasePage
{
protected void Page_Load(objec t 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" <jo***@bigstrin g.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.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...
Feb 21 '07 #6
"john_c" <jo***@bigstrin g.comwrote in message
news:11******** *************@a 75g2000cwd.goog legroups.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...
Feb 21 '07 #7
Hi Milosz,

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

Feb 21 '07 #8
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" <markf...@fitzm e.comwrote:
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.
Feb 21 '07 #9
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?

Feb 21 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1855
by: Paul | last post by:
Hi. Just trying to find out the best approach as I beleive it might give me problems later on down the road. I have an ASP.NET application which references a shared database class which contains methods for serialising and de-serialising objects to the database storage. I put this as a shared class as multiple web clients will be using the class to store and retreive data, the problem I'm haivng now is that I think multiple threads...
1
3096
by: James E | last post by:
I have a question about best practices of how to deal with lookup data from my C# apps. On a couple of occasions I have come across a problem where I have to automate inserting a record into a table that has a foreign key constraint that is linked to a lookup table. E.g. Take the following database structure: SQL-Server Database: Table 1:
8
2030
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function lookup'. "This is especially usefull in a loop where you call the function a zillion time" they say. I think this is very odd behavior.
2
2509
by: Max | last post by:
This is the best method I've found to store and load settings from database. I load the settings once at start as public variables. The method can be called easily anytime you want to load the new settings from database. Comments very welcome... I'd like to refine this... besides the error handling I need to add for the db conn... Global.asax --------------- Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim...
17
5523
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables too much - that it's bad programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don't want to make a federal case out of it. This comes up over and over...
3
2136
by: gordon | last post by:
Hi I am looking to store some details about a user's configuration choices, in particular the place where they have installed some data files, the OS that they use, and their Windows user name. This information is used in a windows C#.net application. I would like to capture this info the first time that the user opens the app, but each subsequent time to make sure that the location is current when they open the application. This is...
3
1980
by: Nemisis | last post by:
Guys, I would like to write a error handler, or something, that will allow me to write to a database when an error occurs on my site. I am trying to implement this in the global.asax file a the moment, but am having problems when a 404 error occurs, i cant access sessionstate. Is writing this code in the global.asax file the best way to do this? I have been searching on the net and hear alot about httphandlers? Will a httphanlder...
6
4085
by: pj | last post by:
Hi, I 'm currently writing a program that performs transliteration (i.e., converts greek text written using the english alphabet to "pure" greek text using the greek alphabet) as part of my thesis. I have decided to add the capability to convert words using some sort of lookup algorithm as a sidekick to the "normal" conversion algorithm, and here it starts getting interesting. I want to find an algorithm that satisfies these...
3
4022
by: at_the_gonq | last post by:
Hello, I am hoping to get some guidance on the following scenerio: I have a password protected site where users have various permissions. Are sessions the best way of storing the user's id? And if so, on load of a page should I be hitting the database for their permissions (based on the session stored user id), or should everything I need be stored in session variables to save the trip to the database? I have also wondered about...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.