# Friday, September 24, 2004

The mouse arrived today, and a worthy companion has been found and purchased (getting it brand new for $1100 cheaper than retail, thankfully). Soon (within a week or so), they will join together to create sweet sounds (or code, if you want to be technical). This will be a nice development machine with which to craft the code of my dreams.

I am starting a new job on Monday, working for TEK Systems (as a W-2 employee!) on a contract in downtown Salt Lake. I am leaving behind an independent contract job (as a 1099 contractor . . . yuck) which was fun for a while.

Okay, I am rambling now and really need to get back to installing this beautiful mouse on my current machine, so that I can break it in while it is waiting for its future partner!

Kick It on DotNetKicks.com

posted on Friday, September 24, 2004 12:49:07 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [2]
# Wednesday, September 22, 2004

Behold this beautiful manifestation of engineering prowess:


What is it? Well, it is none other than the latest and greatest
Bluetooth-utilizing mouse from the people in Redmond. It will be arriving at my home, in all of its unabashed glory, in the next 48 hours, where it will await a worthy companion which I am currently in the process of selecting. Together, they (along with one enhancement and another) will enable the user to have what he considers to be the ultimate setup. No wires attached. Oh yes, what fun will be had!
Kick It on DotNetKicks.com

posted on Wednesday, September 22, 2004 6:04:46 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]

In addition to code generation, object/relational mapping is a subject that makes me feel all warm and fuzzy inside. Although I have not been able to use an O/R Mapper as of yet on any projects (not because of lack of desire, only because of lack of support for doing so among the developers I have worked with), I have read about and wanted to get involved with them for probably a year and a half now.

After reading Paul Wilson's rant (via Steve) regarding mappers, I think I am going to finally force myself to sit down and implement a project using one.

As with code gen, O/R mapping is something which, unfortunately (or fortunately, depending on how you look at it (I use code gen, for example, so I can often move quicker on a given project than unbelievers (read: I can do it cheaper))) receives little attention. Funny thing is that people in the J2EE world are familiar with and utilize this pattern, and have for quite a while (Hibernate, SimpleORM). As seems typical with those in the Microsoft camp, slow adoption is prevalent, and amazingly few people really even know what it is or why it may be useful.

Having long been a user of his free DAL generator, LLBLGen (going on 2 years now), Bouma's LLBLGen Pro appeals to me simply because I have watched Frans posts in the Architecture forum at www.asp.net over the last couple of years, and I like his ideas and agree with his opinions (though I will admit my opinions are not as well-informed as his are!).

Until I feel compelled to spend the money and buy LLBLGen Pro, I will give Paul's mapper a try at the much lower cost of $50 (after trying the demo of course). Unfortunately, my anticipation of seeing ObjectSpaces was ruined by Microsoft's inevitable delay thereof. Oh well, what can you do?

Kick It on DotNetKicks.com

posted on Wednesday, September 22, 2004 6:37:03 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [1]
# Thursday, August 26, 2004

This is more of a note to myself, nothing too interesting. Simply some links to interesting articles on patterns and such:
Enterprise Development Patterns
Patterns & Practices
Improving .NET Application Performance and Scalability

 

Kick It on DotNetKicks.com

posted on Thursday, August 26, 2004 6:20:15 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Tuesday, August 24, 2004

I do a lot of ASP.NET development, but I don't have many reasons to mess around much with writing custom web controls and the like (I use quite a number of 3rd party controls as well as the .NET framework controls). I write user controls quite a bit, but have not had much time to look into compiled controls. Someday maybe I will get that chance. I did create one control that is not very exciting but has been useful for me, and herein I will describe it.

First, let me tell you about the scenario that brings me to this point. I use the DropDownList quite a bit. The usual thing I do is bind a collection to it and have AutoPostBack set to true so that I can then do some other thing with the selected value (drilling down into nested data, for example).

