Programming, Policitcs, and uhhh Pineapples.
# Wednesday, January 06, 2010

SharePoint 2010 Content Type Publishing Setup

Wednesday, January 06, 2010 9:46:35 PM UTC

One of the cool features of SharePoint 2010 is the simplification of content type management/synchronization.

In SharePoint 2007, site collections formed a metadata boundary meaning that content types defined in one site collection could not be used in another site collection without redeploying the content type to the other site collection.  This works well enough if you are using features to deploy your content types (and I mean, who isn't!?), but what about managing those content types and keeping them synchronized after you've deployed them?

Nightmare.

While it's relatively easy to set up, I came across an immediate error when trying to publish my first content type:

No valid proxy can be found to do this operation.

Using the correlation ID, I looked through the logs and found the following:

No proxy associated with the site http://metadata.dev.com
recognizes the site as a hub site d4d22525-2e9d-4ea4-becc-fe1d42735ee4 

A user failed to publish or unpublish a content type
Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypeSyndicationException:
No valid proxy can be found to do this operation.

I came across Chakkaradeep Chandran's instructions here, but his instructions seemed to have a small gap.

To access the functionality, you need to go to Central Administration -> Application Management -> Manage service applications (under Service Applications)

In this screen, select (don't click the link) Managed Metadata Service.

Again, very important: don't click the link (that takes you to the screen for managing keywords/terms/taxonomy; click somewhere next to the text and select the row to enable the Properties button at the top.

Next click Properties:

This will bring up the pane where you can set the Content Type hub setting (scroll to the bottom of the screen).  Just enter the URL of your hub application (as an example, http://metadata.dev.com)

Next, you need to select the Managed Metadata Service Connection (the indented Managed Metadata Service) and again, click Properties.  This brings up the pane that will allow you to check the Consumes content types from the... setting.

As Chakaradeep mentions, you need to run two timer jobs to get the content types to sync up.  You can access this via Central Administration -> Check Job Status (under Monitoring):

Then page through that list looking for Content Type hub and Content Type subscriber.  Clicking on the link to the job will open it up and allow you to click the Run now button to execute the job immediately.

In any case, it's a neat feature.  I wonder, though, if there are improvements for managing deployment of content types via features (i.e. an incremental feature which adds new fields to exsiting content types and pushes the changes down) as opposed to UI based management of content types (which I'm not a terribly big fan of).  My guess is "no".

Hope this helps someone!

# Wednesday, December 30, 2009

C# and ASP.NET Syntax Highlighting in Trac

Wednesday, December 30, 2009 1:08:37 AM UTC

Well, spent the good amount of time trying to figure this out.  See the configuration info below from my trac.ini file.

[mimeviewer]
max_preview_size = 262144
mime_map = text/x-dylan:dylan,text/x-idl:ice,text/x-ada:ads:adb,
php_path = php
pygments_default_style = trac
pygments_modes = text/x-csharp:csharp:7,text/plain:aspx-cs:7
tab_width = 4
treat_as_binary = application/octet-stream,application/pdf,application/postscript,application/rtf

Oh yeah, it helps if you actually install Pygments, too.

# Tuesday, December 22, 2009

SharePoint Design Patterns: Entry 2.5

Tuesday, December 22, 2009 3:11:48 AM UTC

In the previous entry, we looked at how we can model a SharePoint list item using a more domain specific model to simplify programmatic access to the list item thus reducing otherwise error prone data access code and making the overall framework easier to use.  Again: the idea is to promote reuse and decrease complexity through domain specific code that abstracts the underlying SharePoint object models, making it easier for a team to build functionality on top of this framework.

One interesting point is that if you're already building your fields and content types using features XML, the work required to generate the domain specific wrappers can be simplified dramatically using automation.  In a sense, a content type is basically a class (this is generally how I map them in my domain design); why double your effort and write both the content types and the classes?

So how do we go about this?

(Side note: this isn't so much a "design pattern" as it is an "implementation pattern")

As an example, here is a simple XML file which defines a set of fields and content types which use those fields:

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Field DisplayName="Model Code"
    Name="Model_Code"
    StaticName="Model_Code"
    ID="{F0000000-0000-0000-0000-000000000001}"
    Type="Integer"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <Field DisplayName="VIN"
    Name="VIN"
    StaticName="VIN"
    ID="{F0000000-0000-0000-0000-000000000002}"
    Type="Text"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <Field DisplayName="Make"
    Name="Make"
    StaticName="Make"
    ID="{F0000000-0000-0000-0000-00000000003}"
    Type="Text"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <ContentType Name="Vehicle"
    ID="0x0100FC000000000000000000000000000001"
    Description="Used car inventory"
    Group="My Custom Content Types" >
    <FieldRefs>
      <FieldRef ID="{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" Name="ContentType" />
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" 
        Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
      <FieldRef ID="{F0000000-0000-0000-0000-000000000001}" Name="Model_Code"/>
      <FieldRef ID="{F0000000-0000-0000-0000-000000000002}" Name="VIN"/>
      <FieldRef ID="{F0000000-0000-0000-0000-000000000003}" Name="Make"/>
    </FieldRefs>
  </ContentType>  
  <Field DisplayName="Dealership Code"
    Name="Dealership_Code"
    StaticName="Dealership_Code"
    ID="{F0000000-0000-0000-0000-00000000004}"
    Type="Integer"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <Field DisplayName="Dealership Fax Number"
    Name="Dealership_Fax_Number"
    StaticName="Dealership_Fax_Number"
    ID="{F0000000-0000-0000-0000-00000000005}"
    Type="Text"
    SourceID="http://schemas.someusedcarinventory.com"
    Group="My Custom Columns"/>
  <ContentType Name="Dealership"
    ID="0x0100FC000000000000000000000000000002"
    Description="Dealerships"
    Group="My Custom Content Types" >
    <FieldRefs>
      <FieldRef ID="{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" Name="ContentType" />
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" 
        Required="TRUE" ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
      <FieldRef ID="{F0000000-0000-0000-0000-000000000004}" Name="Dealership_Code"/>
      <FieldRef ID="{F0000000-0000-0000-0000-000000000005}" Name="Dealership_Fax_Number"/>
    </FieldRefs>
  </ContentType>    
</Elements>

When approaching this problem, I considered three ways of handling the class file generation:

  1. Use an object model and StringTemplate to create .cs files.  This invovled writing POCO classes (or generating them from the schema) which I could deserialize the XML to and then passing those objects to a template instance.  This seemed like too much work, given that I really didn't feel like maintaining all of that code as well.  Plus, while StringTemplate isn't - by any sense of the imagination - hard, it is a non-standard syntax that someone would have to learn to maintain and/or extend the conversion.
  2. Use an XDocument and CodeDom to create .cs files.  This seemed like even more work!  While it's framework supported, I feel like this solution would be hard to extend and maintain for most developers.
  3. Use an XSL transform to create .cs files.  This seemed to be the most natural solution given that the source file is already in XML format and a the target content structure is far from complex (the basic class file is fairly simple).  Plus, while XSLT isn't trivial, it's not that hard either (and the syntax is "standard").

One of the cool features of XSL 2.0 is the xsl:result-document element which allows you to create multiple documents from one source document.  Only one problem: .NET's XSLT engine doesn't implement XSLT 2.0!  What a bummer; it seemed like if I wanted to get this to work and generate multiple output files, it was going to take some work in code or find an XSLT 2.0 capable processor.

Enter Saxon, which provides an XSLT 2.0 processor for .NET.  The following code takes the XML above and uses the xsl:result-document to create two class files, one for each content type:

using System;
using System.IO;
using Saxon.Api;

namespace FeatureToClass
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Uri xmlFile = new Uri(
                @"C:\Users\Charles\Desktop\elements.xml");

            // Create a Processor instance.  
            Processor p = new Processor();

            // Load the source document.  
            XdmNode node = p.NewDocumentBuilder().Build(xmlFile);

            using (Stream stream = File.OpenRead("core-transform.xslt"))
            {
                // Create a transformer for the stylesheet.  
                XsltTransformer transformer = 
                    p.NewXsltCompiler().Compile(stream).Load();

                // Set the root node of the source document
                // to be the initial context node.  
                transformer.InitialContextNode = node;

                // BaseOutputUri is only necessary for xsl:result-document.  
                transformer.BaseOutputUri = xmlFile;

                transformer.SetParameter(
                    new QName("ct", "http://www.customtransform.com", "namespace"), 
                    new XdmAtomicValue("My.Custom.Package"));

                // Create a serializer.  
                Serializer serializer = new Serializer();
                transformer.Run(serializer);
            }
        }
    }
}

