Steve's blog

  • StyleCop released

    The static analysis tool FXCop, and its later relation Visual Studio Code Analysis, have been well known as static analysis tools which help improve the quality and resilience of your code.  I'm a big fan of this tool's ability to catch bugs and potential performance or security issues sooner in the development process.

    A lesser known tool named StyleCop, which was only available internally at Microsoft up until now, analyses the source code and checks that it conforms to suggested styling guidelines.  People often have their own preferred way of styling code, which is fine for a single-man project but can make it harder to work collaboratively.

    The first time I was encouraged to use StyleCop I initally found it irritating, but you soon learn to appreciate the benefit of consistent styling across a large body of code.  Certainly the goal here isn't to preach a religious viewpoint on the One True Way of utilising curly braces, but rather ensuring that you can quickly and easily read any part of the codebase; it's surprising how much difference consistency can make to this.  It also provides further checks on code documentation such as suitable headers, which isn't covered by the other tools.

    Read more about it on the Source Analysis blog.

  • *sigh*

    Okay, I'll admit it...my definition of shortly sucks. Ten months between blog posts was very much not what I intended. All I can say is I got pulled into a couple of CAB projects for another bank which have sucked up all my time, but things have calmed down a bit and I hope to post a little more regularly now (yeah yeah, I know).

    So what momentous event caused me to start blogging again?  The announcement by Tim Sneath that .NET 3.5 Service Pack 1 is on the horizon, including the third major release of WPF.  What's more the long missed DataGrid control is making a WPF comeback in an out-of-band release.  This should make WPF a much more attractive proposition to those creating business applications; should certainly make the reams of data-binding code I've written to cope with WinForms limitations redundant.

    An interesting point a customer made to me the other day was that there's no clear guidance on where we've been, are or where we're going with smart client technologies. A lot of phrases, acronyms and codenames get bandied about without a coherent view of how they all fit together, which can be confusing for someone who's coming to it all new, especially when explaining the benefits this technology can bring to non-technical people. I'm going to have a shot at bringing it together in some sort of simple top-level diagram, so watch this space.

  • Still here, just

    So I've been on a CAB project based in Johannesburg for the last few months, which is my excuse for the woeful inattention I've paid to this blog in that time.  I hope to publish some of my learnings from the experience here shortly, but in the meantime I've been brushing up on some other interesting technologies around right now.  I've found Riemers XNA tutorials to be an excellent resource - in case you've been living under a rock for the past year, XNA is Microsoft's new game development platform targeting the Xbox 360 as well as the PC.

    The tutorials cover basics from creating your first triangle, to 3D terrain, lighting, bump mapping and many more cool things - one series of tutorials has you generating an entire combat flight sim.  Don't forget XNA is free to develop games for the PC and allows you to program games with minimum fuss using either C# or Visual Basic .NET.

  • Activating WorkItems in a TabWorkspace

    Apologies for those to whom the Smart Client Software Factory means nothing, but I thought I'd share a recent experience with regard to the TabWorkspace workspace control.  I do plan to come back and explain SCSF at a more fundamental level in later posts, but hopefully this will prove useful to anyone who has had similar issues.

    My problem related to automatically activating SmartParts and their associated WorkItems hosted in a TabWorkspace.  Controls tagged with the [SmartPart] attribute automatically activate their associated WorkItem when activated, but unfortunately switching to a tab hosting the control is not enough to fire this event.  In my particular situation I had a status bar which I wanted updated with information from the active WorkItem, so I needed a way to capure that TabWorkspace control's SelectedIndexChanged event and activate the correct WorkItem.

    Whilst the TabWorkspace control does have an ActiveSmartPart property, SmartParts don't contain any special properties for getting at their associated WorkItem.  Given I knew the exact type of SmartPart I was going to be opening in the tab pages I could have simple casted to the appropriate type and got at the WorkItem that way, but this would have introduced tighter coupling and lack of reusability.  Instead I decided to keep a register of tab pages and associated WorkItems, so I created a Dictionary<TabPage, WorkItem> collection in my ShellLayoutView class.  I then used the Event Publication/Subscription model to fire an event when a WorkItem was created, and then used that event to register the WorkItem against the TabPage it appeared in.

    [EventSubscription(EventTopicNames.RegisterWorkItem)]
    public void RegisterWorkItem(object sender, EventArgs workItem)
    {
        _registeredWorkItems[_rightWorkspace.SelectedTab] = workItem.Data;
    }
    

    Finally within the WorkItemController itself I added handlers for the WorkItem.Activated and WorkItem.Terminating events, to update the status bar of the shell application when the active WorkItem changed.  I'm still figuring all the intricasies of CAB/SCSF out and this may not have been the optimal solution, but it worked for me.  I'd love to hear from anyone who found better or alternative ways of achieving the same.

  • Using Object Builder as a singleton factory

    One of the most basic uses of Object Builder is when we want to specify that certain objects should always be created as singletons.  For example, we may require that only one instance of a DataAccess object should ever exist.  This can be specified by setting a policy, in this case a SingletonPolicy, that applies to all objects of type DataAccess.

    The following example demonstrates how this code might look, and uses the Object Builder to create a DataAccess object four times - twice with the singleton policy enabled and twice without.

    Builder builder = new Builder();
    Locator locator = new Locator();
    
    // We need a lifetime container to keep references to the objects
    // SingletonStrategy depends on this!
    locator.Add(typeof(ILifetimeContainer), new LifetimeContainer());
    
    // Enable singleton policy for DataAccess objects
    builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(DataAccess), null);
    
    SingletonPolicyDemo object1 = builder.BuildUp<DataAccess>(locator, null, null);
    SingletonPolicyDemo object2 = builder.BuildUp<DataAccess>(locator, null, null);
    Trace.WriteLine("Objects equal: " + (object1 == object2));
    
    // Clear locator
    locator = new Locator();
    locator.Add(typeof(ILifetimeContainer), new LifetimeContainer());
    
    // Disable singleton policy for DataAccess objects
    builder.Policies.Set<ISingletonPolicy>(new SingletonPolicy(false), typeof(DataAccess), null);
    
    object1 = builder.BuildUp<DataAccess>(locator, null, null);
    object2 = builder.BuildUp<DataAccess>(locator, null, null);
    Trace.WriteLine("Objects equal: " + (object1 == object2));
    

    Running this code will show that the singleton pattern is used for the first pair of references, and individual objects are generated for the second pair.  The locator object contains weak references to the objects that are created, and when the singleton policy is in effect this is searched to see if this object has already been created.  However since only weak references are used, we need to create a LifetimeContainer to maintain strong references to the created objects and prevent them from being garbage collected.

  • Changing the default using directives in Visual Studio

    The other day I was working on a demonstrator application for the Object Builder framework, and adding a lot of new classes to my project.  Having to manually add using directives for OB in every class didn't seem terribly efficient, so I set about finding how you can change the default C# class template.  I discovered this nugget in Anson Horton's blog - if you open %Program Files%\Microsoft Visual Studio 8\Common7\IDE\ItemTemplates\CSharp\1033\Class.zip, you can modify the class.cs file within that's used to generate all new C# source files - it looks like this:

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace $rootnamespace$
    {
    	class $safeitemrootname$
    	{
    	}
    }
    

    You can then add or remove the using directives you want at the top of this file, and save it back to the archive.  Finally run %Program Files%\Microsoft Visual Studio 8\Common7\IDE\devenv.exe /setup to refresh Visual Studio's template cache.  Now all new C# files you create should match your modified template.

  • Pretty Patterns

    The Patterns and Practices team here provide a host of industry tested guidance on software development for the Microsoft platform, as well as software factories, application blocks and reference implementaions.  The current project I'm involved with is examing the Composite UI Application Block for creating rich smart client applications.

    This block allows the developer to easily create complex user interfaces from simpler reusable parts, and provides an MVP pattern implementation from which to develop.  Additional features include dynamic loading of independent but cooperating modules, event brokering and thread marshalling for loosely coupled communications, and a framework for pluggable infrastructure services.

    The Composite UI Application Block makes use of Object Builder, a Dependency Injection (DI) framework also originally written by the P&P group. Object Builder can be used to manage complex dependencies between objects and decouple objects in complex systems. There is a detailed webcast on Object Builder which is a good place to start understanding what it does for us and how it works.

  • New job, new blog

    Welcome to my shiny new blog! I've recently moved to MCS (Microsoft Consulting Services) UK from DPE (Developer and Platform Evangelism), so having talked about how great all these products are I now have to drink my own medicine and use them to solve real-world problems. My old blog is now retired, but shall remain preserved for the forseeable future.

    I'm currently working on a project for a large organisation in South Africa, and looking at a whole bunch of new technology that will enable their business to run even better. Stuff we're looking at include the whole .NET 3.0 stack (Windows Presentation Foundation, Windows Communication Foundation, Workflow Foundation), as well as the Composite UI Application Block, the Offline Application Block and the Smart Client Software Factory. Far too much to look at in one blog entry, but I hope to spend time on each of them in the coming posts.


© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker