Developing For .NET

Real World .NET Methods, Tricks, and Examples

Archive for June, 2008

[REPOST] – RVNUG May 2008

Last night I attended the monthly RVNUG (Roanoke Valley .NET User’s Group) meeting. Kevin Hazzard gave a good presentation on Silverlight 2.0, which I have been eager to try. After what I saw last night, and now that I have a little experience with WPF, I am even more eager to give it a shot. What I am not eager to do is what I saw last night, and that is Silverlight development in Visual Studio. More specifically, I have no desire to hand code XAML, which is why I like Blend so much.

Today I’ll be downloading and installing the Silverlight 2.0 Beta 1 Tools, and I may install Blend 2.5 Preview (which I hear is far from rady for prime time). I’ll be putting together a couple Silverlight samples in the next week or so. If I come up with anything interesting, I’ll be sure to share it.

No comments

[REPOST] – Local Method Practical Example

I recently wrote about the idea of using Generic Delegates to create Local Methods (methods wholly defined within the scope of another method). Today, I had a practical application for this technique so I wanted to share it.

In this example, our task is to build a portion of an SQL update string that contains only the fields within a class that have been changed. The class stores original values and current values, so finding the changed fields is a simple matter of comparison. We are going to use a StringBuilder to construct the string. The problem comes in properly appending a comma between each name-value pair string. You cannot always add one after adding a new string since it may be the last, so the solution is to check the length of the StringBuilder before appending the string: if it is greater than 0, then we append a comma first.

The task is simple enough, but what we end up with is a lot of redundant code:

StringBuilder updateString = new StringBuilder();
if (this.FIELD1 != this.o_FIELD1 )
{
updateString.Append( “FIELD1 = ‘” + this.FIELD1 + “’” );
}
if (updateString.Length > 0)
{
updateString.Append(“,”);
}
if (this.FIELD2 != this.o_FIELD2 )
{
updateString.Append( “FIELD2 = ‘” + this.FIELD2 + “’” );
}
if (updateString.Length > 0)
{
updateString.Append(“,”);
}
if (this.FIELD3 != this.o_FIELD3 )
{
updateString.Append( “FIELD3 = ‘” + this.FIELD3 + “’” );
}
if (updateString.Length > 0)
{
updateString.Append(“,”);
}
// Etc.

As you can see, this repeatedly defines the same behavior. It is especially aggravating when dealing with some of our legacy database tables that have hundreds of fields. While it may not be likely in this example, if we had to change this behavior it would be very tedious and some changes could easily be missed. This action is only relevant within the context of our Update method, so I really don’t want to outsource this to a private class method. Instead I will use a Generic Delegate to create a Local Method.

Since I am updating a local variable, I really don’t need to pass it: I’m going to take advantage of local variable access to reduce the parameter list and simplify the method signature. I do not need a return value, so I am going to use Action<T> to define a method that accepts a string and appends it to the StringBuilder and handles the commas as necessary. To ensure I can access the desired StringBuilder variable, I need to define it before defining my Local Method:

StringBuilder updateString = new StringBuilder();
Action AppendUpdateString = new Action(s =>
{
if (updateString.Length > 0)
{
updateString.Append(“,”);
}
updateString.Append(s);
});

Now, I simply call the method at each desired instance (also defined after the Local Method declaration):

if(this.FIELD1 != this.o_FIELD1 )
{
AppendUpdateString( “FIELD1 = ‘” + this.FIELD1 + “’” );
}
if (this.FIELD2 != this.o_FIELD2 )
{
AppendUpdateString( “FIELD2 = ‘” + this.FIELD2 + “’” );
}
if (this.FIELD3 != this.o_FIELD3 )
{
AppendUpdateString( “FIELD3 = ‘” + this.FIELD3 + “’” );
}
// Etc.

And this code could probably be simplified a little more, but I think this is adequate to the task. Hopefully, you’ll agree that this technique has merit. It’s very clean and easy to implement. I really like this, so much so that I am going to need to be careful to take my own advice and not go crazy with this approach.

1 comment

[REPOST] – Deploying ASP.NET MVC on IIS6.0