The code above is a simple console program that takes a (hardcoded) path to a source XML file (a SharePoint elements.xml file) and (hardcoded) namespace and loads an XSL file to transform the XML to C# class files.

Here's the transform (it's a bit messy for output formatting reasons, so you're best off copying it into an XML aware text editor to get a better view):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
    <!ENTITY space "<xsl:text> </xsl:text>">
    <!ENTITY cr "<xsl:text>
</xsl:text>">
]>

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:sp="http://schemas.microsoft.com/sharepoint/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:functx="http://www.functx.com"
  xmlns:ct="http://www.customtransform.com"
  version="2.0">
    <xsl:param name="ct:namespace"/>
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="//sp:ContentType">
            <xsl:variable name="classname" select="replace(@Name, ' ', '')"/>
            <xsl:variable name="filename" select="concat($classname,'.cs')" />
            <xsl:value-of select="$filename" />
            <!-- Creating  -->
            <xsl:result-document href="{$filename}">
using System;

    namespace <xsl:value-of select="$ct:namespace"/>
    {
        public partial class <xsl:value-of select="$classname"/>
        {
            private string _contentTypeId;
            
            public string ContentTypeId {
                get { return _contentTypeId; }
                set { _contentTypeId = value; }
            }

            public <xsl:value-of select="$classname"/>()
            {
                _contentType = "<xsl:value-of select='@Name'/>";
                _contentTypeId = "<xsl:value-of select='@ID'/>";
            }

            <xsl:apply-templates/>
    }
}
            </xsl:result-document>
        </xsl:for-each>
    </xsl:template>

    <!--///
        Templates
    ///-->
    <xsl:template match="sp:FieldRef">
        <xsl:variable name="fieldname" select="functx:lower-first(replace(@Name, '_', ''))"/>
        <xsl:variable name="fieldid" select="@ID"/>
        <xsl:variable name="dotnettype">
            <xsl:choose>
                <xsl:when test="//sp:Field[(@ID = $fieldid) and (@Type = 'Integer')]">int</xsl:when>
                <xsl:otherwise>string</xsl:otherwise>
            </xsl:choose>
        </xsl:variable>

        private <xsl:value-of select="$dotnettype"/> _<xsl:value-of select="$fieldname" />;
        <xsl:call-template name="attribute"><xsl:with-param name="fieldid" select="$fieldid"/></xsl:call-template>
        public <xsl:value-of select="$dotnettype"/>&space;<xsl:value-of select="replace(@Name, '_', '')"/>
        {
            get { return _<xsl:value-of select="$fieldname" />; }
            set { _<xsl:value-of select="$fieldname" /> = value; }
        }
    </xsl:template>

    <xsl:template name="attribute">
        <xsl:param name="fieldid"/>
    <xsl:if test="exists(//sp:Field[@ID = $fieldid]/@ID)">[Caml(Id="<xsl:value-of select='//sp:Field[@ID = $fieldid]/@ID'/>", StaticName="<xsl:value-of select='//sp:Field[@ID = $fieldid]/@StaticName'/>", Type="<xsl:value-of select='//sp:Field[@ID = $fieldid]/@Type'/>")]</xsl:if></xsl:template>


    <!--///
        Custom functions
    ///-->
    <xsl:function
        name="functx:lower-first" as="xs:string"
        xmlns:functx="http://www.functx.com" >
        <xsl:param name="arg" as="xs:string"/>
        <xsl:sequence select="concat(lower-case(substring($arg,1,1)), substring($arg,2))"/>
    </xsl:function>
