You are browsing the archive for 2010 June.

52 Weeks to a Pragmatic Programmer: Challenge 2

June 21, 2010 in Pragmatic Programmer

Take Responsibility

Tip 3 in The Pragmatic Programmer states:

Provide Options, Don’t Make Lame Excuses

For one day keep track of what you say in meetings, on the phone, or through e-mail and categorize them as:

  • Reasonable option/solution
  • Lame excuse

You don’t have to share your list, but blog/comment about the experience.

Infragistics XamWebOutlookBar & Caliburn

June 14, 2010 in Silverlight

Last week I set out to build a simple Caliburn sample that included the Infragistics Silverlight XamWebOutlookBar.  I made progress and got to a point where Rob Eisenberg was able to help me get the rest of the way.  The approach I was taking was trying to figure out what conventions I should add to get Caliburn to recognize the XamWebOutlookBar.  Rob’s answer was that not everything needs to be a convention.

When you run the application, you will see the XamWebOutlookBar hosted in a sidebar.  Both groups shown in the OutlookBar have corresponding ViewModels that in turn have associated Views:

image

At the end of the article is a link to the project source code.  In order to get the project to compile, you will need to download the Infragistics assemblies and add references to the following:

image

A trial of Infragistics can be downloaded from here.

Here is the final project structure:

image

Application

In typical Caliburn style, the App.xaml has been changed to a CaliburnApplication:

<cal:CaliburnApplication xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
             xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
             xmlns:cal="clr-namespace:Caliburn.PresentationFramework.ApplicationModel;assembly=Caliburn.PresentationFramework"
             x:Class="InfragisticsCaliburn.App">
    <Application.Resources>
    </Application.Resources>
</cal:CaliburnApplication>

In App.xaml.cs, we setup MEF as the container and the use MEF to find the ShellViewModel:

protected override IServiceLocator CreateContainer()
{
    return new MEFAdapter(CompositionHost.Initialize(new AssemblyCatalog(Assembly.GetExecutingAssembly())));
}

protected override object CreateRootModel()
{
    var shell = ServiceLocator.Current.GetInstance<IShell>();

    if (IsRunningOutOfBrowser && HasElevatedPermissions)
        MainWindow.Closing += (s, args) => { args.Cancel = !ServiceLocator.Current.GetInstance<IShell>().CanShutdown(); };

    return shell;
}

ViewModels

MEF is able to find ShellViewModel in the container using the IShell type because it implements IShell and Exports itself as IShell:

[Export(typeof(IShell))]
public class ShellViewModel : ScreenConductor<IScreen>, IShell
{
    private readonly MainViewModel _firstScreen;

    [ImportingConstructor]
    public ShellViewModel(MainViewModel firstScreen)
    {
        _firstScreen = firstScreen;
    }

    protected override void OnInitialize()
    {
        this.OpenScreen(_firstScreen);
    }

}

Because of the ImportingConstructor attribute, an instance of MainViewModel is created.  The class is implemented as:

[Export(typeof(MainViewModel))]
public class MainViewModel : ScreenConductor<ISidebarItem>.WithCollection.OneScreenActive
{
    [ImportingConstructor]
    public MainViewModel([ImportMany] IEnumerable<ISidebarItem> sidebars)
    {
        Screens.AddRange(sidebars);
        this.OpenScreen(Screens.First());
    }
}

For the XamWebOutlookBar implementation, the important thing to note is that MainViewModel is setup as a ScreenConductor that manages ISidebarItem instances where only one sidebar screen is active at a time.  The ImportingConstructor attribute then finds all items registered in the MEF container as ISidebarItem.

In this example, both ContentAViewAModel and ContentBViewModel are similar:

[Export(typeof(ISidebarItem))]
public class ContentAViewModel : Screen, ISidebarItem
{
    string message = "ContentA is working";

    public ContentAViewModel()
    {
        DisplayName = "Content A Header";
    }

    public string Message
    {
        get { return message; }
        set
        {
            message = value;
            NotifyOfPropertyChange(() => Message);
        }
    }

    public string Icon
    {
        get { return @"../../Images/briefcase2.png"; }
    }
}

Each of the ViewModels export themselves as ISidebarItem, inherit from Screen, and implement ISidebarItem.

Views

In MainView.xaml, the DisplayName and Icon properties are used to render the group’s header and icon:

<igOB:XamWebOutlookBar x:Name="sidebarOutlookBar" 
        MinimizedWidth="35" AllowMinimized="True" 
        NavigationPaneMinimized="sidebarOutlookBar_NavigationPaneMinimized"
        NavigationPaneExpanding="sidebarOutlookBar_NavigationPaneExpanding"
        VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
        GroupsSource="{Binding Screens}">
    <igOB:XamWebOutlookBar.GroupContentTemplate>
        <DataTemplate>
            <ContentControl cal:View.Model="{Binding}" />
        </DataTemplate>
    </igOB:XamWebOutlookBar.GroupContentTemplate>
    <igOB:XamWebOutlookBar.GroupHeaderTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding DisplayName}" />
        </DataTemplate>
    </igOB:XamWebOutlookBar.GroupHeaderTemplate>
    <igOB:XamWebOutlookBar.DefaultLargeIconTemplate>
        <DataTemplate>
            <Image Source="{Binding Icon, Converter={StaticResource stringToImageSourceConverter}}"/>
        </DataTemplate>
    </igOB:XamWebOutlookBar.DefaultLargeIconTemplate>
    <igOB:XamWebOutlookBar.DefaultSmallIconTemplate>
        <DataTemplate>
            <Image Source="{Binding Icon, Converter={StaticResource stringToImageSourceConverter}}"/>
        </DataTemplate>
    </igOB:XamWebOutlookBar.DefaultSmallIconTemplate>
</igOB:XamWebOutlookBar>

Notice the GroupsSource property is bound to the Screens property that the ViewModel inherits from its base class.  The ContentControl for each group is set to the ViewModel representing the group.  The group header is bound to the DisplayName property and the small and large icons are set to the Icon property.

The Message property is bound in the View for the group content:

<UserControl x:Class="InfragisticsCaliburn.Views.ContentAView"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:xhttp://schemas.microsoft.com/winfx/2006/xaml
    xmlns:d=http://schemas.microsoft.com/expression/blend/2008
    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock x:Name ="Message"/>
    </Grid>
</UserControl>

Download the source of the project here.

52 Weeks to a Pragmatic Programmer: Challenge 1

June 14, 2010 in Pragmatic Programmer

Care About Your Craft

For you, what defines the craft of programming and what can you do show that you care about your craft?  Make a list of ways to show quality in your craft and pick one to improve on this week.

The craft of programming for me includes tools, knowledge, skills, experience, and interactions.  As a Microsoft developer, my primary tool is Visual Studio 2010 with its editor, debugger, designers, and add-ins.  There are additional tools that I use regularly such as Blend, Reflector, Silverlight Spy, Fiddler, and LINQPad.  I glean knowledge from blogs, books, magazines, MSDN, forums, and tweets. Every day I learn something new about C#, Silverlight, Azure, WCF RIA Services, MEF, MVVM, or some other pattern, methodology, or tip.  Each day is a new experience and opportunity to hone my skills.

I talk to (at watch) my co-workers to see how they approach different situations.  Two examples.  I’ve worked with a programmer that handwrites code using pen and paper before his hands touch the keyboard.  He doesn’t do this for every line of code.  I think it is his way of solving more challenging problems.  Another former co-worker keeps notes in spiral-bound notebooks.  The pages contain answers to questions, how he approached a problem.  Things he tried that didn’t work.  Sometimes I would ask a question about how to do something or why a particular choice was made on the project.  He would pull out a notebook and find the appropriate entry.

I feel the craft of programming has a lot to deal with people.  How you interact with your co-workers, how you mentor someone new to a project or technology, how you deal with a client.

Here is a list of some things that I think shows that a programmer cares about his craft:

  • Understand requirements, ask clarifying questions
  • Have a broad knowledge of technologies
  • Know the ins and outs of your tools
  • Write unit tests, practice TDD
  • Comment code
  • Use code analysis tools such as FxCop
  • Look for code smells and refactor
  • Be prepared for meetings and be on time
  • If you are leading the meeting, send out an agenda at least the day before
  • Be precise in answering the 3 questions for the daily Scrum standup
  • Add tasks to TFS before working on them, update items regularly

 

This week I am going to explore new features of Visual Studio and incorporate some of them into my daily programming.

What are you going to do this week to show that you care about the craft of programming?

Am I a Pragmatic Programmer?

June 9, 2010 in Pragmatic Programmer

As promised, I will be transparent throughout the 52 Weeks to a Pragmatic Programmer challenge.  Here are my responses to the pre-assessment.

Scale: 0 – Strongly Disagree, 1 – Disagree, 2 – Neutral, 3 – Agree, 4 – Strongly Agree

  1. I am an early adopter and love trying new things. 4 – Strongly Agree
  2. I am a fast adapter. When given something new I grasp it quickly and am able to integrate it with the rest of my knowledge.  3 – Agree
  3. I am inquisitive.  I tend to ask lots of questions about how to do things or why they work the way they do. 3 – Agree
  4. I am a critical thinker. I seek to get the facts and not just accept a statement as truth. 3 – Agree
  5. I am realistic.  I understand that there is no such thing as an ideal day.  Often things are harder and take longer than expected. 3 – Agree
  6. I am a Jack of all trades.  I strive to keep abreast of new developments and a broad range of technologies and methodologies even though on my current project I may be a specialist. 2 – Neutral
  7. I see programming as a craft and I care about doing my craft well. 3 – Agree
  8. I think about my work and think about ways to improve. 3 – Agree