For an example, let's say I am binding a list of names to this list. When the user sees this list, the first member of the collection shows up already selected in the dropdownlist. This means that the only way I am going to get autopostback to work on the control is if I select any name other than that one. But what if I want to choose that one? The obvious answer is to place something in that first position. Here is what I usually do:

ClientDDL.DataSource = UserManager.GetAll();
ClientDDL.DataTextField = "Name";
ClientDDL.DataValueField = "UserID";
ClientDDL.DataBind();
ClientDDL.Items.Insert(0, new ListItem("Choose . . .", "Choose . . ."));

Then, in the handler for the SelectedIndexChanged event, I use the selected value to do something (constrain another list maybe) and then do this:

if(ClientDDL.Items[0].Value == "Choose . . .") ClientDDL.Items.RemoveAt(0);

That effecitively gets rid of that first option (the "Choose . . .") so that 1) no one can choose that option and 2) because there is no longer a need for it.

This is a recurring pattern, and I knew I could encapsulate this into a derived version of this control. So, I wrote the following class:

public class MyDropDownList : System.Web.UI.WebControls.DropDownList
{
   protected override void
OnLoad(EventArgs e)
   {
      base
.OnLoad (e);
      if(!this
.Page.IsPostBack)
      {
         this.Items.Insert(0, new ListItem("Choose . . .", "Choose . . ."
));
      }
   }
   protected override void
OnSelectedIndexChanged(EventArgs e)
   {
      base
.OnSelectedIndexChanged (e);
      if(this.Items[0].Value == "Choose . . .") this
.Items.RemoveAt(0);
   }
}

I compiled this and added the DLL to my toolbox. Now, all I do is drag that to the design surface and bind to it like so:

ClientDDL.DataSource = UserManager.GetAll();
ClientDDL.DataTextField = "Name";
ClientDDL.DataValueField = "UserID";
ClientDDL.DataBind();

So, I don't have to think about this "Choose . . . " line anymore, it is all taken care of automagically for me because I have encapsulated it in that derived control. I don't think this is necessarily the ideal way to do this, but it does seem to work just fine (I just did this moments ago, so maybe further testing will reveal some flaw or another). Again, for what I am doing this is perfect. It may not be the ideal control for other uses of a dropdownlist.

Kick It on DotNetKicks.com

posted on Tuesday, August 24, 2004 6:35:38 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Sunday, August 22, 2004

For one of the innumerable personal apps I am writing, I decided to store some values in session variables. However, I loathe the syntax for both getting and setting such variables:

Session["UserID"] = userID;

GetSomeData(Convert.ToInt16(Session["UserID"]));

Yuck. Why? The literal strings. I want strongly-typed stuff. I don't want to have the opportunity to accidentally type Session["UsrID"] and deal with resulting issues (I know, it is not that big of a deal, but you know), I want design-time features like code completion/Intellisense. So, at first my idea was to create an enum containing all of the session variables I would use for the app, like so:

public enum SessionVariables
{
   UserID, ClientID, etc.
}

And then I would do this:

Session[SessionVariables.UserID.ToString()] = userID;

That gave me Intellisense and strong typing (more so than a string literal), reducing the chance that something would be typed incorrectly.

But I still don't like the whole "Session[...]" thing. In order to understand my solution, I first must say that I have created a base class for my site's pages. Every ASP.NET page must inherit from the System.Web.UI.Page class at some point, either directly or as a result of a deeper inheritance structure, and I have an intermediate class which inherits from that class, and then all of my pages inherit from that derived class. So, the solution I came up with (I doubt this is in any way original, nor do I feel it is necessarily the ideal) involves creating properties on that base class. For example:

      public short User_UserID
      {
         set
         {
            Session["UserID"] = value;
         }
         get
         {
            return short.Parse(Session["UserID"].ToString());
         }
      }

As long as the page inherits from this intermediate base class instead of inheriting directly from the Page class, I have an easy, strongly-typed means of obtaining session variables without using the nasty syntax (I only need to use it in the property). So, now I can do this in my code-behind files:

User_UserID = userID;

GetSomeData(User_UserID);

Much sexier, and as an added bonus it abstracts WHERE I store the variables. So, if I want to, I can change that property in the base class so that instead of storing this value in session, I can store and retrieve it from a cookie should I desire to do so. (By the way, the reason I preface the property with the "User_" is because it makes it gives the property name a context, in this case these are properties associated with the current User. Also, one of the variables I am storing is a ClientID and that is already the name of a property of the Page class, so I have a collision there.)

Now, the question I have is this: is this a good way to accomplish this? It works, and I personally like it, but I have only thought it through for about 25 minutes.

Kick It on DotNetKicks.com

posted on Sunday, August 22, 2004 5:48:02 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [2]
# Wednesday, August 18, 2004

[Note: This is by NO means an exhaustive discussion of code gen, and only illustrates the most miniscule example of what you can do and how it can be done.]

One of the topics I really have an interest in is code generation ('code gen' for short). Some call it useless, while others, such as myself, find it compelling. I don't know how I would live without it, since writing redundant code is one of those things I have much apathy for. Why write code that can be generated with the click of a button?

For those not familiar with this concept, I will give you a simplified example. Let's say you have a SQL Server database that has a table with the following schema:

Now, you usually have to perform the usual CRUD operations on such a table, with various variations, but for the most part, we can generally assume you want to INSERT new records into this table, SELECT all the data from the table, UPDATE records, and DELETE records. You get the idea.

We can *easily* generate a set of 4 stored procedures (sprocs) to do these things. It is ridiculously easy in fact. There are sprocs extant after you install SQL Server that will allow you to obtain metadata about tables and their fields, their relationships, etc. Once we have this data, we can generate our sprocs. Example code for generating the SELECT sproc:

    // let's assume the name of the table is in this string variable, "tableName"
    // and all of the fields of the table are in this array variable, "fieldNameArray"
    // (we obtained that info by querying the database for the metadata)

    string selectAllSproc = "CREATE PROCEDURE " + tableName + "_SelectAll AS SELECT ";

    for(int i = 0; i < fieldNameArray.Length; i++)
    {
       selectAllSproc += fieldNameArray[i];
       selectAllSproc += (i == (fieldNameArray.Length - 1)) ? " " : ", ";
    }

    selectAllSproc += "FROM " + tableName + " GO";

When this is done, our string selectAllSproc has the value:

CREATE PROCEDURE User_SelectAll AS SELECT UserID, Username, Password, FirstName, LastName, DateCreated, Status FROM User GO

So, if we were to print that to a text file (probably would be formatted better), we would have a script we could run, thus creating the sproc in our database (obviously there is a little more to it, but really that is the essence).