</xsl:stylesheet>

I borrowed one function from the FunctX library to create the camelCased field names.  The XSL probably isn't nearly as clean or optimized as it should be (my XSL is admittedly a bit rusty), but it gets the job done.  Here's one of the two classes (and class files) which get generated at the source directory of the input XML file:

using System;

namespace My.Custom.Package 
{
    public partial class Dealership
    {
        private string _contentTypeId;
        public string ContentTypeId {
            get { return _contentTypeId; }
            set { _contentTypeId = value; }
        }   

        public Dealership()
        {
            _contentType = "Dealership";
            _contentTypeId = "0x0100FC000000000000000000000000000002";
        }
                        
        private string _contentType;
        public string ContentType
        {
            get { return _contentType; }
            set { _contentType = value; }
        }      

        private string _title;
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
    
        private int _dealershipCode;
        [Caml(Id="{F0000000-0000-0000-0000-000000000003}", 
            StaticName="Dealership_Code", Type="Integer")]
        public int DealershipCode
        {
            get { return _dealershipCode; }
            set { _dealershipCode = value; }
        }
    
        private string _dealershipFaxNumber;
        [Caml(Id="{F0000000-0000-0000-0000-000000000004}", 
            StaticName="Dealership_Fax_Number", Type="Text")]
        public string DealershipFaxNumber
        {
            get { return _dealershipFaxNumber; }
            set { _dealershipFaxNumber = value; }
        }
    }
}

Awesome!  It's amazing how little code was required to get this basic scenario working.

Now we have a single source which defines our SharePoint artifacts and our code artifacts; I love it.  Write your fields and content types in your feature and you get class files for free!  You'll note that I've added a simple CamlAttribute where applicable.  This will prove handy when it comes time to automate construction of the object instance from a SharePoint list item, which we'll look at next time (for the time being, feel free to modify the XSL and remove the line for it or write an implementation of CamlAttribute).

Again, to reiterate: the goal is make it easy to build applications on top of a SharePoint deployment by adding a layer of domain specific APIs and objects so that a team can be productive while reducing duplication and the ramp up time required to understand the business domain. 

Other points for improvement and enhancement (look for these in a future installment):

  1. Parameterize the program.
  2. Consider making it a Visual Studio add-in or a custom tool.
  3. Make it go the other way; in other words: generate the content type and field XML from class files (which would be cool, too).

But even as it is, it's incredibly useful.  On the next installment, we'll see how we can build more intelligence into the model and make it more useful.

# Monday, December 07, 2009

Host Headers, SharePoint (2010), and Access Denied (401)

Monday, December 07, 2009 8:10:25 PM UTC

I've been working on setting up a VM to play around with SharePoint 2010 and kept running into a weird issue where I would be prompted for my credentials repeatedly and I was denied access to the site entirely, even when using an administrator account.

I had set up my SharePoint web application using a host header; for example, beta.dev.com.

Meanwhile, I was able to access the same site from my host environment without any issue.  What gives?  Turns out that there is a slight registry tweak you have to make when working with host headers and IIS.

Phil Harding has a good writeup with links to the Microsoft KBs and additional details.

# Friday, December 04, 2009

More WebSequenceDiagrams.com Awesomeness

Friday, December 04, 2009 12:43:54 AM UTC

As I've been working with a client which has demanded rigorous sequence diagrams as deliverables for the design phase of the project, I've started to use WebSequenceDiagrams.com more and more.

I've blogged about it previously, but I've only come to truly appreciate it after having to use Visio for a few days before I convinced the client that I could deliver the content faster and in an easier to maintain format (well, text) using WSD. 

What Visio Gets Wrong


I ran out of connection points.  Yes, it's possible; for a long activation sequence, you can actually run out of connection points.  One could argue that this calls for a refactoring of the diagram in the first place, but then I would say that you've never tried to actually refactor a Visio diagram...I still find it hard to believe: the activation box ran out of connection points; I wouldn't have believed it if I didn't see it myself:

I kept having to resize the page.  By default, you can't really fit much on the page.  But as I started to build my sequence up, I found that I had to keep toggling around with the paper size just so that I would have a grid.  This was annoying since it also involved then zooming out so that I could select everything and reposition it to the top left corner of the page then zooming back in.  You'd think that Visio would be smart enough to do this, but it isn't...

I kept having to move elements around.  Want to add a new step?  What about introducing a new actor in between two existing actors?  Prepare for some carpal tunnel my friend.  There's simply no easy way to do it aside from zooming out, grabbing everything to the right and shifting it around while counting gridlines and getting your result some 10-20 clicks later.  What's worse is that you end up having to scroll around horizontally (reordering actors) and vertically (reordering steps) while dragging a bunch of stuff around.

I kept having to fix connection lines.  This was absolutely mindboggling: if I extended an activation, it would cause the first connection on the activation to jump, which would then require me to manually drag the connection back to where it belonged.  I probably spent a good 10% of my time simply fixing these connection points as a adjusted activations:

This is an incomprehensible design flaw; I have no idea how people work around this in Visio since I adjust activations multiple times as I'm working through a diagram.

There was no representation of alternate paths or optional steps.  I ended up having to draw a rectangle and manually managing the size of it as I changed steps and added more steps.  What made this even more annoying was that having the rectangle, even though I sent it to the back, then made it difficult to select elements that were enclosed by the rectangle like a message line or a connection point or a note; I'd end up selecting this stupid rectangle instead.

I had to keep managing the location of notes.  The notes don't seem to anchor to anything and it's not clear to me how that's supposed to work.  That meant that I had to keep moving notes around as I changed activations and modified connection points.

