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.

November 2008 - Posts

  • New ASP.NET Charting Control: <asp:chart runat="server"/>

    Microsoft recently released a cool new ASP.NET server control - <asp:chart /> - that can be used for free with ASP.NET 3.5 to enable rich browser-based charting scenarios:

    Once installed the <asp:chart/> control shows up under the "Data" tab on the Toolbox, and can be easily declared on any ASP.NET page as a standard server control:

    <asp:chart /> supports a rich assortment of chart options - including pie, area, range, point, circular, accumulation, data distribution, ajax interactive, doughnut, and more.  You can statically declare chart data within the control declaration, or alternatively use data-binding to populate it dynamically.  At runtime the server control generates an image (for example a .PNG file) that is referenced from the client HTML of the page using a <img/> element output by the <asp:chart/> control.  The server control supports the ability to cache the chart image, as well as save it on disk for persistent scenarios.  It does not require any other server software to be installed, and will work with any standard ASP.NET page.

    To get a sense of how to use the <asp:chart /> control I recommend downloading the Microsoft Chart Controls Sample Project.  This includes over 200 ASP.NET sample pages that you can run locally.  Just open the web project in VS 2008 and hit run to see them in action - you can then open the .aspx source of each to see how they are implemented.

    The below example (under Chart Types->Line Charts->3D Line and Curve Charts) demonstrates how to perform Line, Spline and StepLine charting:

    The below example (under Chart Types->Pie and Doughnut Charts) demonstrates a variety of pie and 3D doughnut options:

    The below example (under Chart Types->Advanced Financial Charts) demonstrates some graph charts:

    In addition to the above samples, you can download the Microsoft Chart Control Documentation or ask questions on the Chart Controls Forum to learn more.

    This should provide a useful (and free) addition to your standard ASP.NET toolkit of functionality, and enable you to easily add richer visualization and data workflow scenarios to your ASP.NET applications.

    Hope this helps,

    Scott

  • Visual Studio Keyboard Shortcut Tip: Ctrl+-

    I learned this Visual Studio keyboard shortcut from a client a couple of months ago. I use it several times a day now.

    Typing the Ctrl key and - (the minus key) returns you to the previous cursor location, be it in the same file or in a different, open file.

    Visual Studio maintains your position in a file as a stack. So as you do a Find and jump to a particular location in a file, or right-click on a method or property name and choose “Go To Definition” Visual Studio keeps track of your position prior to these jumps. You can then 'pop' the position off the stack and return to it by hitting Ctrl+-.

    This keyboard shortcut is quite useful when you're bug hunting or trying to understand how some bit of code is working and are drilling down into methods and search results. When you are ready to return to the previous location, which could be in another file or elsewhere in the currently opened file, just hit Ctrl+- to get back! And if you need to get back to the spot before that, hit Ctrl+- again.

  • jQuery Intellisense in VS 2008

    Last month I blogged about how Microsoft is extending support for jQuery.  Over the last few weeks we've been working with the jQuery team to add great jQuery intellisense support within Visual Studio 2008 and Visual Web Developer 2008 Express (which is free).  This is now available to download and use.

    Steps to Enable jQuery Intellisense in VS 2008

    To enable intellisense completion for jQuery within VS you'll want to follow three steps:

    Step 1: Install VS 2008 SP1

    VS 2008 SP1 adds richer JavaScript intellisense support to Visual Studio, and adds code completion support for a broad range of JavaScript libraries.

    You can download VS 2008 SP1 and Visual Web Developer 2008 Express SP1 here.

    Step 2: Install VS 2008 Patch KB958502 to Support "-vsdoc.js" Intellisense Files

    Two weeks ago we shipped a patch that you can apply to VS 2008 SP1 and VWD 2008 Express SP1 that causes Visual Studio to check for the presence of an optional "-vsdoc.js" file when a JavaScript library is referenced, and if present to use this to drive the JavaScript intellisense engine.

    These annotated "-vsdoc.js" files can include XML comments that provide help documentation for JavaScript methods, as well as additional code intellisense hints for dynamic JavaScript signatures that cannot automatically be inferred.  You can learn more about this patch here.  You can download it for free here.

    Step 3: Download the jQuery-vsdoc.js file

    We've worked with the jQuery team to put together a jQuery-vsdoc.js file that provides help comments and support for JavaScript intellisense on chained jQuery selector methods.  You can download both jQuery and the jQuery-vsdoc file from the official download page on the jQuery.com site:

    Save the jquery-vsdoc.js file next to your jquery.js file in your project (and make sure its naming prefix matches the jquery file name):

    You can then reference the standard jquery file with an html <script/> element like so:

    Or alternatively reference it using the <asp:scriptmanager/> control, or by adding a /// <reference/> comment at the top of a standalone .js file. 

    When you do this VS will now look for a -vsdoc.js file in the same directory as the script file you are referencing, and if found will use it for help and intellisense.  The annotated

    For example, we could use jQuery to make a JSON based get request, and get intellisense for the method (hanging off of $.):

    As well as help/intellisense for the $.getJSON() method's parameters:

     

    The intellisense will continue to work if you nest a callback function within the method call.  For example, we might want to iterate over each JSON object returned from the server:

    And for each of the items we could execute another nested callback function:

    We could use the each callback function to dynamically append a new image to a list (the image src attribute will point to the URL of the returned JSON media image):

    And on each dynamically created image we could wire-up a click event handler so that when it is pressed it will disappear via an animation:

    Notice how the jQuery intellisense works cleanly at each level of our code. 

    JavaScript Intellisense Tips and Tricks

    Jeff King from the Web Tools team wrote up a great post earlier this week that answers a number of common questions about how JavaScript intellisense works with VS 2008.  I highly recommend reading it.

    One trick he talks about which I'll show here is a technique you can use when you want to have JavaScript intellisense work within user-controls/partials (.ascx files).  Often you don't want to include a JavaScript library <script src=""/> reference  within these files, and instead have this live on the master page or content page the user control is used within.  The problem of course when you do this is that by default VS has no way of knowing that this script is available within the user control - and so won't provide intellisense of it for you.

    One way you can enable this is by adding the <script src=""/> element to your user control, but then surround it with a server-side <% if %> block that always evaluates to false at runtime:

    At runtime ASP.NET will never render this script tag (since it is wrapped in an if block that is always false).  However, VS will evaluate the <script/> tag and provide intellisense for it within the user-control.  A useful technique to use for scenarios like the user control one.  Jeff has even more details in his FAQ post as well as his original jQuery intellisense post.  Rick Strahl also has a good post about using jQuery intellisense here.

    More Information

    To learn more about jQuery, I recommend watching Stephen Walther's ASP.NET and jQuery PDC talk. Click here to download his code samples and powerpoint presentation.

    Rick Strahl also has a really nice Introduction to jQuery article that talks about using jQuery with ASP.NET.  Karl Seguin has two nice jQuery primer posts here and here that provide shorter overviews of some of the basics of how to use jQuery. 

    I also highly recommend the jQuery in Action book.

    Hope this helps,

    Scott

  • New Version of Error Logging Modules and Handlers (ELMAH) Available

    ELMAH is a free, open-source library created by Atif Aziz for logging errors that occur in an ASP.NET application. I've written about ELMAH many times before; its one of the first things I setup when creating a new ASP.NET application. A new version - ELMAH 1.0 BETA 3 - was recently released.

    With just a few minutes of setup and configuration you can have ELMAH automatically log unhandled exceptions to a number of different data stores - SQL Server, Oracle, a Microsoft Access database, an XML file, to an e-mail address, and so on. You can also write code to proactively record an error via the ELMAH library. Granted, ASP.NET provides some built-in support for logging errors via its health monitoring system, but ELMAH is a simpler version of the health monitoring system that focuses specifically on error logging and is easier to configure. It also works in ASP.NET 1.x applications, whereas health monitoring is only available in ASP.NET 2.0 and up. What's more, ELMAH provides a built-in web page and RSS support for viewing error information.

    So, what's new in ELMAH 1.0 BETA 3? The project page and issue tracker has the full set of details, but in a nutshell here are the enhancements that most interest me:

    • Three new error logging sources: Oracle, Microsoft Access, and VistaDB.
    • Log errors in AJAX applications.
    • Addition of an ErrorLogDataSourceAdapter, which you can use with the ObjectDataSource to declaratively work with the error log details from an ASP.NET page.

    It is imperative that every web application in production log errors and report those errors to the development team. ELMAH offers this important functionality and can be setup, configured, and customized within a few minutes.

  • Spike Code and Source Control

    Jeremy’s post “Don’t Check In Spike Code” reminds of something I’ve advocated for years: dedicate a place in your source control repository where each developer can check in their “experimental” code. The rule of thumb is to throw away code you write for a spike, but all code can be valuable, even if it isn’t production worthy.

    Here are the kinds of things I’ve checked into “experiments/sallen” over the years:

    • Code to isolate and reproduce compiler, framework, and library bugs.
    • Code written to learn a new technology, platform, or library.
    • Code that I almost threw away because I was sure it would only be needed once (like a one time data import).
    • UI mockups
    • Code written to evaluate a product.
    • Code that doesn’t work (because you’ll have proof 6 months later when someone else has the same idea).

    I’ve found that keeping a dedicated area for these types of check-ins offers some advantages:

    • It keeps spike code out of production branches (as Jeremy suggests).
    • It keeps code around that you might find useful to refer to one year from now.
    • After someone leaves a project, their experiments live on.
    • It saves you from saying “I think I tried something like that last year but threw the code away”.

    If nothing else, you can look back at what you wrote when you were spiking on a new technology and laugh in that “oh man, we really didn’t know what we were doing ” way.

  • Running the Same Query Against Multiple Databases

    One of my clients has a data-driven Software as a Service (SaaS) web application that is used by a dozen different customers. In particular, this client of mine has a web application that's used by various clinics and hospitals to track, schedule, and bill health care-related activities. As discussed in the Multi-Tenant Data Architecture guide from Microsoft, there are three approaches for modeling data that comes from many distinct customers:

    • Separate Databases - each customer has a separate database on the database server.
    • Shared Database, Separate Schema - a single databsae is used, but each customer has a unique set of database tables.
    • Shared Database, Shared Schema - a single set of database tables are used, with a CustomerId column or some other technique used to identify what rows belong to what customers.

    Each approach has its pros and cons, and is good reading if you plan on building a SaaS web app.

    For security and privacy reasons (important concerns for health care providers), we used the Separate Databases approach - each customer's data is hosted as a separate database on the server. I would not recommend this approach for the vast majority of multi-tenant applications. The most pronounced drawback of this model centers around the pain points associated with managing and administration of these databases. For example, when rolling out a new version or a bug fix, the database changes must be applied to all customer databases. If there is a business logic-type bug discovered, where there may be logically incorrect data in the database that needs to be addressed, you now have to look through all databases to see where the problem occurs and apply the fix across all databases.

    One tool that assists with such debugging challenges is the undocumented Microsoft SQL Server stored procedure sp_MsForEachDb. In short, this stored procedure allows you to run a command on every database on the server. You can use it like so:

    EXEC sp_MsForEachDb @command1='command to execute on all databases'

    Within the @command1 input you can use a question mark (?) to denote that the name of the database the command is currently executing on be injected.

    sp_MsForEachDb is most commonly used for administrative tasks such as updating statistics on all databases, but can also be used for debugging or bug hunting in a multi-tenant SaaS architecture that uses separate databases. Here's how: consider that you unearth a bug in the application code that allows for some illogical value in the database. Perhaps there's some business rule that dictates that a patient cannot have their insurance information entered if the patient's primary address is not provided, yet the application did not correctly enforce this rule and as a result there may be some patients with insurance information on file but without a primary address. Your task is to find out what patients in each database fit this description. You could run the following query on every single database:

    SELECT p.PatientId, p.PatientName
    FROM Patients p
       INNER JOIN PatientInsuranceInformation pi ON p.PatientId = pi.PatientId
    WHERE p.Address1 IS NULL AND pi.Active = 1

    The issue is that this quickly becomes a pain if you have many different databases. Here's where sp_MsForEachDb comes in - you can use it to run the above on each database with the following statement:

    EXEC sp_MsForEachDb @command1 = 'SELECT ''?'' as DatabaseName, p.PatientId, p.PatientName
        FROM ?.dbo.Patients p
           INNER JOIN ?.dbo.PatientInsuranceInformation pi ON p.PatientId = pi.PatientId
        WHERE p.Address1 IS NULL AND pi.Active = 1'

    This will output a three column resultset that displays those patients that have insurance information but no primary address along with what database the patient information resides in. The ''?'' part in the SELECT list displays the database name. The question mark character is also used in the FROM and INNER JOIN clauses to indicate the database from which to query against (i.e., you can refer to a table as database.owner.tableName).

    The only downside of sp_MsForEachDb is that it runs on all databases, meaning it will run on databases like master, which don't have the application tables. As a result, you'll get SQL errors for those databases which clutter the output a bit. But for the databases specific to the application you will quickly see the results of your query and know, in an instant, what patients in what databases have this particular problem.

  • Update on Silverlight 2 - and a glimpse of Silverlight 3

    We shipped Silverlight 2 last month.  Over the last 4 weeks, the final release of Silverlight 2 has been downloaded and installed on more than 100 million consumer machines.  It has also recently been published to corporate administrators via the Microsoft SMS and Microsoft Update programs to enable them to automatically deploy across enterprises.  Over 1 in 4 computers on the Internet now have some version of Silverlight installed.

    Silverlight 2 was a major release, and delivered an impressive set of cross-browser, cross-platform functionality for Media and Rich Internet Application experiences.  It has been great watching new sites launch using it.

    Media Experiences

    Silverlight 2 enables the highest quality video on the web, and delivers it with the lowest TCO of any media platform.

    One of the capabilities built-into Silverlight 2 is its support for "adaptive streaming" - which enables video to be delivered at multiple bitrates (for example: 400Kbits, 800Kbits, 1.5Mbits, 2Mbits) with Silverlight dynamically choosing the optimal bitrate to use depending on the network bandwidth and CPU capability of the client (it can also automatically switch bitrates seamlessly if conditions change later). 

    Silverlight's adaptive streaming support is extensible.  Move Networks (who helped pioneer the concept of adaptive streaming) have already integrated their adaptive streaming solution with Silverlight.  Silverlight 2 and Move were used to stream the Democratic National Convention live on the web this summer. 

    Last month we announced that Microsoft will be adding adaptive streaming support as a free feature of our IIS7 web-server.  IIS Smooth Streaming will provide an integrated way to deliver HD quality adaptive video over the web. Visit Akamai's www.smoothhd.com site to see some awesome examples of Silverlight 2 and IIS Smooth Streaming in action (with adaptive streaming up to 2.5Mbits).

    The NBC Olympics site used Silverlight 2 to serve more than 3,500 hours of live and on-demand Olympic coverage to over 60 million unique visitors this summer.  Visitors to the site watched an average of 27 minutes of video - which is stunningly high for online video.  The site used the new Silverlight adaptive streaming capability to support 1.5Mbit bitrates - which helped deliver an awesome video experience:

    In addition to powering the Olympics experience in the US, Silverlight was also used in France (by FranceTV), the Netherlands (by NOS), Russia (by Sportbox.ru) and Italy (by RAI).  In addition to video quality, a big reason behind these broadcasters decision to use Silverlight was the TCO and streaming cost difference Silverlight provided.  In the August 2008 edition of Web Designer Magazine (a Dutch publication) a NOS representative reported that they were able to serve 100,000 concurrent users using Silverlight and 40 Windows Media Servers, whereas it would have required 270 servers if they had used Flash Media Servers.

    Over the last month we've seen several major new deployments of Silverlight for media scenarios.  For example: CBS College Sports is now using Silverlight to stream NCAA events from its 170 partner colleges and university.  Blockbuster is replacing Flash with Silverlight for its MovieLink application. And Netflix two weeks ago rolled out its new Instant Watch service using Silverlight. 

    Rich Internet Applications (RIA) Experiences

    Silverlight 2 delivers a cross-browser, cross-platform subset of the .NET Framework, and enables developers to build Rich Internet Applications. 

    Developers can use either VS 2008 or the free Visual Web Developer 2008 Express to open and edit Silverlight 2 projects, and get a powerful code-focused .NET development environment.  Designers can use Expression Blend 2 SP1 to open and edit the same projects and use a creative tool to sculpt and create rich user experiences.  I recently blogged about the nice developer/designer workflow this enables here.  Two weeks ago at the PDC we also shipped the first release of our Silverlight Toolkit - an open source project which adds additional runtime controls and components for Silverlight 2 development (including new charting controls). 

    A number of customers have already launched Internet-facing Silverlight 2 RIA solutions (including Renault, Hard Rock and Toyota). For example, last month AOL launched their new AOL Mail RIA using Silverlight 2:

    Silverlight 2 is also now being used in a variety of enterprise solutions.  For example, K2 recently launched their new Blackpoint workflow management solution for Microsoft SharePoint using Silverlight:

    Microsoft is also deploying new Silverlight based RIA experiences.  The Windows Live Team's new photo application (photos.live.com) and video communications application (videomessages.live.com) are both built with Silverlight 2, as is the new MSN Toolbar (it uses Silverlight to customize the browser frame).  Last month at the PDC we also gave a first sneak-peak demo of some of the new Office 14 Web Companion RIA applications which use Silverlight.

    Silverlight 3

    Next year we will ship our next major Silverlight release -- Silverlight 3. 

    Silverlight 3 will include major media enhancements (including H.264 video support), major graphics improvements (including 3D support and GPU hardware acceleration), as well as major application development improvements (including richer data-binding support and additional controls).  Note these are just a small sampling of the improvements - we have plenty of additional cool features we are going to keep up our sleeves a little longer. ;-)

    Next year Visual Studio and Visual Web Developer Express will also support a fully editable and interactive designer for Silverlight, and add tool support for data-binding:

    We are pretty excited about where Silverlight is today, as well as the roadmap in place over the next year.  It has been really great to watch customers go live with cool solutions.  The next year is going to be a fun one as more and more sites launch with Silverlight 2, and as even bigger scenarios are enabled with Silverlight 3 and beyond. :-)

    Hope this helps,

    Scott

  • Single Letter Variable Names Still Considered Harmful

    There is a lot of humor in the Bad Variable Names entry on the c2 wiki. I like this confession from Alex:

    The worst of which was my counter variable names. I now use i, j, k, and so on for local counts and things like activeRowCount for the more descriptive names. Before, in the early years mind you, it shames me to say, I would name my counters things like Dracula, Chocula*, MonteChristo. They are all counts after all. I apologize for my intial variable naming conventions and shall go beat my face now as punishment.

    (*If you are not familiar with Count Chocula, he’s the mascot of a chocolate flavored breakfast cereal of the same name. The cereal is popular in America, as is any corn based cereal that mixes chocolate and sugar.)

    One letter variable names are considered bad, unless you need a loop counter (i), a spatial coordinate ( x,y,z) …

    … or you’re writing a lambda expression:

    var query = employees.Where(e => e.Name == "Tom");

    This isn’t just C# – other languages also encourage short variable names in nested function definitions. The short names have been bothering me recently, particularly when the chained operators begin to work on different types.

    var query = employees.GroupBy(e => e.Name)
                         .Where(g => g.Count() > 2)
                         .Select(g => g.Key);
    

    LINQ joins are particularly unreadable. There is a piece of code I wrote in a presenter class many months ago, and every time I see the code I wince.

    var controlsToEnable =
            results.SelectMany(r => r.RequiredFields)                
                   .Join(mapper.Entries,
                           ps => ps.PropertyName,
                           e => e.Left.PropertyName,
                           (ps, e) => e.Right)
                   .Select(ps => ps.GetValue<IAnswerControl>(_view));
    

    I’ve been thinking that one way to improve the readability is to use a custom Join operator. The types used in the query above are PropertySpecifier<T> and MapEntry<TLeft, TRight>. The idea is to join property specifiers against the left hand side of a mapping entry to retrieve the right hand side of the mapping entry (which is another property specifier). The extension method has a lot of generics noise.

    public staticIEnumerable<PropertySpecifier<TRight>> Join<TLeft, TRight>(
          thisIEnumerable<PropertySpecifier<TLeft>> specifiers,
        IEnumerable<MapEntry<TLeft, TRight>> entries)
    {
        return specifiers.Join(entries,
    propertySpecifer => propertySpecifer.PropertyName,
    mapEntry => mapEntry.Left.PropertyName,
    (propertySpecifier, mapEntry) => mapEntry.Right);
    }

    At least the custom Join cleans up the presenter class query, which I also think looks better with more descriptive variable names.

    var controlsToEnable =
            results.SelectMany(result => result.RequiredFields)
                   .Join(mapper.Entries)
                   .Select(property => property.GetValue<IAnswerControl>(_view));
    

    For the time being I'm going to avoid one letter variable names in all but the simplest lambda expressions.

  • Styling a Silverlight Twitter Application with Expression Blend 2

    Silverlight 2 provides a rich platform for building cross-browser/cross-platform RIA applications. 

    One of the things that makes Silverlight so powerful is the ease with which developers and designers can collaborate together on projects.  Developers can use Visual Studio to open and edit Silverlight 2 projects and get a powerful code-focused .NET development environment, and designers can use Expression Blend 2 SP1 to open and edit the exact same project and use a creative tool to sculpt and create optimal user experience designs.

    The WPF UI framework shipped in Silverlight further enables a great designer/developer workflow by supporting concepts like layout management, controls, styles, templates, and resources - which help avoid scenarios where designers and developers end up tripping over each other when integrating functionality, behavior and expressive design.

    Silverlight 2 Twitter Sample

    Last month I posted an in-depth blog tutorial on how to build a Silverlight 2 Digg application which you can read here.  This tutorial was aimed primarily at developers, and focused on introducing the fundamental programming concepts involved when building a Silverlight 2 application. 

    Today Celso Gomes and Peter Blois posted a cool 10 minute video tutorial that shows off using Expression Blend to stylize a Silverlight 2 Twitter Messenger application.  You can watch the video here.  You can download the source code for the completed Silverlight Twitter application here.

    The video does a nice job demonstrating how designers can re-style a Silverlight application without having to mess with the code behind it.  In the process it shows some of the power and capability that Expression Blend 2 provides to build really rich user experiences.  Celso starts with a developer version of the application, and then customizes and sculpts the UI to have a fun twitter character theme:

    The Application Model

    The Silverlight Twitter client is hosted within an ASP.NET server application that exposes a web service that enables the Silverlight Twitter application to communicate to the Twitter service (since Twitter does not allow direct access from client applications). Communication between the Silverlight client and the ASP.NET web server is done using Windows Communication Foundation (WCF).

    The client application uses a Model-View-Presenter (MVP) pattern (also known as the Model-View-ViewModel pattern) which is commonly used in large WPF applications. Even though this is a fairly simple application they wanted to take advantage of the flexibility that MVP allows and allow room for future growth. 

    Maintaining the separation between the visuals and the application logic also enables designers to make fairly complex visual changes without impacting the basic application flow.  The video goes through some examples of the styling flexibility this architecture facilitates.

    The Styling Process

    In the video, Celso highlights how Resources can help designers quickly change colors.  A common Brush Resource, for example, can be used to change the color of all the text elements in the application:

    Celso shows how easy is to create new User Controls from graphics using Expression Blend 2 SP1 (just select multiple elements in the designer, right-click, and choose the "Make Control" menu option):

    And also how to create new states inside this new User Control (using the Visual State Manager feature - which is also now supported with WPF), to animate the bird (fly, blink, etc...)

    Celso also shows how to create animations for each state, changing advanced properties like Key Spline curves, and Repeat Behavior:

    He also shows how to create custom buttons from drawings (which can come from XAML or any other design tool like Photoshop or Illustrator). All the states of a Button Control are available out of the box.

    Expression Blend also enables you to easily change complex controls like List Boxes. Designers have access to all Styles, Templates, and states - and can completely customize all the parts of a List Box without having to write any code:

    You can watch the video and download the code to check out the above Twitter application.

    To learn more about Expression Blend, I also recommend watching the Expression Blend: Tips and Tricks presentation from the PDC conference two weeks ago.

    Hope this helps,

    Scott

  • Mapping with Expressions

    Once you know about the magic of Expression<T>, it’s hard not to make use of it.

    Or perhaps, abuse it.

    Here is an excerpt of a class I wrote that uses Expression<T> as a reflection helper.

    public class PropertySpecifier<T>
    {
        public PropertySpecifier(
                    Expression<Func<T, object>> expression)
        {
            _expression = (expression.Body) as MemberExpression;
            if (_expression == null)
            {                
                // raise an error
            }
        }
    
        public string PropertyName
        {
            get
            {
                return _expression.Member.Name;
            }
        }
    
        // ...
    
        MemberExpression _expression;
    }
    

    I use the class to attach metadata to my flowchart shapes about which pieces of data they require via a RequiresField extension method.

    .WithShape("FibrinolyticTherapy")
        .RequiresField(pd => pd.FibrinolyticAdministration)

    The property specifier allows me to find, by name, the property the expression is touching, and also build dynamic getters and setters for the property. For my flowcharts,once each shape is associated with its required data, I can query the flowchart evaluation results to find out what pieces of information the flowchart requires the user needs to enter. This information can be used to selectively enable UI controls.

    var controlsToEnable =
            results.SelectMany(r => r.RequiredFields)                
                   .Join(mapper.Entries,
                            ps => ps.PropertyName,
                            e => e.Left.PropertyName,
                            (ps, e) => e.Right)
                   .Select(ps => ps.GetValue<IAnswerControl>(_view));
    

    Now, that particular piece of code is something I’m not too happy about, but more on that in a later post. This code is in a presenter class and joins the PropertySpecifier objects the flowchart requires with PropertySpecifer objects that reference a view class. The query essentially turns Expression<T> metadata into cold, hard Control references with no magic strings and strong typing all the way down. It’s also easy to write unit tests using some reflection to ensure all properties are mapped, and mapped correctly.

    All that is needed is a property map to store the associations between model data and view controls. This starts with a PropertyMapEntry class.

    public class PropertyMapEntry<TLeft, TRight>
    {
        public PropertySpecifier<TLeft> Left;
        public PropertySpecifier<TRight> Right;
    }
    

    And a PropertyMapper.

    public class PropertyMapper<TLeft, TRight>
    {
        public PropertyMapper()
        {
            Entries = new List<PropertyMapEntry<TLeft, TRight>>();
        }
    
        public List<PropertyMapEntry<TLeft, TRight>> Entries { get; set; }
    }
    

    And a fluent-ish API to build the map.

    static PropertyMapper<PnemoniaCaseData, IPnemoniaWorksheet> mapper =
        new PropertyMapper<PnemoniaCaseData, IPnemoniaWorksheet>()        
            .Property(cd => cd.ChestXRay.Length).MapsTo(v => v.ChestXRay)
            .Property(cd => cd.ClinicalTrial).MapsTo(v => v.ClinicalTrial)
            // ...
            ;
    

    I’m pretty happy with how it worked out – at least it looks like this will produce v 1.0 of shipping product. Yet I still wonder if I’m using Expression<T> for evil or for good. What do you think?

  • The Eight Commandments of Source Code Control

    Feel free to print out a copy of these commandments and tape them to your coworker's monitor.

    1. You shall check in early and check in often. You anger your coworkers when you check out a file and insist on keeping it checked out until some future point in time that is measured using variables that exist solely in your brain.
    2. You shall never check in code that breaks the build. If you code does not compile, it does not belong in the source control repository.
    3. You shall not go home for the day with files checked out, nor shall you depart for the weekend or for a vacation, with files checked out.
    4. You shall leave a descriptive comment when checking in your code. You need not include your name or the date in the comment as that information is already tracked.
    5. You shall use the 'Undo Checkout' option if you check out a file and do not make any changes. It displeases your coworkers when you check in code that has not changed at all from the original.
    6. You shall not use comments to 'save' defunct code.  Fear not, for the code you delete still exists in the source control code history and can be retrieved if needed.
    7. You shall use source control for more than archiving just code. The source code control repository makes an excellent storage for technical docs, SQL scripts, and other documents and files related to the project.
    8. You shall religiously backup your source code control database on a regular basis and store a copy in an off-site location.

     

  • Expression Magic

    In the last post we talked about needing some Expression<T> background. There is a lot of good information out there about Expression<T>, but if you haven’t heard – this class is pure magic. If you want a long version of the story, see “LINQ and C# 3.0”. For a short version of the story, read on.

    .NET compilers like the C# and VB compilers are really good at converting code into an intermediate language that the CLR’s JIT compiler will transform into native code for the CPU to execute. So if you write the following …

    Func<int, int> square = x => x * x;
    var result = square(3); // yields 9
    

    … and you open the assembly with a tool like Reflector, you’ll find the IL instructions created by the compiler.

    ldarg.0
    ldarg.1
    mul
    stloc.0

    That essentially says – load up two arguments, multiply them, then store the result. These instructions will be part of an anonymous method (as lambda expressions are just a shorter syntax for writing an anonymous method), and you can invoke the method by applying the () operator to the square variable to compute some result. IL is the perfect representation for code that ultimately needs to execute, but it’s not a great representation of the developer’s orginal intent. As an example, consider the following.

    var query = ctx.Animals
                   .Where(animal => animal.Name == "Fido");
    

    If ctx.Animals is an in-memory collection of objects, then compiling the code inside the Where method will generate efficient instructions to search for Fido. That’s good – but what if Fido lives in a database, behind a web service, or in some other remote location? Then a LINQ provider needs to translate the code inside the Where method into a web service call, a SQL query, or some other type of remote command. The LINQ provider will need to understand the programmer’s intent of the code inside the Where method, and IL is not designed to express this intent. We don’t want LINQ providers “decompiling” programs at runtime. Thus, we have Expression<T>.

    Expression<Func<int, int>> square = x => x * x;
    var result = square.Compile()(3); // yields 9
    
    

    Wrapping our function inside an Expression produces something we can’t invoke directly – we have to .Compile the expression before we can invoke it and capture a result. This is because the .NET compilers don’t produce IL when they come across an assignment to Expression<T> -  instead they produce a data structure known as an abstract syntax tree (AST). ASTs aren’t the prettiest things to look at, but they are a better representation for the code if you need to figure out what the code is trying to do. The AST for the Fido search will tell us the code consists of a binary expression that tests for equality, and that the left hand side of the equality test is the animal’s name, and the right hand side is a string constant “Fido”. This is enough information for a remote LINQ provider to translate the expression into something like “WHERE Name = ‘Fido’”.

    Code As Data

    Expression<T> gives us the ability to treat a piece of code as data, which is a relatively old concept (hello, LISP!) and fairly powerful. It gives us the ability to walk through “code” at runtime and examine what it intends to do. This feature facilitates all of the remote LINQ query providers, like LINQ to SQL and the Entity Framework, because they can examine the AST and formulate commands that represent the original code in a different language (SQL). This translation is far from simple, but it would have been impossible if the compiler was generating IL instead of syntax trees.

    There are additional scenarios that Expression<T> enables that have nothing to do with queries or databases, which is where Expression<T> gets exciting because you can use it in places you may not have expected, like in a business layer, or a mapping layer, or in the flowcharts I was working on.

    flowChartShape.RequiresField(casedata => casedata.SmokingCounseling)

    You can think of the RequiresField method call as something that can add metadata to a flowchart shape, and this metadata describes the property on some data object that the shape will use during execution. The metadata is strongly typed, intelli-sensed, and refactor friendly. We can use the metadata at runtime to determine what fields to enable in the UI, or what fields are missing that a user needs to address. We’ll dig into this more in a future post.

  • Fluent Interfaces and Flowcharts

    flowchart!In a previous post, I talked about modeling flowcharts with C# code. The flowcharts are designed, documented, and standardized  by a non-profit  organization charged with measuring the quality of patient care inside of hospitals. They do so by looking at common cases that every hospital will see, like heart failure and pneumonia patients. The logic inside each flowchart can determine if the hospital followed the “best practices” for treating each type of case. Some of the logic becomes quite elaborate, particularly when evaluating the types of antibiotics a patient received, and the antibiotic timings, relative orderings, and routes of administration.

    Sitting down with 100 of pages of flowchart logic was intimidating, and realizing that new versions came along every six months was enough to induce fear. I experimented with a few visual tools and rules engines but nothing was making the job easy and producing a maintainable solution.

    From the beginning I also was thinking about a domain specific language. At one point I decided to sit down with pen and paper to write down what I saw in the flow charts and what I thought a DSL might look like:

    a flowchart for pneumonia case data

         has a shape named “Smoking History”

             with an arrow that points to “Smoking Counseling” if the patient DOES smoke

             and an arrow that points to the shape “…” if patient DOES NOT smoke

         has a shape named “Smoking Counseling”

             with an arrow that points to …

    Not great, but it wasn’t too hard to read. It also made me realize that only a few simple abstractions were needed to make an executable flowchart. Flowcharts contain shapes, and shapes contain arrows, and each arrow points to another shape and has an associated rule to evaluate and determine if the arrow should be followed. I showed the code to a couple of these classes in a previous post.

    The only hard part then would be correctly assembling shape, arrow, and rule objects into the proper hierarchy for evaluation. One option was to parse instructions like the text that I had put down on paper, but I wanted to try something simpler first.

    Fluent Interfaces versus XML

    “Object graph” is a computer scientist’s term for a collection of related objects. I needed to arrange shapes, arrows, and rules into an object graph. We deal with object graphs all the time in software. ASPX files describe an object graph that is assembled at runtime to emit HTML. An even better example is XAML - the XML dialect for WPF, Silverlight, and WF. XAML is the direct representation of a hierarchical graph of CLR objects. I considered what a flowchart might look like in XML.

    <Flowchart Name="Pnemonia Measure 6" CaseDataType="PnemoniaCaseData">
      <Shape Name="Smoking History">
        <Arrow PointsTo="Smoking Cessation" Rule="?" />
      </Shape>
      <Shape Name="Smoking Counseling">
        <!-- ... -->
      </Shape>
      <!-- ... -->
    </Flowchart>
    

    The one stickler was how to represent the rule for an arrow. There are mechanisms available to represent expressions and code in XML (XAML’s x:Code directive element and serialized CodeDom objects ala WF rule sets are two examples), but code in XML is always tedious, cumbersome, and worst of all - impervious to refactoring operations.

    Not to mention, I think many developers today are feeling “XML fatigue” – me included. Five years of XML hyperbole followed by 5 years of software vendors putting everything and anything between < and > will do that.

    The other option was to assemble the flowchart using a fluent interface in C# – a.k.a an internal DSL. Chad Myers recently posted some excellent notes on internal DSLs, which he defines as “…bending your primary language of choice to create a special syntax that’s easier for the consumers of your API to use to accomplish some otherwise complicated task”. Chad also has notes on the method chaining pattern, which is the approach I took.

    First, I needed an extension method to add a Shape to a Flowchart…

    public static Flowchart<T, R> WithShape<T, R>(this Flowchart<T, R> chart, string shapeName)
    {
        Shape<T, R> shape = new Shape<T, R> { Name = shapeName };
        chart.Shapes.Add(shape);
        return chart;
    }
    

    …and another extension method to add an Arrow to a Shape. The trick was realizing that the arrow would always apply to the last shape added to the flowchart.

    public static Flowchart<T, R> WithArrowPointingTo<T, R>(this Flowchart<T, R> chart,
                                                            string pointsTo)
    {
        Arrow<T> arrow = new Arrow<T>();
        arrow.PointsTo = pointsTo;
        chart.LastShape().Arrows.Add(arrow);
        return chart;
    }

    Similarly, a Rule always goes to the last Arrow in the last Shape:

    public static Flowchart<T, R> AndTheRule<T, R>(this Flowchart<T, R> chart,
                                                   Func<T, bool> rule)
    {
        chart.LastShape().LastArrow().Rule = rule;
        return chart;
    }

    Using the API looks like:

    new Flowchart<PnemoniaCaseData, MeasureResult>()
        // ...       
        .WithShape("AdultSmokingHistory")
            .RequiresField(pd => pd.SmokingHistory)
            .WithArrowPointingTo("Rejected")
                .AndTheRule(pd => pd.SmokingHistory.IsMissing)
            .WithArrowPointingTo("Excluded")
                .AndTheRule(pd => pd.SmokingHistory == YesNoAnswer.No)
            .WithArrowPointingTo("AdultSmokingCounseling")
                .AndTheRule(pd => pd.SmokingHistory == YesNoAnswer.Yes)
    
        .WithShape("AdultSmokingCounseling")
            .RequiresField(pd => pd.SmokingCounseling)
            .WithArrowPointingTo("Rejected")
                .AndTheRule(pd => pd.SmokingCounseling.IsMissing)
            .WithArrowPointingTo("InDenominator")
                .AndTheRule(pd => pd.SmokingCounseling == YesNoAnswer.No)
            .WithArrowPointingTo("InNumerator")
                .AndTheRule(pd => pd.SmokingCounseling == YesNoAnswer.Yes)
    
        .WithShape("Rejected").YieldingResult(MeasureResult.Rejected)
        .WithShape("InDenominator").YieldingResult(MeasureResult.InMeasurePopulation)
        .WithShape("InNumerator").YieldingResult(MeasureResult.InNumeratorPopulation);
    

    Overall I was happy with how quickly we were able to pound out flowcharts using the fluent API. The code is relatively easy to compare to the original flowcharts in the specifications (although with the larger flowcharts, nothing is too easy). It’s also easy to test, diff-able after check-ins, and still works after refactoring the underlying data model. The real measure of success though, will be how well the code stands up to the changes and revisions over time. The verdict is still out.

    In a future post I’d like to tell you about the .RequiresField method, as it drove a number of key scenarios in the solution. First, we’ll have to talk about the magic of Expression<T>…

  • Tracking User Activity

    Many websites that support user accounts, such as the ASP.NET Forums, include information as to how what users are currently online and what they are doing. For example, the Who Is Online page on the ASP.NET Forums shows the users currently online, what forum they're viewing, and how many minutes it's been since they were last active. Unfotunately the Membership system does not offer much in the way of tracking user activity. All it has out-of-the-box is the ability to log the date and time the user was last 'active' and to report the total number of users online - that is, the count of users whose last active date and time falls within a certain time window (15 minutes, by default).

    With a little bit of elbow grease it's possible to track user activity. I use the word activity here loosely. An activity might mean visiting a particular URL, much like how the ASP.NET Forums Who Is Online page shows what forum the user is visiting. It could also be more broad. For example, if a user just updated their account information the activity could be logged as, “Updating account information.” The activity information of interest can be logged in a database table and, once there, can be used in reports, be it a Who Is Online page or a report detailing the activity of a particular user.

    To learn how to log user activity and display the results in the aforementioned reports, check out my latest article on DotNetSlackers.comTracking User Activity.

  • Announcing ShadowCamp

    There are more and more one-day local events every year, and each time people come into town, go to the event, then meander home sometime on Sunday. A couple months ago I had the idea to run a separate unaffiliated event on the Sunday morning after one of these local events.

    We will be having the first ShadowCamp this weekend on Sunday after the Raleigh Code Camp. It will be held at the same venue (ECPI) and will of course be free. The only catch is that we are capping attendance at 30, but it is first-come, first-serve. Even though Raleigh Code Camp is focused on .NET and ShadowCamp attendance will probably be slanted towards .NET, I am sure there will be plenty of people interested in other languages and technologies so don't let that stop you from coming.

    Many of these code camps now include open spaces events, but I wanted to experiment with a pure open spaces event. There will be no other talks other than open spaces.

    So, if you live in the area or are in-town for Raleigh Code Camp go register and participate in this experiment.

    -James

  • RubyConf 2008 Day 3

    I am a little late in posting this but I wanted to post my thoughts on the last day and the overall conference. I was a little "talked out" by the third day so I only ended up only going to a couple of talks

    Advanced DSLs in Ruby

    Neal Ford did an excellent job talking about how to build DSLs in Ruby and true to the name of the talk he didn't just cover the basics, he dug down into various techniques you can use to build a good DSL in Ruby. There is a great write-up of the talk over here, and Neal has posted his slides over here.

    The Ruby Code Review. A Play in Three Acts

    This was a very entertaining talk with Jim Weirich and Chris Nelson. I worked with both of these guys on my last contract but when Chris and I ran into each other we couldn't place where we knew each other from since we weren't wearing ties and in the right context. The talk was basically a mock code review of Chris showing very poorly tested and written code to Jim. It was a blast to watch and a good re-inforcement of good testing and coding principles.

    I spent the rest of the day getting ready to head out and then had a great conversation with John Nunemaker. John and I have alot in common and I had a great time exchanging ideas and brainstorming about our various projects.

    The conference was over and I headed back to the order with my free ride (Fred) and hung out at the airport with Matthew Bass and Ryan Daigle until out plane left.

    Overall it was an amazing conference, I am already thinking about next year. It was great to meet most of the members of Ruby Row and many of the other developers I have run into over the last year. One thing that I would totally love to see next year (like Jamis) is an open spaces track that would let people self-organize some great conversations and presentations about their projects. Like most conferences the best times are usually in the halls chatting with people or peering over their laptop to see what they are working on.

    -James

  • The Estrangement of LINQ to SQL

    <p>Tim Mallalieu, PM of LINQ to SQL and LINQ to Entities, recently <a href="http://blogs.msdn.com/adonet/archive/2008/10/29/update-on-linq-to-sql-and-linq-to-entities-roadmap.aspx" target="_blank">announced</a>:</p> <blockquote> <p>“…as of .NET 4.0 the Entity Framework will be our recommended data access solution for LINQ to relational scenarios.”</p></blockquote> <p>Tim later tried to <a href="http://blogs.msdn.com/adonet/archive/2008/10/31/clarifying-the-message-on-l2s-futures.aspx" target="_blank">clarify</a> the announcement in a carefully worded post:</p> <blockquote> <p>“We will continue make some investments in LINQ to SQL based on customer feedback.”</p></blockquote> <p>Although Microsoft may not remove LINQ to SQL from the framework class library just yet, they do appear to be treating LINQ to SQL as an experiment that escaped from the lab – something to minimize until it’s erased from our memories. Many consider the decision inevitable, as the ADO.NET Entity Framework is considered the crowning achievement in Microsoft’s long quest to build a deep abstraction over relational databases. </p> <h3>entityFramework.Union(linqToSql)</h3> <p>On the surface the reasoning is sound. The technologies are both solutions in the same problem space, and have overlapping features. Both frameworks use metadata to map relational data to objects, and both implement change tracking, identity maps, and a unit of work class. Each framework also has distinguishing features, however. For instance, LINQ to SQL supports POCOs and implicit lazy loading, while the Entity Framework delivers support for multiple database providers and sophisticated mapping strategies. </p> <p>The belief is that merging the distinguishing features of LINQ to SQL into the Entity Framework will produce a piece of software that makes everyone happy – but I’m not so sure. </p> <p>On the surface, conservative governments and liberal governments are both solutions in the same problem space - governance. Both will implement a chief of staff and a head of state, but the implementation details don’t matter. It’s the philosophical differences between conservatives and liberals that determines the quality of life for the individual citizens of the government … and that’s as far as this analogy will go. </p> <h3>Irreconcilable Differences</h3> <p>LINQ to SQL and the Entity Framework promote fundamentally different philosophies. </p> <p>LINQ to SQL is geared for thinking about <em>objects</em>. There is an “I’ll make this easy for you” feeling you get from the technology with its flexibility (such as mapping with XML or attributes) , and escape hatches (such as <a href="http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executequery.aspx" target="_blank">ExecuteQuery&lt;T&gt;</a>). The simplicity surrounding LINQ to SQL makes many people think of the technology as a toy, or only suitable for RAD prototyping. The truth is that LINQ to SQL just wants to give you <em>objects</em> and let you go about your business. </p> <p>The Entity Framework is geared for thinking about <em>data</em>.The crown jewel of the framework is the theoretically grounded Entity Data Model. The Entity Framework’s intent is to promote the EDM as the<em> </em>canonical, consumable <em>data</em> model both inside an application (via technologies like LINQ to Entities, Entity SQL, and ASP.NET Dynamic Data) and outside an application (via technologies like ADO.NET Data Services). The Entity Framework wants the data model to be pervasive.</p> <p>Some developers like objects, and some developers like data. I think it’s impossible for the Entity Framework to make both types of developers happy 100% of the time. Personally, I prefer the design of LINQ to SQL despite some obvious deficiencies. The Entity Framework wanted to distinguish itself from similar frameworks produced by OSS projects and third party companies, but in doing so I think they missed the key reasons that developers turn to these technologies in the first place. Version 2.0 promises to deliver some of these features, including the ability to do model first development instead of schema first development, but I’m still not convinced that the Entity Framework can ever refactor itself to deliver the simplicity, usability, and philosophy of LINQ to SQL. </p> <h3>What To Do?</h3> <p>There are many suggestions floating around on what to do with LINQ to SQL. <a href="http://codebetter.com/blogs/david.hayden/archive/2008/10/30/linq-to-sql-gets-kicked-to-the-curb-needs-a-good-home.aspx" target="_blank">David Hayden </a>and <a href="http://www.stevestreeting.com/2008/11/03/what-microsoft-should-learn-from-linq-to-sql-backlash/" target="_blank">Steve Streeting</a> both suggest an open source approach, while <a href="http://codebetter.com/blogs/ian_cooper/archive/2008/11/03/linq-to-sql-ef-and-the-thunderdome-solution.aspx" target="_blank">Ian Cooper suggests</a> a successor to both EF and L2S. </p> <p>Here is another idea…</p> <p>It occurred to me recently that I’ve spent an inordinate amount of time over the last 10 years mapping data. Perhaps this is why I am so suspicious of anyone promoting a canonical data model. Between 1998 and 2001 I was writing commercial software for the mortgage banking industry, and our biggest source of pain was getting mortgage information <em>into</em> our software. We were always on the lookout for a way to map data from spreadsheets, CSV files, XML files, and all the other quirky formats banks had to offer. From 2001 to present I’ve been writing commercial software for the healthcare industry. Again, there is mapping required to process data in the hospital’s format, the government’s format, the insurance company’s format, the third party vendor’s format, and 100 permutations of the canonical (there is that word again) “industry standard” formats. </p> <p>The mapping never stops at the system boundaries, either. In the last week I’ve mapped domain objects to DTOs, DTOs to UI controls, and UI control status to the UI control status <em>status</em> display. Sprinkle in a little OLAP, a little XML, and some JSON web services with their own custom mappings and I’ve started to think that all software is controlled by a series of carefully constructed hash tables. </p> <p>The internals of LINQ to SQL could, I think, form the nice foundation for a generic data mapping framework that would be complementary to ORM type frameworks like the Entity Framework. The framework could take out some of the grunt work of object to object mapping, and perhaps through a provider type architecture offer additional mapping capabilities to and from various formats, like XHTML, JSON, CSV. Transforming, massaging, and pushing around data is a fact of life in most large applications and a framework that makes this job easier to implement and test in a uniform manner would be a boon for productivity. </p> <p>In the end, I hope LINQ to SQL garners enough support to remain viable and evolve, but at this time it looks as if LINQ to SQL is facing an <a href="http://starwars.wikia.com/wiki/Order_66" target="_blank">Order 66</a>. </p>
  • Modeling Flowcharts

    A few months ago I worked on a system that was based on a set specifications that included some gnarly flowcharts (see pages 7 – 17 for an example). The good news was that the specs were concrete and readily available. The bad news was that the specs change every 6 months.

    I explored a number of options for modeling the logic in the flowcharts, including WF, WF rules, and code generation from XML, but ultimately decided on staying as close as possible to the flowcharts with C# code. Maintainability and testability were the keys to survival. The end result of building a flowchart in code looks like the following:

    var flowChart = new Flowchart<PnemoniaCaseData, MeasureResult>()
            // ... 
            .WithShape("TransferFromAnotherED")
                .RequiresField(pd => pd.TransferFromAnotherED)
                .WithArrowPointingTo("Rejected")
                    .AndTheRule(pd => pd.TransferFromAnotherED.IsMissing)
                .WithArrowPointingTo("Excluded")
                    .AndTheRule(pd => pd.TransferFromAnotherED == YesNoAnswer.Yes)
                .WithArrowPointingTo("PointOfOriginForAdmissionOrVisit")
                    .AndTheRule(pd => pd.TransferFromAnotherED == YesNoAnswer.No)
            .WithShape("PointOfOriginForAdmissionOrVisit")
                .RequiresField(pd => pd.PointOfOriginForAdmissionOrVisit)
                // ... lots more of the same
            .WithShape("Rejected").YieldingResult(MeasureResult.Rejected)
            .WithShape("Excluded").YieldingResult(MeasureResult.Excluded)
            .WithShape("InDenominator").YieldingResult(MeasureResult.InMeasurePopulation)
            .WithShape("InNumerator").YieldingResult(MeasureResult.InNumeratorPopulation);
    

    The flowcharts, particularly the lengthier ones, were still tedious to build, but overall I think this approach gave us something that we could use to crank out the 30 odd flowcharts in a maintainable, testable, and arguably more readable fashion than some of the other methods I considered. All the string literals tend to make me nervous, but they mostly match a corresponding shape in the specification, making an eyeball comparison easier. Typographical errors in the strings are easily negated with tests that use LINQ queries. For example, there should never be two shapes in the same flowchart with the same name:

    var duplicateShapes = Shapes.GroupBy(s => s.Name).Where(g => g.Count() > 1);

    And there should never be an arrow pointing to a shape name that doesn’t exist:

    var names = Shapes.Select(s => s.Name);
    var problemTransitions = Shapes.SelectMany(s => s.Arrows)
                                   .Where(a => !names.Contains(a.PointsTo));
    

    The classes used to model the flowchart were relatively simple. For example, here is the Shape class that takes generic parameters to define the type of data it operates on, and the type of the result it can yield.

    public class Shape<T,R>
    {
        public Shape()
        {
            Arrows = new List<Arrow<T>>();
            Result = default(R);
        }
        public R Result { get; set; }        
        public string Name { get; set; }
        public PropertySpecifier<T> RequiredField { get; set; }
        public List<Arrow<T>> Arrows { get; set; }
    }