Mike Bosch's Blog on .NET

Agile enterprise architecture in .NET, SOA, WCF, WS-*, AJAX, MVC, Sharepoint and more...
GiftListr Beta - ASP.NET MVC Facebook Application

I am officially announcing the beta release of the GiftListr for Facebook application.  

GiftListr is the only truly universal gift registry allowing you to add products from ANY website without limitations.  Just browse stores the way you usually do when you shop online.  Once you find the item you want, simply copy-and-paste the url into the GiftListr application.  GiftListr will then download all the product information and product images available.  You can add notes to each item and share you gift list with friends.

The application is built entirely on ASP.NET MVC and makes extensive use of the jQuery library.  It also uses the Facebook Developer Toolkit available on CodePlex.  I would love to hear some feedback from the ASP.NET community.  Let me know what you think.  Any features you'd like to see?  Would you be willing to contribute to it if I add it to CodePlex as a community project?

Posted: Aug 05 2008, 11:26 AM by MikeBosch | with 13 comment(s) |
Filed under:
Part II - Fluent Filters, IQueryable and Linq To Sql for Easy Data Access

I decided to write a second part to my previous post on extending the IQueryable interface to create a really neat and organized data access layer with Linq To Sql.  The cool thing about this practice is that it nicely separates individual filters for integration testing and then lets you combine them to form much more complex queries. 

I added a new filter, CreatedBefore, which simply filters on the created date of the blog. 

 

I can now use this by itself or daisy-chain it with the other filters I've created.  The resulting T-SQL is below.  Note the new CreateDate filter's effect on it.

Linq to Sql gets even cooler!  Let's say a Blog had associated Posts (a standard one-to-many, parent-child relationship).  This would result in a entity model similar to the one below.

 

Using the same filters I already created for the Blog entity and combining it with the DataLoadOptions property of the data context, I can now get not just the filtered Blog entities, but their associated Posts in just one trip to the database!  Nice! 

I've blogged about how to use the DataLoadOptions in a previous post.  Setting this as usual gives you the immediate result you're looking for. 

The generated T-SQL statements start to get a little uglier, but I'll take the Pepsi challenge when it comes to some custom SQL a developer might have written for this same functionality.

Hope this helps!

Posted: Aug 01 2008, 08:00 AM by MikeBosch | with 3 comment(s) |
Filed under: ,
Fluent Filters, IQueryable and Linq To Sql for Easy Data Access

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?

Posted: Jul 31 2008, 09:00 AM by MikeBosch | with 5 comment(s) |
Filed under: ,
Using mbUnit's RowTest Attribute in a TDD Scenario

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!