The lifelines didn't synchronize.  For the life of me, I couldn't figure out how to get the lifelines to synchronize in length so I didn't have to manually go back and drag each one down to the same length.  You can't actually CTRL+click two lifelines, as one would think you'd be able to do, and drag them both to extend them simultaneously.  I mean, this seamed like pretty basic stuff to me.

So yeah, all in all, I'm not sure why anyone would want to subject themselves to the pain of creating sequence diagrams in Visio.  Maybe if it's a final product and you don't plan on touching it ever again and you've already done most of the work on paper or something, but it's a terrible tool if you're just trying to think an idea out and see it visually.

What WebSequenceDiagrams.com Gets Right


I can't speak to the console program or the DLL (yet), but I decided that the only way that I could do this right was to do it in WSD first and then just use Visio to render the final output.  While doing it in the browser is fine, I found it much easier to do it in EditPlus, my text editor of choice.

The first step was to create a syntax and auto-complete file so that it was a little more user-friendly.

Here's my .stx syntax file:

#TITLE=WSD

#DELIMITER=,(){}[]+*%/="'~!&|<?:;.
#QUOTATION1="
#LINECOMMENT=note
#CASE=y

#KEYWORD=Activate
activate

#KEYWORD=Deactivate
deactivate
destroy

#KEYWORD=Alias
as
participant

#KEYWORD=Notes
note
over
right
left

#KEYWORD=Control
alt
else
end
opt

#KEYWORD=Newline
\n

#KEYWORD=Transition
->
-->
#

And my .acp auto-complete file:

#TITLE=WSD

#CASE=y

#T=act
activate ^!
#T=de
deactivate ^!
#T=pa
participant ^!
#T=alt
alt ^!
    
else
    
end
#T=opt
opt ^!
	
end
#

What I get from this is:

I know what you're thinking: Chuck, that looks like code!  By golly, it does!  And -- at least to me -- that's the beauty; all of a sudden, a frustrating, time consuming, mouse-centric activity becomes a keyboard-centric, coding-like activity.  Moving objects around becomes a matter of moving lines of text.  Reordering actors involves moving your participant declarations around.  Notes stay in their context if you add a step since everything gets pushed down.  There's no manual resizing of anything.  There's no fixing connection points.  There's no stupid.  It actually makes working with sequence diagrams, beyond just whiteboarding, much more useful and much more productive as it lets you kind of think out the code by actually writing pseudocode.

I highly recommend downloading EditPlus (you can keep using it for free, perpetually, if you're a cheap bastard or pony up the $20 for such an awesome editor).  For me, EditPlus is a perfect pairing for WSD due to the easy to create language syntax/autocomplete files and the handy split-document feature so you can easily reference your participants at the top.

Simply create a new file type and add the .stx and .acp files I defined:

Now the downside of this whole affair is that you have an extra step of having to copy out the text and pasting it into the browser, but even with that extra little bit of annoyance, the time and frustration saved over working with Visio is more than worth it.

The next step, once I get my hands on the command line tool, is to hook it up to the external tools feature of EditPlus for a quick hit.  I'm also considering writing an integration for Visual Studio for custom rendering within VS or at least something quick-and-dirty like an add-in.

One additional note is that WSD has an HTML/Javascript API whereby you can render a diagram inline with your HTML by simply using a set of <pre></pre> tags and a reference to a Javascript file.

What's cool about this is that now you can use the standard CTRL+B/CTRL+E shortcut keys to preview without a copy/paste step!  For free!  That's pretty awesome.

Of course, the downside is that using this method, there is a limit to the size of the sequence that you can send up as well as the fact that you need some additional hijinks to make the syntax highlighting work (I gave up on that part :-D) .  But if were doing it in the browser to begin with or using notepad and you don't care for the syntax highlighting, then this is a huge upgrade.

Conclusion: stop using Visio :-D Now I'm just looking forward to WebERDiagrams.com, WebStateMachineDiagram.com, and....well, you get the idea.

My EditPlus .stx and .acp files for anyone that wants 'em: wsd-files.zip (.52 KB)

# Monday, November 23, 2009

Galileo's Fingers Found

Monday, November 23, 2009 5:48:12 PM UTC

Cool story because Galileo is one of my top 5 scientists:

(CNN) -- Two fingers cut from the hand of Italian astronomer Galileo nearly 300 years ago have been rediscovered more than a century after they were last seen, an Italian museum director said Monday.

Removing body parts from the corpse was an echo of a practice common with saints, whose digits, tongues and organs were revered by Catholics as relics with sacred powers.

There is an irony in Galileo's having been subjected to the same treatment, since he was persecuted by the Catholic Church for advocating the theory that the earth circles the sun, rather than the other way around. The Inquisition forced him to recant, and jailed him in 1634.

The people who cut off his fingers essentially considered him a secular saint, Galluzzi said, noting the fingers that were removed were the ones he would have used to hold a pen.

"Exactly as it was practiced with saints of religion, so with saints of science," Galluzzi said. "He was a hero and a martyr, keeping alive freedom of thought and freedom of research."

Very awesome.

# Friday, November 13, 2009

Removing a Dead SharePoint Web Part

Friday, November 13, 2009 7:48:08 PM UTC

If you've deleted the DLL or if you reinstalled a DLL which no longer contains the codebehind class for a user control in a web part, you could end up in a scenario where you have a "dead" web part on the page.  In my case, it prevented the page from loading and resulted in an error because it couldn't load the type.

Don't panic!  The solution is to add a ?Contents=1 to the URL (i.e. http://moss.dev.com/default.aspx?Contents=1), which will allow you to delete the web part from the page.

# Thursday, November 05, 2009

Analysis Paralysis : Getting Carried Away With UML

Thursday, November 05, 2009 5:07:09 PM UTC

UML is a useful tool, no doubt.  It's a tool to help model complex logic in a visual manner.  It's a language in and of itself and it can aid in communicating design ideas with exacting precision, leaving little room for error.  However, at the same time, as with any formal language, it creates strict rules for communicating in that language.  Syntax, vocabulary, "grammar"...these all apply to propperly using UML.

Myself, I've never been a big fan of UML.  There are different ways to convey ideas, intent, and understanding of a set of requirements, but imposing UML is like asking a blogger to write all of their posts in iambic pentameter when prose would work just as well.  What's the point?  Your colleagues can already all read English, but not everyone reads UML...why add that burden and rigor?

There's a big difference between the rigor required to build a bridge and the rigor required to build a web application.  Miss your target by a foot, and it's a multi-million dollar mistake if you're building a bridge.  If your bridge throws you an "unexpected exception"? You're talking possible risk of life!  A web application or a portal?  Unless there are fundamental, framework level mistakes, most changes are negotiable; it's software for a reason (and oddly enough, on large projects, the cost overhead isn't necessarily associated with the development side, but with the business side and the specific processes for validation, testing, and change management - maybe those guys need to fix their processes...).