Pretty nice. Imagine your database has 45 tables. Do you really want to hand-craft all of that code? You should be shaking your head "NO" right now. So, not only does this save us time, but it improves consistency, because we know that if this runs right once, it will do the same for all tables (Again, I am generalizing, I don't want to write a book on the subject!).

So, in our schema, we have this field "Status" right? Maybe I only want that column to contain 2 different values, namely "Active" or "Inactive". Well first off we need a check constraint attached to that table indicating that those are the only valid values. Again, we can query the database and find this out so that programmatically, we can access that info. Well, this is perfect for generating an enumeration like this:

    public enum Status
    {
        Active, Inactive
    }

This can then be used in our code when we want to give someone the option of choosing a value for that field, thereby helping strongly-type the data being passed around (read: maintain the integrity of data, thereby reducing the possibility of errors).

Although simplified, I hope this example shows just what kind of possibilities there are for using code generation in your development process. It can truly be a boon to productivity and overall code quality if done well.

Kick It on DotNetKicks.com

posted on Wednesday, August 18, 2004 6:48:16 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [0]
# Wednesday, August 11, 2004

Artificial black holes: on the threshold of new physics

Can that be a good thing? Help me live, we don't need to be creating black holes so close to home, don't these scientists realize that although they may think they know what they are doing, this could be disastrous?! Apparently splitting the atom and fusing hydrogen is not enough to keep these types happy. No, they want to go and create mini universe-eating objects. I think they need a new hobby or else we are all in trouble!

This is the part of the article I personally love:

"In reality, there is no risk posed by creating artificial black holes, at least not in the manner planned with the LHC."

"[A]t least not in [this] manner?!?!?!" Oh so you acknowledge that, although these guys at the LHC are not doing it, there is some other manner in which to create these things, and maybe that is not so safe! Great, go ahead and plant the idea in our heads (and the heads of those that might want to use it to their advantage!). Yeah, we thought nukes were bad? No, the next WMDs are going to be little black holes. Seriously, I wonder just what conditions happen to cause the manner in which these are made to become critical? One well-placed black hole would ruin our day for sure. It would just feed itself until it had eaten all of earth and then start feeding on our solar system in some sort of cosmic all-you-can-eat buffet.

"The black holes produced at CERN will be millions of times smaller than the nucleus of an atom; too small to swallow much of anything. And they'll only live for a tiny fraction of a second, too short a time to swallow anything around them even if they wanted to."

Oh really?!?! You sure about that? Scientists have had many theories in the past that have been proven incorrect, and I don't think this is one we want to be proven as such by virtue of our planet getting sucked into one. Seriously, can't we do this somewhere else, say maybe one quintillion light-years away from here?

"If it makes you feel any more comfortable, we're pretty sure that if the LHC can produce black holes, then so can cosmic rays, high-energy particles that smash into our atmosphere every day. There are probably a few tiny black holes forming and dying far above you right now. So I think we should all relax, fire up the Large Hadron Collider, and get ready for a view of the universe that we've never seen before."

Oh sure, I feel so much better. Whatever! Stop this madness now! I think Senator Kerry should address this situation and make it an important issue for the election, it may help him win . . .

See you in the singularity!

Kick It on DotNetKicks.com

posted on Wednesday, August 11, 2004 6:12:37 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [1]
# Thursday, July 29, 2004

So, I was curious as to how many lines of code are extant in a project I have been working on for the last 8 months (not full-time, I have only worked on it for 120 hours or so (a really rough estimate) since starting). I don't know if VSEA can do this automagically or not, but I thought it would be a relatively trivial task to write some code to do it for me. So I wrote a small method that takes a directory path and counts the number of lines in all of the files in that directory. It was so simple, yet so . . . fun.

Lame? Maybe. I like to think not. In fact, I want to more fully develop this little "tool" so that it is more "robust" in that it can do lots more. I am thinking of having it do all sorts of crazy, semi-useless things, all for the purpose of providing me with the simple joy that comes from small things. I was even thinking that the main app (both a command-line and WinForms UI) would be nothing more than a glorified file and directory chooser, and then use reflection to then send the selected files to any and all plug-ins living in a specified folder. You know, like maybe you select a directory, tell it you want to only process files ending in either a .cs or .aspx file extension in that directory and it's children, and then choose from a list of plug-ins that will then take those files and perform some processing. So maybe you have one plug-in that counts lines in the files (with the option to ignore lines that have nothing on them) and another plug-in that searches for matches to a regular expression or something. Yeah, although this has probably been done and it is not hard, it would be fun to do and somewhat useful . . . possibly. :P

Kick It on DotNetKicks.com

posted on Thursday, July 29, 2004 6:03:50 AM (Mountain Daylight Time, UTC-06:00)  #    Comments [9]
# Tuesday, July 27, 2004

So, I am always interested to see how people land on my blog, so I go looking at my traffic logs. There have been 3 occasions in the last week or so where people have searched for "free porn" and "free smut" and such things and landed here. First, it is sad that people go looking for such deplorable material, and secondly that my blog shows up in the results! I wonder if the people that click to come to my site think they are going to see links to such stuff, I bet they are really disappointed when they see this place for what it is . . .

Kick It on DotNetKicks.com

posted on Tuesday, July 27, 2004 5:12:19 PM (Mountain Daylight Time, UTC-06:00)  #    Comments [3]