Posted: Jul 30 2008, 10:00 AM by MikeBosch | with 1 comment(s) |
Filed under: ,
ASP.NET Hosting Recommendation (if you're on a budget)

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.

 

Posted: Jul 22 2008, 01:44 PM by MikeBosch | with 6 comment(s) |
Filed under:
Debugging Running Processes in Visual Studio

I recently was overhearing the conversation a colleague of mine was having about debugging a large Win Forms application.  It seems he was jumping through some major hoops to be able to debug the application.  Without getting too much into the architecture of the solution, it was instantiating other applications through a messaging system backend to communicate between apps.  In order to debug it, the team was using some code workarounds to kill processes, muddle around with new threads and processes, create a new process in debug which then impersonates some other process etc.  At least that’s what I think was happening. 

This kind of debugging situation can be solved easily by using the "Attach to Process" debug functionality in Visual Studio.  I was surprised that the team didn't know about this feature so I wanted to post a quick "How-To" on using it so others can benefit from it.

A couple of prerequisites must exist in order to attach to a running process.
(1) The source must match the running application
(2) The running application must have been compiled in debug mode

It's fairly simple.  When you open up the source in Visual Studio, just navigate to the Debug menu and you'll find a menu item "Attach to Process". 

 

When you select this, you will be presented with a window that shows all your running processes.  Simply click on the process you want to attach to and you're done. 

 

If you're debugging a web application and using IIS to debug, you can even attach to the ASP.NET worker process "aspnet_wp.exe".  This will allow you to break on external requests to your web app.  If you're using the ASP.NET development server, you'll also find this on the list of processes you can attach to. 

Hope this helps.

 

Posted: Jun 20 2008, 08:30 AM by MikeBosch | with no comments |
Filed under:
A "Friday the Thirteenths" Solution

There was a recent post on LessThanDot to create a small application to find all the "Friday the 13th's" occuring in a range of dates.  Here is my simple solution to the challenge.  I also made it flexible enought to find any occurrence of a day / day of week combination within that range.  It runs surprisingly quick even when looking over the next 500 years.

Also, if you like my color scheme, I've made it available for download here.

Posted: Jun 09 2008, 12:26 PM by MikeBosch | with 2 comment(s)
Filed under:
Showcase of "Live" ASP.NET MVC Sites

It looks like people are starting to deploy sites based on the ASP.NET MVC Framework already.  I've been trying to collect some examples and here's what I got so far.

If you have a site that you're developing using the ASP.NET MVC Framework, please let me know about it so I can include it here. 

Posted: May 05 2008, 08:00 AM by MikeBosch | with 19 comment(s) |
Filed under: ,
Real World Website Architectures

Have you ever wondered how major sites are logically and physically built?  Not as some high level with shaded boxes but at real platform level details.

I’ve been looking for a site like this for a while. No theory here, just real details about the servers, platforms, db’s, caching, apache mods etc. that the “big boys” use – Facebook, Digg, YouTube, craigslist, MySpace, Wikipedia, PlentyOfFish, Amazon and more...

http://highscalability.com

Posted: Apr 23 2008, 09:49 AM by MikeBosch | with 3 comment(s) |
Filed under:
Using the ComponentController in ASP.NET MVC CTP 2

ASP.NET MVC CTP 2 shipped with a new controller class which you may have missed.  Community feedback begged for a better way to handle component pages that don't rely on the main page's view data.  A classic example might be a stock ticker.  We would want a reusable component that you can place in other pages.

The ComponentController allows you to call a controller action from within the page itself.  It's use is very similar to the regular controller.  You basically create a new class the inherits ComponentController.  Then you create public methods as the actions of the controller.  Within the action method, you can implement whatever logic you need the component to perform.  You finally call RenderView to render the view just like the regular controllers.  

It's important to note that the ComponentController doesn't look in the same places as the regular controller when finding the views.  On this CTP, all component views need to reside in the right directory for the RenderView method to find it.  The convention is Components/[ComponentControllerName]/Views/[ViewName] directory.  In this case, the name of my controller is MyComponentController and the view name is CustomerOrderView

 

I'm not a big fan of forcing convention this since I may want to put my components wherever I please, but I digress.  Let's look at the controller.


This very simple controller has a single action named ShowCustomerOrders.  This method calls my order repository and returns a list of orders for a customer.  I will be able to use this on any view where I need a quick summary of the customer orders.  It accepts a CustomerID and renders a view named CustomerOrderView.

From within the page, I'm using the lambda notion to make the call to this controller.  The Html helper has a method called "RenderComponent".  I specify my component controller and the action it should call.  In this case we are passing the customer ID "1".   This can be easily replaced with some of the view's ViewData properties.


Taking a look at the view that my component will render, we see it is a simple list using a strongly-typed CustomerOrder[] as the ViewData.


That's all there is to it.  When we run the page, our customer orders show up where I placed the call to the RenderComponent method.  

Hope this helps!

UPDATE:  I uploaded the source code for the example.  Download it here.

Posted: Mar 10 2008, 12:45 PM by MikeBosch | with 18 comment(s) |
Filed under: ,
More Posts Next page »