Now this isn't to say that getting it right isn't important, but at some point, process and progress starts to drag as you impose an inordinate amount of rigor. 

With regards to sequence diagrams, Scott Ambler puts it very well:

The most important things that you can do is to keep your diagrams simple, both content wise and tool wise.  I will sketch sequence diagrams on whiteboards to think something through, either to verify the logic in a use case or to design a method or service.  I rarely keep sequence diagrams as I find their true value is in their creation.   

A common mistake is to try to create a complete set of sequence diagrams for your system.  I’ve seen project teams waste months creating several sequence diagrams for each of their use cases, one for the basic course of action and one for each alternate course.  My advice is to only create a sequence diagram when you have complex logic that you want to think through – if the logic is straightforward the sequence diagram won’t add any value, you had might as well go straight to code.

I agree wholeheartedly; the exercise itself is more important than the final artifact.  It's far too easy to interpret the intent of UML incorrectly; the value is not the artifact, the value is the process.

At the end of the day, it is not a silver bullet! It doesn't make your design more complete.  It doesn't mitigate all of the risk.  It doesn't make your intent free from misrepresentation or misinterpretation.  It doesn't make a design document bulletproof.

# Monday, October 05, 2009

Ordering Wood Floors Online

Monday, October 05, 2009 2:16:48 PM UTC

Growing up in the Internet Age, I've become fairly accustomed to and comfortable with buying just about everything online.

But even for me, ordering wood floors online -- by the pallet, no less -- was fraught with "what if's" and a bit of trepidation.  It isn't one of those things that you can just return if you get it and you don't like it or it's damaged or something and it's not exactly easy to get more product if you happen to be short a few square feet.

After doing much research and collecting dozens of samples (over a period of a few years...), I finally decided that I was ready to pull the trigger a few weeks ago and decided on BuildDirect.com for a couple of reasons:

  1. They were having a sale on some really nice engineered mahogany.
  2. Their prices couldn't be beat, even factoring in delivery charges (which are very high compared to some other e-tailers).
  3. Their web app was top notch; very well done.

This review is for people who are considering ordering from them (or ordering flooring online in general) since there weren't many online resources when I was researching them.

Ordering Samples

The sample ordering process is pretty simple and the samples are free (minus a delivery fee).  I've ordered at least a dozen samples from them over a period of two or three years, trying to find the perfect flooring.  Compared to other online flooring companies, I've found that they give pretty generous sample sizes in terms of length so you can get a better feel for the wood.  I will note that there are some products which you have to call to obtain a sample for.

I would recommend that when ordering certain types of wood, get multiple samples since with mahogany or Brazilian cherry, for example, different boards can show dramatically different colors and patterns.  We were considering mahogany, Brazilian cherry, acacia, and some of their twisted strand bamboo (really, really nice floors, IMO - definitely check these out).

I particularly like how the web app displays the weight information, the delivery source, and calculates the cost of shipping and adds it to the price per square foot calculation. 

Their sample ordering process gets a 5/5.

Ordering Product

Once you've decided on your flooring type and measured your square footage, the next step is to go online and order the flooring.  It should be noted that you should probably order at least 110% of your measured square footage.  We had installers put down our floors and the first crew wasn't very conservative with their usage and we ended up one box short.  At that point, you really have a tough choice to make; for us, we've decided to sell the extra flooring (or give it away to family members) instead of ordering additional flooring since the shipping costs would be silly.  Our kitchen was supposed to get the wood as well, but since we were short about 30 square feet, we decided to just leave it for now.

The initial ordering process is generally pretty easy.  It's not much more difficult than the sample ordering process.  However, there does seem to be this extra step of having to call their offices to confirm the order and what not.  At times, it can be difficult to reach anyone in the office (a secretary always picks up, but then you need to be transferred to the right department).  This became a pain-in-the-ass game of phone tag trying to reach the guy.

It was also a bit of a pain to adjust the order size once I had the installer come out and measure and estimate.  Again, it was hard to reach someone in the right department.

They were also initially somewhat inflexible with their pricing.  I had purchased a large quantity (most of it) on a sale price but then wanted to add an additional 400 or so square feet and some accessory pieces.  The sales rep. initially gave me some beef about adding the additional square footage at the original sales price until my wife called and threatened to cancel the order.  We did get the flooring at the sale price, but I don't understand why they made us jump through hoops to get it considering this order was coming in at several thousand dollars already.

One minor gripe is that you can't just go online and order more accessories; if you try to do it online, you have to order more product to be able to do it.  It seems like you should be able to go to your original order and add a limited quantity of product or accessory pieces.  In the end, I simply had to call them up to get one or two more transition pieces, but still, it would have been easier to do it online.

Their ordering process gets a 2.5/5.

Delivery

The delivery was probably one of the most annoying aspects.  The first issue is that their delivery window is very large; you won't know when you'll get it until you get a call the day before asking you to schedule delivery.  This is a pain in the ass.

