BusinessRx Community

Dedicated to the advancement of software, technology and the people who devote their lives to it.

Welcome to BusinessRx Community Sign in | Join | Help
in Search

BusinessRx Reading List

These blog entries are written by industry experts and leaders. We consider this content to be a good read for any software developer or web technologist.

April 2008 - Posts

  • New Lounge Front-end (now with ASP.NET MVC and NHaml)

    Since taking over The Lounge I have been slowly refactoring it to my liking. Not that there was much wrong with it, but like most developers I am pretty opinionated in how I think something should be built. Most of what I have done has been in an effort to make a total rewrite of the front-end easier. The biggest change was refactoring the front-end and models to move as much logic as possible to the models and wrap tests around those models. I consider this a good practice and since I knew I wanted to re-write the front-end it was doubly important.

    When it came time to start the rewrite I decided to go with ASP.NET MVC, even though it's in an early stage I couldn't imagine writing classic ASP.NET if I could do anything to avoid it. I also decided to go with NHaml for the UI to stay away from .aspx completely and because I am a big fan of the original Haml.

    The re-write went very smooth and the site is now live, I know I am not the first site to go live with ASP.NET MVC, but it has to be at least one of the first ones. Especially one of the first ones that is an actual business and not just an experiment. I only ran into a couple issues along the way and they were easy to figure out.

    The simplicity of these two technologies is a joy, for instance here is the controller action for showing a room:

    public void Show(string id)
    {
      Room room = Room.GetRoomByCode(id);
      
      room.LoadVIPs();
    
      RenderView("show", room);
    }
    

    Thats about as simple as you get. (next version we will be able to drop the "show" in the RenderView, even better!)

    Here is a excerpt from the view:

        -foreach(TheLounge.Model.Vip vip in ViewData.Vips)
          #roomdetail
            %a{href=vip.Url,class="borderit"}      
              %img{src="../Content/images/vip/" + vip.Image, class="left"}
            .viptitle
              %a{href=vip.Url}
                = vip.Title
            .vipdesc
              = vip.Bio
            .clearleft
    

    I love it. No controls, no viewstate, just simple web programming.

    I am going to post a number of posts on things I learned about MVC and nHaml, especially about some best practices I learned in rails that I think apply very well to MVC.

    -James

  • Graffiti.Redirect 0.2

    I just pushed out a new release of Graffiti.Redirect with a couple minor fixes and one big fix.

    A couple weeks ago I was talking with Rob Bazinet about moving his blog to Graffiti and I told him he should use my plugin to redirect his old blog to his new blog. He tried it out and ran into a couple problems so he checked out the code and found a huge block of ugly code I had written to append default.aspx to requests, he tried deleting that entire piece of code and just using the simple redirect and everything still worked. On top of everything still working it fixed a couple of annoying bugs.

    It turns out that code is only needed for IIS5 and not needed for IIS6+, it has to do with how wild-card request handling is done. I was doing my testing on XP with IIS 5 so I had to write the code to get it working, but since I am deploying on IIS6 I don't need it either. So I decided to keep Rob's change and drop IIS 5 support from the plugin, I doubt many people are using IIS5 and if they are they can still download the old version and fix the bugs themselves.

    I am thrilled to have a leaner code base and even more thrilled to have a couple annoying bugs fixed, I love open source.

    -James

  • April 28th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Silverlight

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page and Silverlight Tutorials page for links to popular articles I've done myself in the past.

    ASP.NET

    • Displaying the Number of Active Users on an ASP.NET Site: Scott Mitchell continues his excellent series on ASP.NET's membership, roles, and profile support.  In this article he discusses how to use ASP.NET's Membership features to estimate and display the number of active users currently visiting a site.

    • ASP.NET Dynamic Data Update: The ASP.NET team last week released an update of the new ASP.NET Dynamic Data feature.  This update adds several new features including cleaner URL support using the same URL routing feature that ASP.NET MVC uses, as well as better confirmation, foreign-key, and template support. 

    ASP.NET AJAX

    • ASP.NET AJAX UI Templates: Nikhil Kothari from the ASP.NET team has a cool post that shows off a prototype he has been working on that enables clean client-side AJAX templating of UI. 

    ASP.NET MVC

    • Inversion of Control, ASP.NET MVC and Unit Testing: Fredrik Kalseth has a cool article that talks about the concepts behind inversion of control (IOC) and how you can use this with ASP.NET MVC to better isolate dependencies and enable better unit testing of your code.

    Silverlight

    • Silverlight - the Song: Spike Xavier and Dan Wahlin have posted another of their unique and special songs. :-)

    Hope this helps,

    Scott

  • Slides from my ASP.NET Connections Orlando Talks

    Last week I presented at the ASP.NET Connections Conference in Orlando.  I gave a general session talk on Monday, and then two breakout talks later that day.  You can download my slides+samples below:

    General Session

    The slides for my keynote can be downloaded here

    In the talk I demonstrated how to debug the .NET Framework source code.  You can learn how to set this up with VS 2008 here

    I also demonstrated building a site using the new ASP.NET Dynamic Data support - which you can learn more about here.  I also demonstrated using the new ASP.NET MVC Framework - which you can learn more about here.

    I also showed off the new Hard Rock Memorabilia site built with Silverlight 2.  You can try out the Hard Rock application yourself here.  You can learn more about Silverlight from my links page here.

    Building .NET Applications with Silverlight

    The slides + demos for Silverlight breakout talk can be downloaded here.

    You can learn more about Silverlight from my links page here.  In particular, I recommend reading my tutorial posts here and here.

    ASP.NET MVC

    The slides + demos for my ASP.NET MVC talk can be downloaded here.

    You can learn more about the latest ASP.NET MVC source refresh here.  Stephen Walther also just posted a really good set of slides + demos from his post conference tutorial on ASP.NET MVC here.

    Hope this helps,

    Scott

  • Microsoft versus Open Source Software - ALT.NET notes

    At the Seattle alt.net conference, I co-sponsored a session with Justin Angel. The topic was "Choosing Microsoft versus Mature Open Source Alternatives". We wanted to hear the rationale people were using when making choices, like:

    LINQ to SQL or Castle Active Record

    Entity Framework or NHibernate

    Subversion and assorted tools or Team Foundation Server

    Not once do I remember price being a factor. Most of the fishbowl conversation revolved around risk. There are risks that technical people don't like, and risks that business people don't like. I tried to take all the major topics mentioned and fit them into the following table.


    Choose Microsoft

    Choose OSS

    Business Risks


    License issues

    Lack of formal support

    Hard to hire experts

    Technical Risks

    V1 and V2 won't always work

    Waiting on bug fixes

    Friction

    Small communities

    Lack of training material

    Quick summary - Microsoft is a safe choice from the business perspective, but MSFT products can create an uphill struggle for developers. Brad Abrams and ScottGu both popped into the fishbowl to talk about Microsoft's change of direction in building closed source frameworks with "big bang" releases. ScottGu also reminded us that patent trolls create problems for everyone in the ecosystem.

    ALT.NET Trivia

    How much ALT.NET can you fit in a Hyundai?

    According to Hertz, the Hyundai Elantra will accommodate 5 people and 3 pieces of luggage.

    The Elantra I drove into Redmond accommodated 5 people (me, Jeremy Miller, Udi Dahan, Steven "I Love The Back Middle Seat" Harman, and Ayende), 6 pieces of luggage, and 3, maybe 4 laptop bags. It was tight. 

    Who said developers can't optimize for space anymore?

  • Focusing on Focus

    My guest post over on 47hats is now live. I wanted to detail some of the recent things I have been doing to help me focus better, and Bob was gracious enough to let me do a guest post over on his site.

    Let me know what you think.

    -James

  • Free hosting for user groups from Applied Innovations

    I saw this over on the Applied Innovations blog (who happens to be a Lounge advertiser) and it looks pretty cool:

    "Today, we announced a partnership with Kentico, makers of the Kentico CMS to bring their CMS solution to Dot Net User Groups for free. This partnership will provide a free copy of Kentico CMS Enterprise ($1500 value) to .NET user groups with a custom website template built for .NET user groups combined with a Free, full-featured hosting account from Applied Innovations to host their portal. "

    I hadn't heard of Kentico CMS before, but the demo user group site they have setup looks pretty freaking nice. I have seen some pretty crappy user group sites in my day (and built at least one crappy user group site) but now there really is no excuse.

    -James

  • A Gentle Introduction to Mocking

    At the last CMAP Code Camp I did a "code-only" presentation entitled "A Gentle Introduction to Mocking". We wrote down some requirements, opened Visual Studio, and started writing unit tests. Matt Podwysocki provided color commentary. Code download is here.

    I started "accepting" mock objects as one tool in my unit testing toolbox about three years ago (see "The 5 Stages Of Mocking"). Times have changed quite a bit since then, and the tools have improved dramatically. During the presentation we used the following:

    Rhino Mocks – the first mocking framework used in the presentation. Years ago, Oren and Rhino Mocks saved us from "string based" mock objects. Rhino Mocks can easily conjure up a strongly typed mock object. The strong typing results in fewer errors, and greatly enhances the refactoring experience.

    moq – is the latest mocking framework in the .NET space and is authored by kzu and friends. moq uses lambda expressions and expression trees to define mock object behavior, and also provides strongly typed mocks. The recent addition of factories and mock verification means you can do traditional interaction style testing with moq, if that is the path you choose. The primary differentiator between the two frameworks is that moq does not use a record / playback paradigm.

    Here is a test we wrote with Rhino Mocks:

    [Fact]
    public void Does_Not_Make_Deposit_When_Verification_Fails()
    {
        
    MockRepository mocks = new MockRepository();
        
    IAuditService auditService = mocks.DynamicMock<IAuditService>();
        
    IVerificationService verificationService = mocks.CreateMock<IVerificationService>();

        
    decimal amount = 1000;
        
    BankAccount account = new BankAccount(auditService,
                                              verificationService);

        
    using (mocks.Record())
        {
            
    Expect.Call(verificationService.VerifyDeposit(account, amount))
                  .Return(
    false);

        }

        
    using (mocks.Playback())
        {
            account.Deposit(amount);
        }

        account.Balance.ShouldEqual(0);

    }

    The same test using moq:

    [Fact]
    public void Does_Not_Make_Deposit_When_Verification_Fails()
    {
        
    Mock<IAuditService> _auditMock = new Mock<IAuditService>();
        
    Mock<IVerificationService> _verificationMock = new Mock<IVerificationService>();

        
    decimal amount = 1000;
        
    BankAccount account = new BankAccount(_auditMock.Object, _verificationMock.Object);


        _auditMock.Expect(a => a.WriteMessage(
    It.IsAny<string>()));
        _verificationMock.Expect(v => v.VerifyDeposit(account, amount))
                         .Returns(
    false);

        account.Deposit(amount);
        account.Balance.ShouldEqual(0);            
    }

    xUnit.net – although not featured in the presentation, xUnit.net drove all the unit tests. xUnit is a new framework authored by Jim Newkirk and Brad Wilson. The framework codifies some unit testing best practices and takes advantage of new features in the C# language and .NET framework. I like it.

    One question that came up a few times was "when should I use a mock object framework"? Turns out I've been asked a lot of questions starting with when lately, so I'll answer that question in the next post.

  • ASP.NET MVC Source Refresh Preview

    We recently opened up a new ASP.NET CodePlex Project that we will be using to provide previews (with buildable source code) for several upcoming ASP.NET features and releases.

    Last month we used it to publish the first drop of the ASP.NET MVC source code.  This first drop included the source for the ASP.NET MVC Preview 2 release that we shipped at MIX, along with Visual Studio project files to enable you to patch and build it yourself.

    A few hours ago we published a refresh of the ASP.NET MVC source code on the site.  This source refresh is not an official new ASP.NET MVC preview release - instead it is an interim drop that provides a look at the current state of the source tree.  We will ship the official "ASP.NET MVC Preview 3" release in a few weeks after we finish up some more work (more features and tweaks to existing ones, better VS tool integration, VS express edition support, documentation, etc).  If you are someone who wants a hassle-free installation of ASP.NET MVC to use that ships with documentation and full tool support you'll probably want to wait for this official preview release.  If you are someone who wants a chance to see an early "preview of the preview" and have the opportunity to start using and giving feedback on some of the features immediately, today's source refresh is probably interesting to look at.

    Improvements with this ASP.NET MVC Source Refresh

    This week's update (which you can download here) includes a number of improvements to ASP.NET MVC.  Some of these include:

    • In addition to posting the source code for the ASP.NET MVC framework, we are also posting the source code for the unit tests that we use to test it.  These tests are implemented using MSTest and the open source Moq mocking framework.  A VS 2008 project file for the unit tests is included to make it easy to build and run them locally within your VS 2008 IDE.

    • Significantly easier support for testing Controller classes.  You can now unit test common Controller scenarios without having to mock any objects (more details on how this works below).

    • Several nice feature additions and usability improvements to the URL routing system (more details below).

    Creating a New ASP.NET MVC Project

    You can build your own copy of the ASP.NET MVC assemblies by downloading the MVC source and compiling it locally, or alternatively you can download a VS Template package to get a pre-built version of them along with a Visual Studio project template that you can use to quickly build a new ASP.NET MVC Project that uses the latest bits.

    After you install the ASP.NET MVC source refresh .VSI template, a new "ASP.NET MVC Application" project template will show up under the "My Templates" section of your "New Project" dialog:

    This new "My Templates" version of the MVC project template lives side-by-side with the previous ASP.NET MVC Preview 2 release (which you can see above it in the main project templates section of the dialog).  This allows you to safely create new projects and and use both the latest source version and the last official preview version on the same machine.

    When you create a new project using this updated ASP.NET MVC Project template you'll by default get a project that looks like below:

    This new project solution contains one Controller ("HomeController") under the "\Controllers" directory and two View templates ("About" and "Index") under the "\Views\Home" sub-directory.  Both view templates are based on a common master page for the site ("Site.master"), all of whose styles are defined within a "Site.css" file under the "\Content" directory.

    When you run the application the built-in web-server will automatically start up and you'll see the site's "Home" content:

    Clicking the "About us" tab will then display the "About" content:

    The "HomeController" class in the project is responsible for handling both of the URLs above and has two action methods like below:

    The default "Site.master" template looks for a "Title" value in the ViewData collection and uses it to render the <title> element of the HTML page.  The default "Index" view template looks for a "Message" value and uses it to render the home page's welcome message.  You can obviously go in and customize these files however you want.

    Controller Changes with this ASP.NET MVC Drop

    If you were reading the above code closely you might have noticed a few changes with how Controller classes are by default implemented using this new ASP.NET MVC source refresh drop. 

    With the ASP.NET MVC Preview 2 release the above HomeController action methods would have instead been implemented like below:

    The MVC feature team is experimenting with a few ideas in this week's drop and are trying out some new ideas:

    1. Action methods on Controllers now by default return an "ActionResult" object (instead of void).  This ActionResult object indicates the result from an action (a view to render, a URL to redirect to, another action/route to execute, etc). 

    2. The RenderView(), RedirectToAction(), and Redirect() helper methods on the Controller base class now return typed ActionResult objects (which you can further manipulate or return back from action methods).

    3. The RenderView() helper method can now be called without having to explicitly pass in the name of the view template to render.  When you omit the template name the RenderView() method will by default use the name of the action method as the name of the view template to render.  So calling "RenderView()" with no parameters inside the "About()" action method is now the same as explicitly writing "RenderView('About')".

    It is pretty easy to update existing Controller classes built with Preview 2 to use this new pattern (just change void to ActionResult and add a return statement in front of any RenderView or RedirectToAction helper method calls).

    Returning ActionResult Objects from Action Methods

    So why change Controller action methods to return ActionResult objects by default instead of returning void?  A number of other popular Web-MVC frameworks use the return object approach (including Django, Tapestry and others), and we found for ASP.NET MVC that it brought a few nice benefits:

    1. It enables much cleaner and easier unit testing support for Controllers.  You no longer have to mock out methods on the Response object or ViewEngine objects in order to unit test the response behavior of action methods.  Instead, you can simply assert conditions using the ActionResult object returned from calling the Action method within your unit test (see next section below).

    2. It can make Controller logic flow intentions a little clearer and more explicit in scenarios where there might be two different outcomes depending on some condition (for example: redirect if condition A is true, otherwise render a view template it is false).  This can make non-trivial controller action method code easier to read and follow.

    3. It enables some nice composition scenarios where a FilterActionAttribute can take the result of an action method and modify/transform it before executing it.  For example: a "Browse" action on a ProductCatalog controller might return an RenderActionResult that indicates it wants to render a "List" view of products.  A FilterActionAttribute declaratively set on the controller class could then have a chance to customize the specific "List" view template rendered to be either List-html.aspx or List-xml.aspx depending on the preferred MIME type of the client.  Multiple FilterActionAttributes can also optionally be chained together to flow the results from one to another.

    4. It provides a nice extensibility mechanism for people (including ourselves) to add additional features in the future.  New ActionResult types can be easily created by sub-classing the ActionResult base class and overriding the "ExecuteResult" method.  It would be easy to create a "RenderFile()" helper method, for example, that a developer writing an action could call to return a new "FileActionResult" object.

    5. It will enable some nice Asynchronous execution scenarios in the future.  Action methods will be able to return an AsyncActionResult object which indicates that they are waiting on a network operation and want to yield back the worker thread so that ASP.NET can use it to execute another request until the network call completes.  This will enable developers to avoid blocking threads on a server, and support very efficient and scalable code.

    One of the goals with this interim preview is to give people a chance to play around with this new approach and do real-world app-building and learning with it.

    We will also post an alternative Controller base class sample that you can use if you still prefer the previous "void" action return approach.  We deliberately didn't include this alternative Controller base class in this source refresh drop, though, because we want to encourage folks to give the "ActionResult" return approach a try and send us their app-building feedback on it.

    How To Unit Test Controller Action Methods

    I mentioned above that the new ActionResult approach can make unit testing controllers much easier (and avoid the need to use mocking for common scenarios).  Let's walk through an example of this in action.

    Consider the simple NumberController class below:

    This Controller class has an "IsEvenNumber" action method that takes a number as a URL argument.  The IsEvenNumber action method first checks whether the number is negative - in which case it redirects the user to an error page.  If it is a positive number it determines whether the number is even or odd, and renders a view template that displays an appropriate message:

    Writing unit tests for our "IsEvenNumber" action method is pretty easy thanks to the new ActionResult approach.

    Below is an example unit test that verifies that the correct Http redirect occurs when a negative number is supplied (for example: /Number/IsEvenNumber/-1):

    Notice above how we did not need to mock any objects to test our action method.  Instead we simply instantiated the NumberController class and called the action method directly (passing in a negative number) and assigned the return value to a local "result" variable.  I used the C# "as type" syntax above to cast the "result" variable as a strongly typed "HttpRedirectResult" type.

    What is nice about the C# "as" keyword is that it will assign the value as null instead of throwing an exception if the cast fails (for example: if the action method returned a RenderViewResult instead).  This means I can easily add an assertion check in my test to verify that the result is not null in order to verify that an Http redirect happened.  I can then add a second assertion check to verify that the correct redirect URL was specified.

    Testing the scenarios where non-zero numbers are passed in is also easy.  To do this we'll create two test methods - one testing even numbers and one testing odd numbers.  In both tests we'll assert that a RenderViewResult was returned, and then verify that the correct "Message" string was passed within the ViewData associated with the view:

    We can then right click on our NumberControllerTest class inside VS 2008 and choose the "Run Tests" menu item:

    This will execute our three unit tests in-memory (no web-server required) and report back on whether our NumberController.IsEvenNumber() action method is performing the right behavior:

    Note: with this week's source drop you still need to use mocking to test the TempData property on Controllers.  Our plan is to not require mocking to test this with the ASP.NET MVC Preview 3 drop in a few weeks.

    MapRoute Helper Method

    URL routing rules within ASP.NET MVC applications are typically declared within the "RegisterRoutes" method of the Global.asax class.

    With ASP.NET MVC Previews 1 and 2 routes were added to the routes collection by instantiating a Route object directly, wiring it up to a MvcRouteHandler class, and then by setting the appropriate properties on it to declare the route rules:

    The above code will continue to work going forward.  However, you can also now take advantage of the new "MapRoute" helper method which provides a much simpler syntax to-do the same thing.  Below is the convention-based URL route configured by default when you create a new ASP.NET MVC project (which replaces the code above):

    The MapRoute() helper method is overloaded and takes two, three or four parameters (route name, URL syntax, URL parameter default, and URL parameter regular expression constraints). 

    You can call MapRoute() as many times as you want to register multiple named routes in the system.  For example, in addition to the default convention rule, we could add a "Products-Browse" named routing rule like below:

    We can then refer to this "Products-Browse" rule explicitly within our Controllers and Views when we want to generate a URL to it.  For example, we could use the Html.RouteLink view helper to indicate that we want to link to our "Products-Browse" route and pass it a "Food" category parameter using code in our view template like below:

    This view helper would then access the routing system and output an appropriate HTML hyperlink URL like below (note: how it did automatic parameter substitution of the category parameter into the URL using the route rule):

    Note: with this week's source drop you need to pass-in the controller and action parameters (in addition to the Category param) to the Html.RouteLink() helper to resolve the correct route URL to generate.  The ASP.NET MVC Preview 3 drop in a few weeks will not require this, and allow you to use the Html.RouteLink call exactly as I've written it above to resolve the route.

    Other URL Route Mapping Features

    This week's MVC source drop also supports a bunch of new URL route mapping features.  You can now include "-", ".", ";" or any other characters you want as part of your route rules.

    For example, using a "-" separator you can now parse the language and locale values from your URLs separately using a rule like below:

    This would pass appropriate "language", "locale", and "category" parameters to the ProductsController.Browse action method when invoked:

    URL Route Rule Example URL Parameters Passed to Action method
    {language}-{locale}/products/browse/{category} /en-us/products/browse/food language=en, locale=us, category=food
      /en-uk/products/browse/food language=en, locale=uk, category=food

    Or you can use the "." file extension type at the end of a URL to determine whether to render back the result in either a XML or HTML format:

    This would pass both "category" and a "format" parameters to the ProductsController.Browse action method when invoked:

    URL Route Rule Example URL Parameters Passed to Action method
    products/browse/{category}.{format} /products/browse/food.xml category=food, format=xml
      /products/browse/food.html category=food, format=html

    ASP.NET MVC Preview 2 introduced wildcard route rules.  For example, you can indicate in a rule to pass all remaining URI content on as a named parameter to an action method:

    This would pass a "contentUrl" parameter to the WikiController.DisplayPage action method when invoked:

    URL Route Rule Example URL Parameters Passed to Action method
    Wiki/Pages/{*contentUrl} /Wiki/Pages/People/Scott contentUrl="People/Scott"
      /Wiki/Pages/Countries/UK contentUrl="Countries/UK"

    These wildcard routes continue to work fine with this week's preview - and are very useful to look at if you are building a blogging, wiki, cms or other content based system.

    Note that in addition to using the new routing system for ASP.NET MVC scenarios, we are also now using the same routing system within ASP.NET Dynamic Data (which uses ASP.NET Web Forms).

    Summary

    Hopefully the above post provides a quick update on some of the new features and changes exposed with this week's ASP.NET MVC source update drop. 

    You can download it here if you want to start using it immediately.  Alternatively, you can wait a few weeks for the official ASP.NET MVC Preview 3 drop - which will have some more features (and incorporate feedback people provide on this week's drop), deliver a more seamless installer, provide nice VS integration, and deliver up to date documentation.

    For any questions/issues with this week's drop of ASP.NET MVC, make sure to also check out the ASP.NET MVC forum on www.asp.net.

    Hope this helps,

    Scott

  • April's Toolbox Column Online

    My Toolbox column in the April 2008 issue of MSDN Magazine is avaiable online. The April issue examines:

    • Techniques for logging Web application errors. Provides an overview of two technologies for automatically logging web application exceptions: Microsoft's Health Monitoring system and Atif Aziz's open-source ELMAH library.
    • Blogs of Note - Coding Horror, the interesting and popular blog written by (now) full-time blogger Jeff Atwood.
    • The Bookshelf - Pro LINQ, by Joseph C. Rattz, Jr.

    Enjoy! - http://msdn2.microsoft.com/en-us/magazine/cc500592.aspx

    As always, if you have any suggestions for products or books to review for the Toolbox column, please send them into toolsmm@microsoft.com.

  • April 11th Links: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight

    Here is the latest in my link-listing series.  Also check out my ASP.NET Tips, Tricks and Tutorials page and Silverlight Tutorials page for links to popular articles I've done myself in the past.

    ASP.NET

    • 50 Useful CSS Tips and Tricks: A useful page that provides a nice listing of various CSS tips, tricks and tools you can use for common web scenarios.

    ASP.NET AJAX

    • Accessible UpdatePanel: Bertrand Le Roy from the ASP.NET team has an article that describes how to make the ASP.NET AJAX's UpdatePanel control accessible for screen-readers.

    • Building ASP.NET AJAX Components: Mike Ormond has written an excellent 8-part series that covers building re-usable ASP.NET AJAX components that work on both the client and server.

    ASP.NET MVC

    • ASP.NET MVC: Membership Starter Kit: Troy Goode has a built an awesome membership starter kit for ASP.NET MVC that provides registration and login pages for users to authenticate on your site, as well as a set of administration functionality that allows admins to create/manage users and roles.  Download it here.

    • How to Enable Pretty URLs with ASP.NET MVC and IIS6: James Geurts posts a useful article that describes how to enable extension-less URLs with ASP.NET MVC on IIS6 (note: you do not need to configure anything special with ASP.NET MVC on IIS7 to enable extension-less URL support).

    Visual Studio

    Silverlight

    • Dave Campbell's Excellent Silverlight Link Series: Dave Campbell posts a regular series of links to new Silverlight articles and content on the web.  I highly recommend subscribing to his blog if you want to stay current with all the latest Silverlight articles and posts.

    • Silverlight 2 Map / DataGrid Tutorial: Jason Zander has a great two-part Silverlight tutorial that demonstrates how to build a nice data-driven application that integrates a map control with a datagrid to filter and analyze data.

    Hope this helps,

    Scott

  • ASP.NET Dynamic Data Preview Available

    A few months ago we released an ASP.NET 3.5 Extensions Preview that contained a bunch of new features that will be shipping later this year (including ASP.NET AJAX Improvements, ASP.NET MVC, ASP.NET Silverlight Support, and ASP.NET Dynamic Data).

    The ASP.NET Dynamic Data support within that preview provided a first look at a cool new feature that enables you to quickly build data driven web-sites that work against a LINQ to SQL or LINQ to Entities object model.  ASP.NET Dynamic Data allows you to automatically render fully functional data entry and reporting pages that are dynamically constructed from your ORM data model meta-data.  In addition to supporting a dynamic rendering mode, it also allows you to optionally override and customize any of the view templates using any HTML or code you want (given you full control of the experience).

    ASP.NET Dynamic Data Preview

    Today we released an updated ASP.NET Dynamic Data Preview.  You can learn more about it and download it here.

    This new dynamic data preview now works with the standard built-in ASP.NET data controls (GridView, ListView, FormView, DetailsView, etc).  The dynamic data support enables these controls to automatically handle foreign-key relationships.  For example, on a gridview you'll now get automatic friendly name display of foreign key column values and automatic drop-down list selection support of these values when in edit mode:

    The new dynamic data support also provides automatic UI validation support (both client-side and server-side) based on the constraints you set on your data model classes.  For example, if a column in the database is limited to 50 characters in size, and is marked as non-nullable, appropriate UI control validators will automatically be applied by ASP.NET dynamic data to enforce this constraint in the UI pages as well.  If you change the constraints within your LINQ to SQL or LINQ to Entities data model classes, the UI will automatically pick up these changes and enforce the new constraints on the next web request.

    In addition to standard data model metadata, you can also declare custom metadata to further control validation and the default display of UI of objects. 

    You will be able to use all of the above features with both LINQ to SQL and LINQ to Entities.

    Visual Studio Dynamic Data Project Wizard

    In addition to the core ASP.NET dynamic data runtime support, the VS web tools team today also shipped a first preview of a new dynamic data project wizard that enables you to quickly get a data driven web-site started.  The wizard allows you to select a database, and then the tables, views and sprocs within it that you want to build a LINQ to SQL data model around:

    After creating a data model, the wizard allows you to easily choose dynamic data driven template pages to build UI around it:

    You can then choose what type of inserting/editing/updating UI is supported on each page:

    And when you click finish it will setup a project with your data model classes and data UI pages setup to run.  You can learn more about the wizard and watch it in action in a blog post and screencast here.

    How to Get Started

    You can learn more about this new dynamic data preview and download and run it locally here.

    You can watch David Ebbo's dynamic data presentation at MIX 08 to learn more about how it works.  Also check out Scott Hunter's screen-cast here, and Brad Millington's screen cast here.

    You can ask questions and submit feedback via the www.asp.net forums here.

    Hope this helps,

    Scott

  • Following Principles

    A dictionary definition of principle often uses the word "law", but principles in software development still require judgment. Sometimes the judgment requires some technical knowledge, like knowing the strengths and weaknesses of a particular technology. Other times the judgment requires some business knowledge, like the ability to anticipate where change is likely to occur.

    Asking someone to make a sensible judgment about a principle is difficult when all you see is a snippet of code in a blog. The code is outside of its context. Take Leroy's BankAccount class. We don't really know what sort of business Leroy works for, or even what type of software Leroy is building. Nevertheless, let's apply a few principles to see what's bothering Leroy.

    Remaining Single

    Does Leroy's original BankAccount class violate the Single Responsibility Principle? I think so. The class is opening text files for logging, calculating interest, and oh, by the way, it needs to provide all the state and behavior for a financial account, too. Even without knowing the context, it seems reasonable to remove the auditing cruft into a separate class. After writing some tests, and implementing a concrete auditing class, Leroy's BankAccount might look like the following.

    public class BankAccount
    {        
        
    public void Deposit(decimal amount)
        {
            Balance += amount;
            _log.Write(
    "Deposited {0} on {1}", amount, DateTime.Now);
        }

      
    // ...
      
      
    AuditLog _log = new AuditLog();

    }

    Leroy has an almost infinite number of choices to make before coming up with the above implementation, though. Leroy could have derived BankAccount from an Auditable base class, or forced BankAccount to implement an IAuditable interface. But what guides Leroy to this particular solution in the universe of a million possibilities are other principles - like the Interface Segregation Principle, and Composition Over Inheritance.

    An Addiction to Auditing

    Leroy might still frown at his class, feeling he has violated the Dependency Inversion Principle. Without any additional information, we have to trust Leroy's judgment when he decides to make some additional changes.

    public class BankAccount
    {        
        
    public BankAccount (IAuditLog auditLog)
        {
            _log = auditLog;
        }

        
    public void Deposit(decimal amount)
        {
            Balance += amount;
            _log.Write(
    "Deposited {0} on {1}", amount, DateTime.Now);
        }

      
    // ...
      
      
    IAuditLog _log;
    }

    Perhaps Leroy already knew about some future changes in his auditing implementation, or perhaps Leroy just wanted to make his class more testable. Some of us view software as a massive heap of dependencies, and we fight to reduce the brittleness created by dependencies using inversion of control and dependency injection techniques. In some environments, this isn't needed. The principles to apply depend on the language you use, the tools you use, and ultimately depend on the problem the software is trying to solve.

    Is There Still Something Wrong With This Code?

    WWWTC has had a run of 19 episodes. I have some material for at least another 20. Problem is, most of the material deals with API trivia and edge cases you might never see. Interesting? To me, at least, but I'm thinking of introducing more squishy design type entries. I know a lot of people struggle to apply the latest frameworks and libraries, but design questions are always enlightening and produce the most spirited debate, giving us all something we can learn from.

  • Tip/Trick: Creating and Using Silverlight and WPF User Controls

    One of the fundamental design goals of Silverlight and WPF is to enable developers to be able to easily encapsulate UI functionality into re-usable controls.

    You can implement new custom controls by deriving a class from one of the existing Control classes (either a Control base class or from a control like TextBox, Button, etc).  Alternatively you can create re-usable User Controls - which make it easy to use a XAML markup file to compose a control's UI (and which makes them super easy to build).

    In Part 6 of my Digg.com tutorial blog series I showed how to create a new user control using VS 2008's "Add New Item" project item dialog and by then defining UI within it.  This approach works great when you know up front that you want to encapsulate UI in a user control.  You can also use the same technique with Expression Blend.

    Taking Existing UI and Encapsulating it as a User Control

    Sometimes you don't always know you want to encapsulate some UI functionality as a re-usable user control until after you've already started defining it on a parent page or control.

    For example, we might be working on a form where we want to enable a user to enter shipping and billing information.  We might begin by creating some UI to encapsulate the address information.  To-do this we could add a <border> control to the page, nest a grid layout panel inside it (with 2 columns and 4 rows), and then place labels and textbox controls within it:

    After carefully laying it all out, we might realize "hey - we are going to use the exact same UI for the billing address as well, maybe we should create a re-usable address user control so that we can avoid repeating ourselves". 

    We could use the "add new item" project template approach to create a blank new user control and then copy/paste the above UI contents into it. 

    An even faster trick that we can use within Blend, though, is to just select the controls we want to encapsulate as a user control in the designer, and then "right click" and choose the "Make Control" menu option:

    When we select the "Make Control" menu item, Blend will prompt us for the name of a new user control to create:

    We'll name it "AddressUserControl" and hit ok. This will cause Blend to create a new user control that contains the content we selected:

    When we do a re-build of the project and go back to the original page, we'll see the same UI as before - except that the address UI is now encapsulated inside the AddressUserControl:

    We could name this first AddressUserControl "ShippingAddress" and then add a second instance of the user control to the page to record the billing address (we'll name this second control instance "BillingAddress"):

    And now if we want to change the look of our addresses, we can do it in a single place and have it apply for both the shipping and billing information.

    Data Binding Address Objects to our AddressUserControl

    Now that we have some user controls that encapsulate our Address UI, let's create an Address data model class that we can use to bind them against.  We'll define the class like below (taking advantage of the new automatic properties language feature):

    Within the code-behind file of our Page.xaml file we can then instantiate two instances of our Address object - one for the shipping address and one for the billing address (for the purposes of this sample we'll populate them with dummy data).  We'll then programmatically bind the Address objects to our AddressUserControls on the page.  We'll do that by setting the "DataContext" property on each user control to the appropriate shipping or billing address data model instance:

    Our last step will be to declaratively add {Binding} statements within our AddressUserControl.xaml file that will setup two-way databinding relationships between the "Text" properties of the TextBox controls within the user control and the properties on the Address data model object that we attached to the user control:

    When we press F5 to run the application we'll now get automatic data-binding of the Address data model objects with our AddressUserControls:

    Because we setup the {Binding} declarations to be "Mode=TwoWay", changes users make in the textboxes will automatically get pushed back to the Address data model objects (no code required for this to happen). 

    For example, we could change our original shipping address in the browser to instead go to Disneyland:

    If we wire-up a debugger breakpoint on the "Click" event handler of the "Save" button (and then click the button), we can see how the above TextBox changes are automatically reflected in our "_shippingAddress" data model object:

    We could then implement the SaveBtn_Click event handler to persist the Shipping and Billing Address data model objects however we want - without ever having to manually retrieve or manipulate anything in the UI controls on the page.

    This clean view/model separation that WPF and Silverlight supports makes it easy to later change the UI of the address user controls without having to update any of our code in the page.  It also makes it possible to more easily unit test the functionality (read my last post to learn more about Silverlight Unit Testing).

    Summary

    WPF and Silverlight make it easy to encapsulate UI functionality within controls, and the user control mechanism they support provides a really easy way to take advantage of this.  Combining user controls with binding enables some nice view/model separation scenarios that allow you to write very clean code when working with data.

    You can download a completed version of the above sample here if you want to run it on your own machine. 

    To learn even more about Silverlight and WPF, check out my Silverlight Tutorials and Links Page.  I also highly recommend Karen Corby's excellent MIX08 talk (which covers User Controls, Custom Controls, Styling, Control Templates and more), which you can watch online for free here.

    Hope this helps,

    Scott

  • Lounge Updates

    I took over The Lounge back in December and it has been growing quickly since then. One of the key ideas of the Lounge is that we only run ads for respectable companies and quality products, I am thrilled that we have found a number of new advertisers in the last month that really illustrate this idea. Applied Innovations and SlickEdit have been advertising with The Lounge since I took it over and just in the last month RedGate has started a campaign for their ANTS Profiler, TechSmith is running a campaign for SnagIt, and Typemock started a campaign for Typemock Isolator.

    I am also happy to announce that we have added a number of new publishers to The Lounge starting in April.

    In the .NET room we have added devlicio.us, an excellent community style blog site similar to codebetter.com.

    In the small publishers room we have added 3 great new publishers:

    Dave Donaldson is a good friend of mine and great developer. I am excited to have his blog in The Lounge.

    Simone Chiaretta is one of the developers of SubText and a great blogger, I think I have the entire SubText team on The Lounge now.

    Joel Ross is another great developer who I have gotten to know much better through twitter. I also have a chance of finishing in the top 3 of his bracket group if UNC makes it all the way.

    If you have a blog that you think would be a good fit for The Lounge or would like to advertise in one of our rooms please drop me an email.

    I am also working on a re-design of the site as well as a couple new rooms which I will hopefully launch in the next month.

    -James

  • Final Three Security Tutorials Published

    As noted in previous blog entries, I've been working on some tutorials for the www.asp.net site on the topics of forms authentication, authorization, membership, and roles. The first set of tutorials covered security basics and examined forms authentication in detail; the second set looked at the Membership system and the SqlMembershipProvider. The third set of tutorials examined the Roles framework and the SqlRoleProvider. The final slate of tutorials includes three on implementing common administrative tasks.

    • Building an Interface to Select One User Account from Many [VB | C#] - when we needed to select a user account in previous tutorials we used a drop-down list. While a drop-down list is sufficient if there are only a handful of user accounts, it  becomes unusable when there are hundreds or thousands of users. This tutorial looks at building an interface to efficiently and easily select one user account from a large number of accounts. 
    • Recovering and Changing Passwords [VB | C#] - shows how to use the PasswordRecovery and ChangePassword controls to allow users and administrators to recover lost passwords and change existing passwords.
    • Unlocking and Approving User Accounts [VB | C#] - as a security measure, if a user supplies an incorrect password too many times, they are locked out. Users may also be unapproved. In either case, they cannot login. This tutorial examines how these properties are set, h