where to put this code?

V

V. Jenks

I have a C# class that generates a string based on 2
integers. One integer is equal to int.MinValue and the
other is equal to int.MaxValue. Each time my class
generates a new string from those 2 numbers, one of the
integers is incremented.

What I need is a way to safely store the current string and
the 2 integers in a way that will guarantee I do not lose
the values, it is important that each time I generate the
string, it is unique, as it is used as a primary key in my
database tables.

If I were to load the values from say, an XML file when the
program loads in Application_Start in the Global.asax
code-behind, could I safely keep them in memory and are
they guaranteed to remain in state while the application is
running? I was thinking I would re-write the values back
into the XML file in the Application_End event handler.

BUT...when exactly is Application_Start fired and when
exactly is Application_End fired? What constitutes a web
application starting and ending? Does it end when there
are no more active sessions in the app?

Could I safely pull this off w/o any concurrency problems
when writing/reading to the XML files to get my values?

I would store these values in a database table but I don't
want to make a new connection and round-trip to the
database every time I need a value to insert as a primary
key, this would murder my app's performance.
 
L

Lucas Tam

What I need is a way to safely store the current string and
the 2 integers in a way that will guarantee I do not lose
the values, it is important that each time I generate the
string, it is unique, as it is used as a primary key in my
database tables.

Not a very safe way of generating PKs... Isn't there anything else you
can use a PK? Or this newly generate PK should be checked as soon as
possible to ensure it's uniquness.
If I were to load the values from say, an XML file when the
program loads in Application_Start in the Global.asax
code-behind, could I safely keep them in memory and are
they guaranteed to remain in state while the application is
running? I was thinking I would re-write the values back
into the XML file in the Application_End event handler.

No, if the application crashes, bye bye values!
BUT...when exactly is Application_Start fired and when
exactly is Application_End fired? What constitutes a web
application starting and ending? Does it end when there
are no more active sessions in the app?

Applicatin Start is fired when the application is first loaded.

Application End is fired when the application is shut down - I believe
typically 20+ minutes of inactivity. There maybe other situations where
application end is fired.

But generally speaking, you don't want to use Application_End for
process that MUST occur... because application_end has been shown to be
unreliable from time to time.
Could I safely pull this off w/o any concurrency problems
when writing/reading to the XML files to get my values?

I would store these values in a database table but I don't
want to make a new connection and round-trip to the
database every time I need a value to insert as a primary
key, this would murder my app's performance.

Perhaps you should look at generating the key in the database?

Or add some error handling logic in your PK generation routines... such
as checking the PK to make sure it's a valid PK before returning back to
the user. It's probably not a good idea just to rely on an XML file or
in memory store... as they could get out of sync with the server or data
could have been lost on a crash.
 
V

V. Jenks

I agree, it's not safe, I think I knew that when I posted,
just probing for a new perspective on my problem.

Technically, it could work, but I don't like the idea of
relying on a file and the events fired on the web
application itself, too disparate to be managed appropriately.

What I have is a 32-bit hexidecimal value that I use for a
primary key, I call it an OID. It is binary but can be
read as a 16-character string, prefixed by a "0x" (i.e.
"0x000045F4BD20A3C2"). In C# this is a byte array.

I use two 32-bit integers to generate these OIDs and use
them as my primary keys. This is useful because every
single record in my database is globally unique. Since it
would take a rediculously long time to run out of OIDs you
can even make your records globally unique across your
databases, should you require it.

Anyhow, I don't want to generate them in the database for 2
reasons.

1. Not portable. I have a T-SQL version already written
but now I'm using Firebird and I don't want to re-write the
logic in Firebird's query language. Even if I do, what if
I want to use this in another database someday? Obviously
a bad design choice.

2. Rount-Tripping & Performance. I would have to run
round-trip to a table on every single record added to the
database to get my OIDs. On bulk or large inserts, this
could be a real drag.

So, since I have already taken the time to re-write the
T-SQL procedure in C#, and it works great (using a file or
database table), I'd like to find the most
efficient/safe/portable way to implement it.

I gave you the short, now there's the long of it!

Thanks,

-v
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top