The second issue is that while their web site is generally pretty well done, their delivery estimates aren't very well integrated with their stock levels as we were delayed by a month as their stock levels were low.  This wasn't an issue for us since our schedule was flexible, but I can see how this might be an issue if you're in the process of building a house and you have a much less flexible timeline.

The actual delivery itself was a bit of a pain as well.  The product comes in pallets, but the pallets for our product were like 8x4 (oblong), which makes it a bit harder to store (say in your garage since you have to clear out a lot more space).  Not only that, they're only supposed to drop it in your driveway!  As I wasn't home that week, we asked my father-in-law to accept delivery for us.  He gave the driver a $50 tip to help him get it into the garage using his hand lift.  Just beware that the delivery process is less than optimal for average folk.

It would be one thing if they could give you a very specific delivery date and then you could schedule your installers to be there at the same time to move/install the product, but the combination of such a wide delivery window and delivery service (driveway drop) makes it a bit of a pain.

On the other hand, I have to say, the product was very well packaged and had no damage at all.  This was one of my primary concerns, that there would be damage during transit and that we'd have to deal with a messy return process.

Their delivery process gets a 3/5.

Product

Despite all that, the wife and I absolutely love the floors.  The engineered material is pretty good at this price point when you compare it against the stuff you'd find in big box stores like Home Depot or even from Lumber Liquidators.  The specific engineered flooring that we got, their house brand Vanier Santos Mahogany, came with a 4mm wear layer.  At this price point, if you were going to get it from HD or Lowes, you'd probably be getting something like a 0.5mm wear layer.  With a 4mm wear layer, it can be sanded and refinished at least once in its lifetime (probably twice).

From a durability perspective, we've only had it for a few weeks now, but we have three cats and as far as I can tell, it looks to be holding up well to their claws.  I was worried about rolling around in the office chairs and leaving marks, but so far, it's been fine, even without a protective plastic mat or an area rug.

I was a bit worried watching the installers handle the product since they were pretty rough with it, tossing around pieces here and there, stepping over them, hammering it pretty heavily with rubber mallets...but it held up.  There were only like one or two places where their rough handling was apparent (possibly from dropping a nail gun).  Otherwise, the boards seemed fairly resilient - no breakage, no splitting of the veneer from the base, no cracking.  I should note that watching the installers cut the boards lengthwise, they did it by simply using a hand held circular saw and they were able to get very clean cuts.  I would think this speaks a bit to the workability of the product.

Overall, in the 60 some boxes that were installed, there were probably only 3-4 boards that had defects in them (hairline cracks in the finish, splintered tongue, etc) and only a few boards which weren't very attractive (only based on the specific tree or the section of the tree that it was cut from - we set these aside or used them in closets).  Otherwise, the boards were remarkably well milled and 99% perfect in terms of being square and straight.  The accessory pieces also matched nicely in terms of color and finish.

The packaging should also be noted as well: the contents of each box were sealed with fairly durable plastic and there were thin layers of foam sheets between each layer of the product inside the box.  I was pretty impressed with the lengths they went to ensure that the product wasn't going to be damaged in transit (although I wasn't too happy about how much waste this generated).

The product itself gets a 5/5.

Overall

I'd say I'd probably order from them again in the future and would recommend them to friends and family.  In the end, I think it's a great value as long as you're willing to plan it out properly and accept the risks with ordering this stuff sight unseen (it can be really hard to judge some types of flooring based on samples).

I'd say it's a solid 4/5.

# Wednesday, September 30, 2009

Chain Of Command And Passing Parameters

Wednesday, September 30, 2009 2:02:46 AM UTC

One of the more useful patterns that I've used quite frequently is a version of Chain of Responsibility that integrates with the Command pattern.  In a classic CoR, the idea is that only one component in the chain handles the request and then execution flows out of the chain.  In a CoC pattern, the idea is that the execution flows through the entire chain.

I like Shahan Khatchadourian's description of this pattern:

When programming, certain sections of code can sometimes be viewed as a workflow or preset sequence of tasks or commands. This can considered to be the design pattern called Chain of Command...

There are two key problems that this pattern solves that make it immensely useful in everyday programming (it's a bit surprising that dofactory's listing of CoR lists the frequency of use as a 2/5).

The first problem that it solves is extensibility.  By implementing the chain as a list of abstract types (Command or Validator or whatever), using reflection (one way or another), you can build a list of concrete commands, giving each element in the chain a chance of working on the input request.  One example of how I use this is to implement validation where I might have an abstract base class called Validator.  To build the chain of validators, one very quick and easy solution is to reflect on the assembly and simply find all of the classes which implement Validator (additional complexity can be added as necessary, such as supporting validators in external assemblies or different groupings of validators).

The second (related) problem that it solves is excessively large blocks of if statements.  In a validation example, you can imagine that if it were written in-line, each validation rule would essentially map to an if statement in a large block.  In a way, it's a very useful pattern for exchanging a tiny bit of performance and memory for more modular organization of logic.  Without the CoC pattern, adding a new validation rule would mean adding another if block - yuck!  Using CoC with reflection, we can simply add another class which inherits Validator to our project and count on the component building the chain to find our class and add it to the chain.

Here is a very simple, barebones implementation:

/// <summary>
/// A simple command chain factory that doesn't do wiring.
/// </summary>
public static class SimpleCommandChainFactory
{
    /// <summary>
    /// Creates a simple list of commands to execute using reflection.
    /// </summary>
    public static List<Command> Create()
    {
        var commands = new List<Command>();

        Type[] types = Assembly.GetExecutingAssembly().GetTypes();
        Type commandType = typeof (Command);

        foreach (Type type in types)
        {
            if (!type.IsSubclassOf(commandType))
            {
                continue;
            }

            MemberInitExpression init = Expression.MemberInit(
                Expression.New(type), new MemberBinding[0]);

            Command command = Expression.Lambda<Func<Command>>(init)
                .Compile().Invoke();

            commands.Add(command);
        }

        commands = commands.OrderBy(c => c.Priority).ToList();

        return commands;
    }
}