Last week, I finally got the ball rolling on our IIS server. With a little help from a friend, I managed to get the SSL Certificate installed. A quick tutorial, and the site was moved to the server but it would not respond. Monday, I managed to get the site to respond by installing the .NET 3.5 framework on the server and I mistakenly thought it was up and running, so I released it to the beta testers. Of course, immediately the testers informed me that only the home page was showing, while all other links returned a 404 message.

I went through several efforts and can now happily say that it does work, so I am officially saying it is deployed. It is far from finished, but it is live on the web (sorry, I can’t share the address at this time). Here are the steps I followed that got it running:

  1. Install .Net Framework 3.5 on IIS Server
  2. Change Route URLs in Global.aspx.cs to include the .mvc extension (this is in the comments for that file)
  3. Install ASP.NET MVC Preview 2 on the IIS Server.
  4. Add an ISAPI extension to the application for .mvc that points to “c:\windows\microsoft.net\framework\v2.0.50727\aspnet_isapi.dll”.
  5. Uncheck the “Verify that file exists” box on the Extensions configuration dialog.

The last one is what finally made all the pieces fall into place. I don’t know if it is a “DUH” sort of a thing, but you have to remember that I am not an IIS or ASP.NET guy.

I found information on all but the last entry scattered across the web. Hopefully by putting them all in one place it will help some poor wayward soul in the future. According to those resources, these steps should also work for IIS7 in classic mode (integration mode? are those the same?) but should be unnecessary in the default IIS7 configuration.

At any rate, it lives!

1 comment

[REPOST] – Geeking on a Saturday

In a recent post entitled I am a Professional Geek, I mentioned that I only geek at work. Well, Saturday I had the opportunity to prove just how deep that particular Rabbit hole goes… I gave a presentation on .NET 3.5 Language Enhancements at the Richmond Code Camp Saturday morning, and boy did I stink! I really had trouble focusing my message and expressing myself. I was live coding and kept screwing up the syntax (to those attending, I was missing the delegate keyword on the Local Methods demonstration). We have a tendency to judge ourselves harshly, so I can only hope that the attendees didn’t think it went as poorly as I felt.

I guess every one has off days: Babe Ruth struck out a lot and Edison found 2,000 ways not to make a light bulb. Even Einstein stank up the joint on occasion (read about “The Ether” sometime). Not that I am Einstein, Edison, or Ruth: clearly I’m not. But I believe it is important to give ourselves these pep talks when we mess up. Let’s face it: as programmers, we mess up a lot!

So what’s the lesson? For me it is that weekends and technology don’t mix. Hopefully I can redeem myself in June when I will be presenting at RVNUG on ASP.NET MVC. At least that is during the week!

No comments

[REPOST] – Of Mice and Men(tors)

Just a weekly update…

When I bought this machine, I picked up a new wireless mouse and keyboard. About a week ago, I noticed that my mouse had a flashing red light on it. I had never seen it before, and I quickly realized that this is a battery low indicator light. I had mixed feelings about this. First, I appreciate the notice, because there is nothing more frustrating than being deep in the zone and the mouse or keyboard runs out of power. But when I got to thinking about it, I thought that in a way it is foolish. Just when the power is getting low, you add an additional energy draw by powering a flashing light! I even went around the office showing everyone and commenting about how dumb it was. I likened it to the bank drawing an overdraft charge when you bounce a check. Hello! The problem is there is no money in there in the first place! <sheesh>

Well, today it finally died… right in the middle of a great coding session. Naturally, I was annoyed, both at the interruption and (sheepishly) at the realization that I should have obeyed the warning and replaced the stinking batteries. But I did learn two things: first, the light flashed for over a week before the batteries finally died, and second I learned that the mouse will function on a single battery (which I discovered when I was replacing the batteries). Interesting.

On the Mentor side (to make the title work), I will be giving my “.NET 3.5 Language Enhancements” presentation tomorrow morning at the Richmond Code Camp. Unfortunately I’ll be driving in and immediately driving out and will miss some of the presentations I would like to see. If you see me there, mention the blog and say hello.

No comments

« Previous PageNext Page »