she swears <i>geek</i> is a term of endearment

SubSonic "SELECT WHERE IN" solution

June 25th, 2008 Rusty

The In Problem

I searched to find out how to execute a pretty common query in SubSonic but found nothing.

Let’s say you want to accomplish the following result

SELECT * 
FROM MyTable 
WHERE Id IN( ‘val1′, ‘val2′, ‘val3′ )

That’s pretty straight forward, no?!

However, There doesn’t seem to be any way to make subsonic collections do that.  At least this appears to be true when using Guid’s

I tried to make SubSonic.Comparison.In work for me but had no luck.

The Solution

public SubsonicDB.MyClassCollection GetByIds( List<Guid> MyClassIds )
{
    // SubsonicDB represents my generated namespace of active record classes
    // wtf?  have to convert to object[]…
    object[] items = new object[MyClassIds.Count];
    for( int idx = 0; idx < MyClassIds.Count; idx++ )
    {
        items[idx] = MyClassIds[idx].ToString();
    }
 
    // create "IN" query
    SubSonic.Query query = new SubSonic.Query( SubsonicDB.MyClass.Schema );
    // create collection
    SubsonicDB.MyClassCollection results = new SubsonicDB.MyClassCollection();
    // create query and execute reader, filling collection
    results.LoadAndCloseReader( query.IN( SubsonicDB.MyClass.Columns.Id, items ).ExecuteReader() );
    return results;
}

 

This worked.  I am posting it in the hopes of saving someone the time and trouble of trying ten other ways that look like they should work bu, indeed, do not…

New Site Woes…

June 20th, 2008 Rusty

My new beta site went down this morning.  The whole server is non-responsive but it certainly appears that the beta site was the cause.  I checked the log files and Google was going ape shit.  I thought it was unwise to link to it in my previous post.  I actually thought, "Google is going to sniff this out and index our Beta site.  Then I’ll have to 301 beta."   But I followed that thought with, "at least I can uncover performance issues while time is on my side."  Sometimes I hate being right  :>(

Setting the Active Record straight

June 19th, 2008 Rusty

I was asked yesterday whether my new site, beta.ockhamresearch.com, was developed using Ruby on Rails.  In a very indirect way, sort of.  Ok, no, not at all!  Its Asp.Net MVC

Mvc stands for Model View Controller, a popular design pattern for modern website architecture.  I feel it is significantly more productive and effective than the page-centric architecture you find as the default in most web programming languages.  Ruby’s first popular website platform was Ruby on Rails.  There were other MVC implementations out there already for platforms such as Java and Smalltalk.  However, Rails maid it intuitive, productive and even fun.  This led other programmers to favor the pattern for developing websites (such as myself) and led to the development of Castle Monorail in dot net and other variants as well as on other platforms.  Many of these were ports of Rails for folks who weren’t able to use Ruby as their target language for one reason or another.  I had been using Monorail for some time when Microsoft announced the development of Asp.Net MVC.  Microsoft isn’t well-known for open development initiatives and often does things the Microsoft way rather than the conventional way.  The Microsoft Asp.Net team deserves much credit for the current quality of Asp.Net MVC and the integration of tremendous community feedback. 

Why not just use Rails?

Competency

I have very little experience with Ruby.  When I need to write some Ruby code, I need to look up each construct and find examples to work from.  I can code C# in the woods on the trunk of a tree.  Of course, compiling it would be a challenge.

Confidence

I know where Asp.Net and SQL Server limitations are and that the platform can achieve high performance.  I know that Ruby has known performance issues and no roadmap for reconciling these issues.  I’d rather not be the person responsible for overcoming performance issues on a production web server farm when we have paying clients expecting service.  If I can run a commercially supported platform with current licenses and support contracts, I have someone to lean on if anything goes wrong.  Furthermore, I can fix almost anything on Windows but my experience with Linux is feeble.  I aim to change that but, for now, I can provide confidence to my constituents that my web app will not fail due to memory leaks or unusual bugs that are not a result of my code.  Yes, there will definitely be errors in our future but using NUnit, Cruisecontrol.Net, WatiN, Visual Studio.Net and SQL Server will minimize the risk to a more than acceptable level.  I don’t know where to turn for the same thing in Rails.

Community

This is the most important factor.  The above two points are true for all the programmers I’ve worked with over more than the last decadeWe’re all very familiar and invested in Microsoft technologies.  There is no reason to switch if the features are available and productivity is commensurate with alternatives.  If I need help modifying some code because I have too much to do myself or we have an awesome contract pending, I don’t want to be searching in a small pond for good people. For every good programmer, there are a hundred terrible ones that will make a mess of my beautiful code :)  I can’t have that. 

Why Use Rails at All?

I can’t find current data that indicates the trend for what programmers are using but I can tell you my gut feelings and personal observations.  No community growth, in my technology career, has struck me with as much fervor and excitement as the Ruby and Rails explosion has.  These people aren’t Windows haters like Java programmers were.  They aren’t choosing Ruby because that’s where the jobs are (yet).  They are choosing Ruby because they get hooked!  They love working with it.  More often than not, a Ruby programmer dresses hip, is very laid back, uses a Mac, drives a Hybrid.  These are all traits of people with both time and money.  I want more time and money and would like to be hip.  However, I still prefer to burn more gas in exchange for fast times to sixty.  Glutinous, I know.

When do you switch?