The code checks to see if a type if a sub-type of Command and, if so, creates an instance and puts it on the chain.  The commands could then be executed like so:

internal class Program
{
    private static void Main(string[] args)
    {
        List<Command> commands = SimpleCommandChainFactory.Create();

        // Execute each command.
        foreach(Command command in commands)
        {
            command.Execute();
        }
    }
}

In this case, I'm not passing in data or checking for stop conditions (which might be useful in a validation scenario where the first failure stops processing).  To do so, you could simply pass in a single instance of some context class to each command when executing and check to see if the stop condition is true after the execute call (and break out of the for-loop).

While this pattern is immensely useful any time you find a big if or switch block that's particularly volatile, one problem that I've found with this pattern is passing parameters between two elements in the chain.  Ideally, no element in the chain should have a dependency on another element in the chain.  We want to decouple each of the elements to make it easier to build the chain dynamically.  What this means is that no element should directly set values on another element in the chain.  In essensce, to emulate the functionality of DependencyObject and DependencyProperty that we find in WF and WPF.  (Why not just use WF then?  Complexity and performance.)

At least two solutions come to mind.  The first is to pass a context with a dictionary through each element of the chain.  This would allow each element to place an output value into the dictionary and downstream components to pull these values out.  The downside of this approach is that unless you force everything into one value type (i.e. serialize to an XML string?), you can't really pass strongly typed data and now you're keyed by strings (or whatever value type) which you need to have know about in order to retrieve the value. 

A second approach would be to leverage DependencyObject and DependencyProperty.  While this sounds good in principle, it requires a mess of code to accomplish in your own code with your own objects.  Not only that, it seems to be overkill since many times, you don't need the full capabilities of the dependency system - you just want to pass a value downstream in a nice, strongly typed manner.

(There is a third approach using thread local storage, but this is probably an even worse option than the first since it wouldn't be very accessible to most developers and it doesn't really address the issue at hand.)

In the past, I've relied on the dictionary based approach.  While it wasn't ideal, it was the simplest solution that got the job done.  Deep down, I always hated this approach because I didn't like having to know the keys and having to know how to cast the results retrieved from the dictionary.  However, I recently came up with a much better solution to this issue: dynamically wired events.

We introduce two attribute classes which we can use to identify our event publishers and event subscribers.  For brevity, I'll only show the publisher attribute (they are pretty much the same in this implementation):

/// <summary>
/// Attribute used to identify event publishers.
/// </summary>
[AttributeUsage(AttributeTargets.Event)]
public class EventPublisherAttribute : Attribute
{
    private readonly string _eventName;

    /// <summary>
    /// Initializes a new instance of the <see cref="EventPublisherAttribute"/> class.
    /// </summary>
    /// <param name="eventName">Name of the event.</param>
    public EventPublisherAttribute(string eventName)
    {
        _eventName = eventName;
    }

    /// <summary>
    /// Gets the name of the event.
    /// </summary>
    /// <value>The name of the event.</value>
    public string EventName
    {
        get { return _eventName; }
    }
}

The only difference between the two in this case is the AttributeUsageAttribute.  In the case of the subscriber, we want it to apply to methods, not events.  Next, we need to apply these attributes to our concrete command types that we're going to chain.  For this example, let's say that the first item in the chain generates a GUID key that the rest of the items in the chain also need to use:

/// <summary>
/// Generates a GUID that may be needed by the rest of the chain.
/// </summary>
public class GenerateKeyCommand : Command
{
    /// <summary>
    /// Raised when a key is generated;
    /// </summary>
    [EventPublisher(EventNames.KeyGenerated)]
    public event EventHandler<EventArgs<Guid>> KeyGenerated;

    /// <summary>
    /// Executes this instance.
    /// </summary>
    public override void Execute()
    {
        Guid key = Guid.NewGuid();

        Console.Out.WriteLine("From GenerateKeyCommand: {0}", key);

        OnKeyGenerated(key);
    }

    /// <summary>
    /// Gets the priority.  A lower value indicates higher priority.
    /// </summary>
    /// <value>The priority.</value>
    public override int Priority
    {
        get { return 1; }
    }

    /// <summary>
    /// Raises the key generated event.
    /// </summary>
    /// <param name="key">The key.</param>
    private void OnKeyGenerated(Guid key)
    {
        if(KeyGenerated != null)
        {
            KeyGenerated(this, new EventArgs<Guid>(key));
        }
    }
}

As you can see, it's pretty standard stuff, with the exception of the additional attribute on the event.  Downstream, we want to handle these events in other commands:

/// <summary>
/// Simple command just for demonstration purposes.
/// </summary>
public class DoSomethingWithKeyCommand : Command
{
    private Guid _key;

    /// <summary>
    /// Executes this instance.
    /// </summary>
    public override void Execute()
    {
        Console.Out.WriteLine("From DoSomethingWithKeyCommand: {0}", _key);
    }

    /// <summary>
    /// Gets the priority.  A lower value indicates higher priority.
    /// </summary>
    /// <value>The priority.</value>
    public override int Priority
    {
        get { return 100; }
    }

    [EventSubscriber(EventNames.KeyGenerated)]
    private void HandleKeyGenerated(object sender, EventArgs<Guid> e)
    {
        _key = e.Data;
    }
}

You can see that in the event handler method, we just grab the value from the event arguments and set it on a local variable for use when Execute() is called.

Now the trick is to wire these events up using reflection to avoid creating the direct dependency between the different elements in the chain.  Spring.NET offers one way to do this using declarative events, however, it should be noted that it only works with singleton objects (this bit me in the butt until I figured it out).  Depending on the nature of your elements in the chain, that may or may not be sufficient for you.  If you need new instances every time, then we can accomplish this ourselves using a bit of reflection.