Now that I took the pre-assessment, I realize that some of my Agree responses would have been better answered by a Somewhat Agree if the choice had been available.  Overall, I think I possess the qualities mentioned above but I definitely have room for improvement.  At my previous employer, I tended to focus on only a few main technologies whereas now that I am consulting I am exposed to much more.  It is a welcome change.

Rate your overall level as a pragmatic programmer on a scale of 1-10 (1 being Journeyman and 10 being Master)

This question is hard to answer.  I have 14 years experience with software development so shouldn’t I be a Master by now?  But we are talking about being a pragmatic programmer.  I likely have much to learn about being pragmatic as these weekly challenges will demonstrate.  So I am going to rate myself more on the Journeyman side at a 4.

I hope others join in on this adventure.  If you do, please post or comment so that I don’t feel like I am doing this alone.

Thanks,

Mark

52 Weeks to a Pragmatic Programmer: Pre-Assessment

June 8, 2010 in Pragmatic Programmer

Before we get going on the 52-week challenge of becoming a pragmatic programmer, it might be helpful to do a self-assessment to judge where you are today.

Scale:

0 – Strongly Disagree

1 – Disagree

2 – Neutral

3 – Agree

4 – Strongly Agree

Rate yourself on the following statements:

  1. I am an early adopter and love trying new things.
  2. I am a fast adapter. When given something new I grasp it quickly and am able to integrate it with the rest of my knowledge.
  3. I am inquisitive.  I tend to ask lots of questions about how to do things or why they work the way they do.
  4. I am a critical thinker. I seek to get the facts and not just accept a statement as truth.
  5. I am realistic.  I understand that there is no such thing as an ideal day.  Often things are harder and take longer than expected.
  6. I am a Jack of all trades.  I strive to keep abreast of new developments and a broad range of technologies and methodologies even though on my current project I may be a specialist.
  7. I see programming as a craft and I care about doing my craft well.
  8. I think about my work and think about ways to improve.

 Rate your overall level as a pragmatic programmer on a scale of 1-10 (1 being Journeyman and 10 being Master)

Let the journey begin.

52 Weeks to a Pragmatic Programmer

June 8, 2010 in Pragmatic Programmer

More than 10 years ago Andrew Hunt and David Thomas published a book called, The Pragmatic Programmer: From Journeyman to Master

I came across it again a few months ago when I emptied my bookcase at work.  I thought that it would be a good chance to re-read the book and blog about it.  Eleven years ago I was programming in a different language, using different tools, and using a different methodology.  I am also a consultant now instead of a full-time employee so my perspective has changed some.

The forward by Ward Cunningham concludes:

So here it is: an easy to read–-and use-–book about the whole practice of programming.  I’ve gone on and on about why it works.  You probably only care that it does work. It does. You will see.

In the Preface, the authors promise that this book will help to make you a better programmer by helping you become more effective and productive.  They argue that programming is not just a job, but a craft. 

When I think of craftsmen, I often think of my carpenter ancestors who used tools, knowledge, and experience to construct projects.  They used their hands to make things.  So do I.  Instead of hammer and saw, I use keyboard and mouse. I use experience from past projects or gleaned from conversations with others or blog posts.  I gain knowledge by reading books and blogs and tweets, watching presentations, following tutorials, and attending conferences.

This book takes a “hands on”, doing approach.  It is not idealistic.  It is practical and sensible.  In one word, it is pragmatic.

In this blog series, each week I will present a challenge based on a tip or topic from the book.  I will do the challenges myself but also ask you to do them as well.  It seems the pragmatic thing to do after all.  If you have a blog (or are thinking about starting one) then blog about your experience with each of these challenges.  Otherwise, the comments section is always open.

Take the challenge and see if this time next year you are not a more pragmatic, productive programmer.  I dare you.

But first, if you don’t own The Pragmatic Programmer then purchase a paperback or digital copy:

Caliburn Book: Chapters 1-3

June 7, 2010 in Silverlight

Many months ago I put learning about the Caliburn framework on my “to do” list.  A few weeks ago I started the task and decided to write a book about it.  I don’t think that I am expert enough at the framework to actually publish a book.  Not yet anyway.  But I figured if I could grok it well enough to write about it that I would be well on my way to learning the framework. 

You can download the book from here.

The first chapter is a simple introduction to Caliburn.  Chapter 2 walks you through a simple, Hello Caliburn, application.  Chapter 3 is more ambitious and walks you through various aspects of the framework using the Silverlight GameLibrary sample.  It covers the same topics that Rob Eisenberg covered in his MIX10 talk.  There is also an Appendix included that lists some of the conventions found in Caliburn.

If I decide to write additional chapters, then I would include:

  • Adding to Conventions
  • Overriding Conventions
  • TDD and Caliburn
  • Caliburn extensibility points
  • IResult implementations
  • Coroutines in more depth
  • UI patterns besides MVVM

There is likely much more to Caliburn that I don’t even know what I don’t know yet.

Please reply with any feedback, corrections, further explanations, or additional chapter ideas.

If a book is ever published about Caliburn, Rob should be the one to do it.