Rails still needs to be proven in the enterprise.  My prediction is that this will happen in 2008.  Sites using Ruby on Rails will publish numbers and costs of operation that make it a very attractive alternative to the company standard.  The best place to try Ruby is on a new, small scoped project with just a few very close end users in a highly agile environment.  Don’t be silly and just announce that you are now Ruby and expect all your devs to just pick it up.  On the other hand, it would be a good idea to get some very intense training for your old school programmers (if you have them) because they are accustomed to working within the constraints of their strongly typed, compiled, high performance environment and will not naturally make the appropriate transitions necessary to achieve success, maintainability and scalability in a Rails environment.  Ask yourself if your compiled apps (yes, Asp.Net is compiled) are as fast as they should be and then ask yourself whether your app will still be acceptable at 50% of that speed.  On the other hand, if you have 80% of your hardware capacity remaining, you can probably virtualize and take advantage of your current infrastructure with very little cost.

Try it on like deodorant.  Give it a few days and try a few different activities and then ask your wife if your pits stink.  If not, take it a little further.

Extension Methods Grant C# Wishes

June 9th, 2008 Rusty

Have you ever found yourself calling some utility method frequently and you wished it were just part of the class you always used it on?  For example, isNumeric was very useful in VB and I’ve missed it in C#.  Sure, we now have int.TryParse() but that is much more cumbersome.  I like how you can rearrange JavaScript to do your bidding using prototype inheritance.  Of course, you can certainly get into trouble and there’s always that paranoid fear that someone might slip the carpet out from under you.

Method After Thoughts

I was xhtml validating my site, using w3c validator, when I (re)discovered that my urls must have the "&" escaped when they are used in an attribute.  Href is an attribute. 

  • Mvc UrlHelper.Action() returns a url. 
  • I’m calling that from my view. 
  • I do not want to call a bunch of utilities all over the views in my site. 
  • I want valid Xhtml.

Poking around, using Reflector, I discovered that the HtmlHelper ActionLink uses HttpUtility.HtmlAttributeEncode() to create valid hrefs for links in html.  I needed my css class name on that link and wasn’t familiar with how to use the linq expressions to generate my links.  It would be so easy if…

I sure wish UrlHelper had an encoding action method built in. 

Extension Method for attribute encoding UrlHelper.Action()

public static class MvcExtensionMethods
{
    /// <summary>
    /// Encodes the url so that it can be used in an xml (or xhtml) attribute such as href (escape ampersands and such)
    /// </summary>
    public static string ActionAttrEncode( this System.Web.Mvc.UrlHelper url, string actionName, string controllerName, object values )
    {
        return HttpUtility.HtmlAttributeEncode( url.Action( actionName, controllerName, values ) );
    }
}

…and now I have a new Method!

image

Sitemap Provider throws empty HttpException when reserved word is used for url

June 4th, 2008 Rusty

I ran across a strange bug today.  Fortunately, Bill saw it first and lead me straight to it.  I banged my head against it (unsuccessfully), trying to diagnose the cause of a System.Web.HttpException with no message being thrown from SiteNavNode.ChildNodes until my battery died on my laptop and then came into the office to continue.  Good thing!

In the Financial Services industry, there is a company with the symbol PRN.  I, of course, use directory urls to organize our dynamic website.  PRN is a reserved word for urls because it is a reserved file system word.  In my opinion, modern OS’s should not have this limitation, but I don’t know enough about it to suggest that it isn’t necessary.

If you attempt to create a file, with any extension, using the following list, it will fail to resolve in a browser.

Reserved Word

What it is

AUX Auxiliary port aka Serial Port COM1
CON Short for console, which Microsoft describes as the display monitor.
COM1 COM port.
COM2 COM port.
COM3 COM port.
COM4 COM port.
LPT1 LPT port.
LPT2 LPT port.
LPT3 LPT port.
NUL NULL
PRN Printer aka LPT1

This is also true of rewritten urls.  I defensively have incorporated a lovely "~" into any url that attempts to name the path segment using one of these reserved words. 

Setting up SMTP virtual server in IIS 6.0

June 3rd, 2008 Rusty

Sending notification emails from an app is a pretty straight forward thing.  I can’t think of an server app that I’ve used or written where this wasn’t a requirement.  A good designer will abstract the details of sending email into a notifications object model where smtp is one provider of delivery services.  However, you still have to set up an email server.  You will also bump against a third party product that needs smtp to function.  We use Google Apps for email.  By doing so we have no need for email administration.  Google handles that for us.  Perhaps they’ll appreciate our patronage when they take over the world.  Who knows?

But I stil need a dang smtp server.  Widows Server 2003 comes with IIS 6.0, which comes with a free smtp server.  GREAT!  There’s a pop server, too, but that’s outta scope.

Enable SMTP

Install SMTP.  That link should have you running smtp services.  Pretty simple, so far.

Configure Access

I’m only including the relevant screens to get it going.  By no means is this an Enterprise Solution but it will get you sending email.

image

Open IIS Admin.  Right click on the SMTP Virtual Server.   Choose "Properties"

Click on the Access tab.

Click on Relay.

This is the part that messed with me all day.  I was getting "Cannot Relay" errors trying to send.  When I Enabled "All Except the list below", with no exceptions listed, email went out.  So I played around until I got to this config. image

 

 

YMMV

The first listed setting is the following:

image  I’m a little rusty on my subnetting (pun intended) but that should allow all local network connections to relay through this server.  All I knows is that it done did worked right.

 

Ports and such

I experimented quite a bit with ports, only to find that changing the outgoing connection port makes it impossible to connect to real smtp server.  So leave it all default unless you know what the hell you are doing. 

Note: I read that leaving "Anonymous Access" enabled without some sort of authentication option means that the server never authenticates but treats all connections as autheticated.  Therefore, uncheck that box on Relay Restrctions to "Allow computers which successfully authenticate to relay…"  Unless you would like to see how long it takes spammers to discover your open server :)