|
<br />
public class SessionWrapper<br />
{<br />
<br />
private HttpSessionState session;<br />
<br />
<br />
public SessionWrapper()<br />
{<br />
session = HttpContext.Current.Session;<br />
}<br />
<br />
public string UserName<br />
{<br />
get{return (string)session[SE_USER_NAME];}<br />
set{session[SE_USER_NAME] = value;}<br />
}<br />
<br />
public int? LastPurchase<br />
{<br />
get{return (int?)session[SE_LAST_PURCHASE];}<br />
set{session[SE_LAST_PURCHASE] = value;}<br />
}<br />
<br />
<br />
}<br />
<br />
I'm actualy working on a code generator to build stuff like that for me, but at the time it was written manualy.
My argument was its a strongly typed wrapper that exposes other developers to exactly what they need, what type it is, and hides the constants.
T
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
Makes sense to me.
A similar thing I do is with ExecuteScalar() , which returns object which then (usually) requires a test for null and a cast, and setting a default value if null. I wrote a wrapper like public virtual T ExecuteScalar<T>(T IfNull){...} to handle the cast. Any time I'm writing a scalar query and don't have to test for null and/or do a cast I save time, the code is more readable too.
Usually, spending the time to encapsulate such common tasks will pay off.
"A stitch in time saves nine."
|
|
|
|
|
Well i spent a few months crusading for sensible coding practices, and now i've given up. The other day i was told to remove it, re-centralise all the constants and simply use Session[AppConstants.CONST_NAME] to access everything.
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
Glad I'm in a small IT department and have free reign on what I write.
|
|
|
|
|
I'm in a small IT department too, that was the CTO and Senior Developer pulling rank on my design decision. :S
(I'm leaving in a week anyway )
T
-------------------------------
Carrier Bags - 21st Century Tumbleweed.
|
|
|
|
|
I just spent the day (working from home) writing a wrapper for NumericUpDown that is generic and will take any of the built-in numerics, hide the actual decimal, and handle the casting so I don't have to.
Ugly as Hillary, but it works.
|
|
|
|
|
Maybe it was just the code.
Strong typing is a good way to control up front what type of information you are looking for rather than waiting for it to hit the database or a flat file and finding out later it isnt what it should be.
CleaKO
"I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that.'" - Tommy (Tommy Boy) "Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)
|
|
|
|
|
|
While converting an existing internal application from vb6 to c# i came accross the following :
'Reusable code -- Cut from here --
do some work here
'Reusable code -- Cut to here --
So you can guess what happens when you change something in that code block
http://sourceforge.net/projects/banshee32
|
|
|
|
|
Ummm... no? I'm guessing either nothing or something catastrophic.
|
|
|
|
|
PIEBALDconsult wrote: something catastrophic
Yes if you don't copy&paste modifications of that so called "reusable" code to all places where it was "reused".
"Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus
"Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Ah! A Visual Studio Code snippet!
|
|
|
|
|
Ah, now that makes sense... kinda.
|
|
|
|
|
Some of those "snippets" were indeed pasted all over the place ... Imagine my surprise when the original author could not understand why his updates didnt work like it should
|
|
|
|
|
Maybe he should use the "C preprocessor" with his BASIC code.
|
|
|
|
|
Very nice resusable code
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
If txtProvCt.Text = txtProvCtS Then
Else
giUpdate = True
End If
|
|
|
|
|
and not unusual...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
CPallini wrote: and not unusual...
Queue the Tom Jones soundtrack.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Lol, i used to do that when i first was teaching myself, then i discovered if not.
Posted by The ANZAC
|
|
|
|
|
Ah a VB users snippet, lack of understanding promotes bad coding.
.net is a box of never ending treasures, every day I get find another gem.
|
|
|
|
|
It's one of my coding rules, which is really useful once you get the point :
if(NULL != l_poStrTest)
{
l_poStrTest->Format
( "My code : %d"
, l_nErrorCode
);
}
else
{
}
It's also all about defines :
#define MY_TEST
#ifndef MY_TEST
int l_nSize = GetStringSize(l_poStrText);
#else // MY_TEST
#endif // MY_TEST
By doing so, I write the full structure of the code which is then ready to fill up, and it also helps to keep an eye-track of the execution logic (inequal statement first) so that you don't have to know if the test is about inequality (!= or #ifndef) or equality (== or #ifdef). You just have to spot the upper or lower block of code and see if it is defined/filled or not...
This also force to structure carefully your code and put some air between the lines, instead to write a rock-compact block of code...
Kochise
PS : I often write empty equality code like this
if(NULL != l_poStrTest)
{
l_poStrTest->Format
( "My code : %d"
, l_nErrorCode
);
}
else{}
In Code we trust !
|
|
|
|
|
= is more faster than !=..thats why
Regards,
Sylvester G
sylvester_g_m@yahoo.com
|
|
|
|
|
I was never one for the intricacies of the C preprocessor. On my latest project, apparently (or rather, unfortunately) someone is.
An .h file has a bunch of definitions (real variable names changed to protect the innocent):
statedefs.h:
<br />
makeState(State1, 0x01)<br />
makeState(State2, 0x02)<br />
makeState(State3, 0x03)<br />
Odd, I think, but ok. Then I go look up "makeState". What the... 3 entries? That's not a good sign.
Instance 1:
<br />
#define makeState(NAME, NUM) ST_##NAME## = ##NUM##,<br />
typedef enum {<br />
#include "statedefs.h"<br />
ST_LAST } statetype;<br />
Instance 2:
Embedded in the middle of a function(!)
<br />
#undef makestate<br />
#define makeState(NAME, NUM) somevariable++;<br />
#include "statedefs.h"<br />
Instance 3:
<br />
#define makestate(NAME, NUM) { "ST_"#NAME,0,0},<br />
sometype stateinfo[] =<br />
{<br />
#include "statedefs.h"<br />
}<br />
There are no external programs being used, no weird autogeneration stuff.. the entire set of code is a replacement for:
<br />
enum<br />
{<br />
ST_State1 = 1,<br />
ST_State2 = 2,<br />
ST_State3 = 3,<br />
<br />
ST_LAST<br />
}<br />
Most editors I've tried cannot tag it, due to the preprocessor concatenation used ("##" in instance 1). References don't work either. And this is one of the easier to read examples of preprocessor abuse...
I just try and remind myself that code written this way makes it that much easier on my next job interview!
|
|
|
|
|
#define makeState(NAME, NUM) ST_##NAME## = ##NUM##,
To add insult to, well, insult - he only needed the first ## . Maybe he thought the macro params needed to be "quoted" with ## ?
|
|
|
|