My first Lunch & Learn

March 13, 2008

Today I was so bold as to cram all the developers on my current project (there are four of us) into a small conference room during the lunch break and show them a screencast about agile development from dnrTV while we enjoyed our food. It was a fun little experiment and I think they appreciated the initiative, but the dnrTV videos are usually one hour long which, it transpires, is a bit too much. We have short lunch breaks, okay?

Does anyone know of other good .NET related videos that we can showcase? I’m thinking of Channel 9 as well, but what more is there? I’m looking mainly for C#, ASP.NET and agile stuff like TDD and so on. And a length of 30 minutes would be soup-erb (food related pun intended).

I’ve had Mono installed since I bought my Mac, but the lack of a good IDE made me frustrated enough to install a Windows XP virtual machine and use Visual Studio instead. Until now that is, because I listened to the .NET Rocks! show #313 yesterday where Miguel de Icaza and Geoff Norton talk about the upcoming Mono 2.0 and how the IDE MonoDevelop is included in the OS X installer for Mono. I’ve tried installing MonoDevelop before, but it was such a hassle on OS X that I just gave up. I need stuff to be easy, or I move on to something else.

After I installed the new Mono (1.2.6) package this morning and then dragged the MonoDevelop beta 3 to Applications, it was a great relief to see the IDE pop up. I created an ASP.NET site in a few seconds and fired it up using the built-in server. Then I added some code-behind and everything worked great. I haven’t tried anything more advanced, but I felt I was so excited about this product that I just had to blog about it instantly. If you’re a Mac & .NET freak (I’m looking in your direction Joakim), try this out poste haste!

Edit: The MonoDevelop installer I found was in the mono 1.2.6 dmg that I downloaded from www.mono-project.com. For some reason I can’t find a similar installer at the MonoDevelop site.

The ”as” operator in C#

December 4, 2007

I don’t know if it’s just me, but I don’t see people using the “as” operator in C# a lot when I read code. It’s a shame, because it’s really nifty. For example, you can use it like this when you retrieve something from the cache:

string s = Cache[cacheKey] as string;
if(s != null) {
    return s;
}
else {
    // Set s to a new value
    Cache[cacheKey] = s;
    return s;
}

This way you don’t have to explicitly check the type of the cache contents. If it can’t be casted, s will just contain null instead.

As I stated in the last post, I’m toying around a bit with Subkismet. It’s a great little tool for keeping comment spam at bay, which is a big problem at the EPiServer site I’m currently working on.

It’s also the first open source code that I’ve ever looked into, developed by Phil Haack et al. You can find out more at the project’s CodePlex site. It’s great fun to see how other people use unit testing, inheritance and so on, and a good way of learning more about your own programming style.

I found that the code didn’t do exactly what I wanted to, so I changed it a little, et voilà, I had a visible captcha ready after a few hours. Open source (“or as Microsoft pronounce it: ‘Open Sores’” – quote by Andy Hunt) is a beautiful thing sometimes.

I’m toying around with subkismet’s visible captcha just for fun, and if you use it with wingdings, no spam bot is ever going to crack it. No human either for that matter…

I’m currently working on website which is supposed to be able to manage many thousands of users at the same time. But we’re struggling with the performance. So I’ve found some quite useful tools for this:

  1. dotTrace is a profiler for .NET code. A great little tool for finding out what parts of your code take the most time/resources. It’s kinda expensive though.
  2. WebLOAD is an open source load testing solution for internet apps. I like it a lot, even though the documentation is a bit sparse and I’m not quite sure how to interpret all the data I get when testing my sites.

Apart from that, I’ve learned one important lesson when it comes to EPiServer and performance: do not use a lot of dynamic properties for large sites. They tend to suck the life out of the database server, we’ve found stored procedures that take 30-60 seconds just to get all the dynamic properties. Instead we’re going to make them ordinary page properties and push them downwards in the hierarchy.

I’ve been waiting for this day. Scott Guthrie has announced on his blog the forthcoming ASP.NET MVC Framework. Soooo hot!

On my web development computer I’ve got 5 (five!) browsers installed, for compability tests. I’ve got both IE7 and IE6, Opera, Firefox and Safari (only available in Beta for Windows so far). It’s amazing how most of the css-stuff can work on all browsers except for IE6, which is a bit annoying since it’s still the most popular web browser out there.

So what I usually do in ASP.NET is to dynamically select different stylesheets depending on which browser is requesting the page. It’s not as neat a solution as, say, putting it all in the same stylesheet and it’s magically working everywhere. But I have yet to see that…

How to do it? It’s easy, you can do it all inline in the <head>-tag if you like:

<% if(Request.Browser.Type.Equals("IE6")) { %>
<link rel="stylesheet" type="text/css" href="IE6.css" />
<% } else { %>
<link rel="stylesheet" type="text/css" href="AllOtherBrowsers.css" />
<% } %>

I usually only put the properties that differ between browsers in the above stylesheets. E.g. if you have a master stylesheet “Styles.css” it can contain:

.RoundedCornersBox
{
Margin-bottom: 10px;
Color: #FBFBFB;
/* Paddings are set in the browser specific stylesheets */
}

Then you can put, as the comment explains, the specific attributes in the other stylesheets:

[IE6.css]

.RoundedCornersBox
{
Padding-right: 16px;
}

[AllOtherBrowsers.css]

.RoundedCornersBox
{
Padding-right: 20px;
}

It’s the neatest solution I’ve seen so far, and the comment in the Styles.css points any future developers to where it is. Also if you’re using some debugging tool like FireBug, it’s usually not a problem at all finding out where a style property comes from in your page.

I’ve been struggling for a while with the installation of an EPiServer site. There’s always something going wrong when installing them it seems. I installed a blank 4.61/ASP.NET 1.1 site and then replaced some of the content with the files from the source control repository, since the whole site wasn’t in the source control system, only the files that had changed.

However, this led me to the following error after having compiled the site for the first time: When trying to open it, my browser just shouted:

“CS1595: ‘development.Global’ is defined in multiple places; using definition from ‘c:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\root\588a0258\4318cd2f\assembly\dl2\af592e75\ 60d0d532_03adc701\EPiServerSample.DLL’”

Okay, so I googled that error. The Microsoft knowledge base article didn’t seem to apply to me at all, instead I found the solution in the Velocity Reviews Forum, where this bloke Alvin Bruney suggests that if there are copies of the same assembly floating around you should:

“delete all these assemblies every where you find them on your hard drive, flush iis temporary files, then rebuild that particular assembly”

So that made my head snap back into it’s position: The EPiServerSample.DLL isn’t even supposed to be there, we’re not using it. So I just deleted it, and then emptied the C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\ folder and re-built the solution. Now there’s only one development.Global, in my real assembly.

Another example of how a tiny, simple problem can delay my work by some hours.

I struggled for like 8 hours with this issue. I had three columns defined as divs on a page. At every 10th or so refresh, the footer div of the page jumped up and cut off the content of the column divs. And this only happened in IE7. Really weird.

Then a friend of mine suggested I add a clear div after the three columns. I did just that, adding an empty div referring to as css class named “clear” which used clear: both, margin and padding 0 everywhere. And it worked!

Thanks Lars!