(Note that none of the code that follows has been optimized; there are a few caching opportunities to take advantage of to cut down on some of the reflection calls.)

The first step is to modify the factory method:

/// <summary>
/// Creates a command chain using reflection.
/// </summary>
/// <returns></returns>
public static List<Command> Create()
{
    List<Command> commands = new List<Command>();

    Type[] types = Assembly.GetExecutingAssembly().GetTypes();
    Type commandType = typeof (Command);

    Dictionary<string, List<EventCoupling>> eventSources
        = new Dictionary<string, List<EventCoupling>>();

    Dictionary<string, List<MethodCoupling>> eventTargets
        = new Dictionary<string, List<MethodCoupling>>();

    foreach(Type type in types)
    {
        if(!type.IsSubclassOf(commandType))
        {
            continue;
        }

        MemberInitExpression init = Expression.MemberInit(
            Expression.New(type), new MemberBinding[0]);

        Command command = Expression.Lambda<Func<Command>>(init)
            .Compile().Invoke();

        commands.Add(command);

        BuildHandlerCache(command, eventTargets, 
            type.GetMethods(_methodFlags));

        // Parse the events.
        EventInfo[] events = type.GetEvents(
            BindingFlags.Public | BindingFlags.Instance);

        if(events.Length == 0)
        {
            continue; 
        }

        BuildEventCache(command, eventSources, events);
    }

    WireEvents(eventSources, eventTargets);

    commands = commands.OrderBy(c => c.Priority).ToList();

    return commands;
}

We create two caches as we iterate through the types to hold the events and handler methods that we encounter as we iterate the types and as a final step, we wire the events together from the caches.  The cache building logic is fairly straightforward:

/// <summary>
/// Builds the handler cache.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="eventTargets">The event targets.</param>
/// <param name="methods">The methods.</param>
private static void BuildHandlerCache(
    Command command, 
    IDictionary<string, List<MethodCoupling>> eventTargets, 
    IEnumerable<MethodInfo> methods)
{
    foreach(MethodInfo method in methods)
    {
        EventSubscriberAttribute[] subscriberAttributes =
            (EventSubscriberAttribute[])
            method.GetCustomAttributes(
                typeof(EventSubscriberAttribute), false);

        if (subscriberAttributes.Length == 0)
        {
            continue;
        }

        foreach(EventSubscriberAttribute attribute in subscriberAttributes)
        {
            if(!eventTargets.ContainsKey(attribute.EventName))
            {
                eventTargets[attribute.EventName] = new List<MethodCoupling>();
            }

            eventTargets[attribute.EventName].Add(
                new MethodCoupling(method, command));
        }
    }
}

/// <summary>
/// Builds the event caches.
/// </summary>
/// <param name="command">The command.</param>
/// <param name="eventSources">The event sources.</param>
/// <param name="events">The events.</param>
private static void BuildEventCache(
    Command command,
    IDictionary<string, List<EventCoupling>> eventSources,  
    IEnumerable<EventInfo> events)
{
    foreach(EventInfo eventInfo in events)
    {
        EventPublisherAttribute[] publisherAttributes =
            (EventPublisherAttribute[])
            eventInfo.GetCustomAttributes(
                typeof (EventPublisherAttribute), false);

        if(publisherAttributes.Length == 0)
        {
            continue;
        }

        foreach (EventPublisherAttribute attribute in publisherAttributes)
        {
            if(!eventSources.ContainsKey(attribute.EventName))
            {
                eventSources[attribute.EventName] = new List<EventCoupling>();
            }

            eventSources[attribute.EventName].Add(
                new EventCoupling(eventInfo, command));
        }
    }
}

The gist of it is that we want to iterate through the events and the methods, find the ones with the attributes, map them to instances of commands, and throw them into a dictionary.  As you can see, the dictionary values are generic lists on both sides; this means that a single event can fire multiple event names and a single handler method can handle multiple events (as long as the method input types are the same).  This may or may not be useful in any given scenario, but it's easy enough to rewrite this to make it a bit simpler if it's not required.

Finally, we need to wire the events together in the chain after it's created:

private static void WireEvents(
    Dictionary<string, List<EventCoupling>> eventSources, 
    Dictionary<string, List<MethodCoupling>> eventTargets)
{
    foreach(string key in eventSources.Keys)
    {
        if(!eventTargets.ContainsKey(key))
        {
            continue;
        }

        List<MethodCoupling> targets = eventTargets[key];
        List<EventCoupling> sources = eventSources[key];

        foreach(EventCoupling source in sources)
        {
            foreach(MethodCoupling target in targets)
            {
                Delegate d = Delegate.CreateDelegate(
                    source.Event.EventHandlerType, 
                    target.Command, 
                    target.Method);

                source.Event.AddEventHandler(source.Command, d);
            }
        }
    }
}

It's as simple as that: we loop through each event (publishers) and see if there is a list of methods (subscribers) to handle it in the chain.  If so, we add a delegate as a handler to the event.  In my sample project, I created three simple command types to demonstrate; here's the output when I run my program:

You can see, once the key is generated in the first command, the value is available in the downstream commands using the events.  The nice thing is that we can add more steps to our logic without much extra work.  This is particularly handy for something like implementing a chain of validation rules as it means that you don't end up writing a big if if block.  But even in general usage, this pattern is useful for breaking out a large method into smaller, more modular pieces in a much more extensible manner.

One neat thing is that it allows you to not only wire events downstream, but also upstream as well.  This means that if an element in your chain triggers an event, code in a previous even is executed if there is a handler wired for it.

In a more complete implementation, you may consider using Spring.NET or Unity or simply .NET configuration to statically identify the elements of the chain (instead of the basic reflection I've used).  You may also consider more error handling logic ;-) and passing an instance of a context through each element in the chain.

The full sample project is available here: ChainOfCommandSample.zip (11.21 KB)

RSS 2.0 Atom 1.0 CDF