Archive for July 2008

Inspired by Rob Conery’s great work on his MVC Storefront screencasts, I decided to look into his Pipes and Filters implementation and attempt to create my own.  Although there was a lot of critical commentary on some of the design decisions, I found myself very intrigued by this fluent interface implementation.  I haven’t referenced back to the specific code samples so if it differs significantly, please keep in mind that this was certainly inspired by it.

A very nice feature of Linq to Sql is that it delays query execution until you start enumerating over the collection.  This lets the developer build complex queries programatically and make only one call to the database.  Linq to Sql is smart enough to derive the resulting T-SQL to produce your result set. 

For this scenario, let’s assume we want to select blogs that are tagged with specific tags and the blog name starts with a certain string.  Below we can see a simple entity model that shows the relationships among the Blog, BlogTag and tag tables. 

Let’s keep the domain-specific language for our extension method names.  I’ll create two extension methods: “AreTaggedWith” and “BlogNameStartsWith”.  The former will accept a string array of tags and the latter will accept a string parameter.  Both of these methods extend the IQueryable<Blog> interface and return the same. 

 

We can then “daisy-chain” these all together to create are linq query before actually enumerating (and therefore executing) the final query.  This will produce a nice looking, fluent interface for our data access layer.

Using SQL Server Profiler, we can take a look at the query that Linq to Sql actually generated and how it is executed with input parameters and all. 

 

Looking at the query above, I’d love to hear your thoughts on whether this is the most tuned query for this scenario.  Did Linq to Sql did a good job generating the query?

Part of a project I’m working on requires users to tag certain items.  These tags are subscribed to by other systems and are also used to provide some neat auto-complete features for other inputs throughout the site.  Since new tags can be added by a user, I want to make sure that when the tag is added, the capitalization is in Title Case.  Here’s a quick demonstration of how to use mbUnit’s RowTest feature to allow you to test more scenarios with less code.  First, lets take a standard, single scenario test:





As you can see above, I am setting an expectation on my repository that when it is called, it will insert the original input as a properly formatted string.  The formatting will be implemented within my service class.  Running this yielded a filed test – RED (good!). 


Now let’s implement this feature:





I decided to create a string extension method which uses the little known ToTitleCase method of the TextInfo class.  This method accepts a string and returns it as a title-cased string.  Also note how we can split the dot-notation across lines making it much cleaner.  Below is the actual service call which will format the string and call my IRepository method:


 


After implementing this, I went back to the test and ran it.  Success!

At this point, something occurred to me.  The tag name “test tag” is easy to verify.  However, how will this implementation handle capitalization rules?  For example, if I enter “LCD Repair” as a tag, it should NOT format it to “Lcd Repair”.  This is where the RowTest feature shines.  Instead of writing a new test for each of these scenarios, I just converted my “Test” attribute to “RowTest” and changed the method signature.





As we can see, the method signature matches the individual Row parameters.  When we run this test again, it executes the test once for each row.  When you actually look at the test runner, it shows up as 3 individual tests so each row can fail independently.


Hope this helps!

If you are looking for ASP.NET hosting, I HIGHLY recommend you check out DailyRazor.com

I wouldn’t usually write up about a hosting company unless I really liked what they were offering.  I think they have the best deal out there for ASP.NET hosting.  I recommended them to a friend who was looking to host his site and he was extremely grateful.  I figured the rest of the ASP.NET community might appreciate it as well.  I’m actually hosting a few websites with them using the ASP.NET MVC Framework and have no complaints.  I am especially impressed with their technical support.  They respond quickly and solve issues with very little “back and forth”.

They also have this “quadruple promotion” that lets you upgrade any plan (starting from 5.95/month) to quadruple the features you get.  The cheapest package of 5.95/month gets you 8 websites and 8 MSSQL 2005 databases.  This is the best deal I’ve found on hosting.  I think you also get 15% off of your order.  If you’re looking for hosting, I highly recommend these guys. Make sure you ask about the “quadruple promotion” if you don’t see an add for it.  Check them out:  http://www.dailyrazor.com.