Friday, May 7, 2010

Favourite Developer Tools Demo

7 May 2010


Today I demo'ed to the dev team a list of tools I have found very useful over the last few years. Here's a rundown in no particular order.

  • Code Metrics in Visual Studio
  • Xaml Formatter
  • Regionerate
  • StyleCop
  • Resharper
  • NCover
  • NUnit Gui
  • Ghost Docs
  • Reflector
  • NDepend
  • XamlPadX



Visual Studio Code Metrics Tool
Its only available in Premium and Ultimate. Its accessed from the Other Windows option in the view menu.



It gives some common / basic statistics about your code. Cyclomatic Complexity is a measure of the number of pathways through your code, this increases the number of nested conditional statements you have. It is generally accepted that this number should be less than 20 for a member. Class coupling: I read on on a wiki somewhere this should be absolutely no more than 30. This seems high to me. Suffice to say the less the easier it is to maintain and test. Inheritance should be less than 6. Check out Wikipedia  and other top google results for tolerance recommendations.


Use this analysis process to improve the usability and maintainability of your code. The simplier one method is, the easiser it is to test and consume.


Xaml Formatter
It does exactly what it says on the tin.






It puts attributes on their own line unless there is only one, and alphabetises the attributes. It will also point out "possibly" unused x:Name and x:Keys for review.
This is a very basic tool (that needs work, and I believe Craig volunteed ;-) ), so its free, see my Essential Tools page for downloading.


Regionerate
It sorts and arranges members within a class. This is a fantastic tool and definitely in my top 3 favourites. See the Regionerate page for more info. Its a free tool, but Rauchy would really like to eat so donate him a pizza.

From ugly to ...



Ah, Pretty.


See my Essential Tools page for downloading a customised style sheet that is compatible with StyleCop.


Keeping your code in a consistent predictable order will make it more approachable, easier for others to learn it, and maintain it. Not to mention those of us with bad memories and we forget what it does after 2 weeks, you know who you are... ;-)


StyleCop
This is a great tool to advise and enforce a consistent style across all your code. See here for more info the Stylecop site and the StyleCop for Resharper  site. It used to be known as FxCop and was originally designed and written by one of the senior Microsoft CLR Architects. Its totally free.
This is a must have for use along side the built in Code Analysis built into Visual Studio premium. See my Essential tools summary page for a link to a stylecop config file almost fully compliant with the  Microsoft Framework Design Guidelines (or even better still buy the book).


This is a snippet of code where you can see the warnings generated by Stylecop in the Errors/Warnings task list, and also the integration into Resharper giving the green underline indicating a Stylecop warning present on that line.







Resharper
Resharper is an enormous cache of goodness that will have you screaming like a 3year old when its taken away. For more information see the JetBrains site. This is another that is easily in my top 3 favourite tools. I've been using it for more than 3 years now and I'm addicted. Believe the hype on the Jetbrains site, you easily recoup the cost of the licence inside 4 weeks in productivity savings. Here's a list of my favourite features.



Renaming: The Refactor-Rename feature will rename comments (optionally) automatically rename references in xml member documentation comments and related members. Ie: if you rename a property it will ask if you would like to rename its backing field also.



Type Navigation with Ctrl+T - easy simple and obvious.



Suggested fixes, actioned by pressing Alt+Enter+Enter. All default actions can be performed against the entire solution with the Code Clean Up Tool.



Excellent Unit Testing interface.



Superior Intelli-Sense.



Find Usages of a member or type across an entire solution.


This is but a small subset of the features in this very easy to use tool.


NCover
I have used this tool previously for better code coverage feedback and stats. Read their  web site for more detail. My exposure to it is limited; I've used it mostly through a add-in to Visual Studio called  Test Driven.Net. The amount of statistics recorded is superior to what you get out of the box with Visual Studio, and includes a graph showing your test passes over time and code coverage stats over time. Its not a free tool, but as stated provides far superior stats than Studio does.





NUnit Gui

A nice simple interface for running tests. It pulls in source code if it finds it on your local drive and Pdb's are present. Can be scripted into the build process. Also an easier to use tool for testers to ensure all tests pass before they start their own tests. See their web site for more info.It has the ability to run tests in parallel (unlike Resharper's unit test runner). Tests can also be given time out values as attributes, Resharper's test runner also ignores these. NUnit is an open source program and has been easily the most popular unit testing framework for .Net.


Ghost Docs
This is a fantastically simple and surprisingly clever tool. Its a free tool and you can download it here. It will add xml documentation comments to members and type on key combination (Ctrl+Shift+D by default). Normally it will make a surprisingly good guess at a summary description for your method. It will even pull down comments from a base class or interface. At the very least it will insert a template allowing quick entry of documentation.

This shows the default the xml documentation for the ToString override. It has pulled this comment from the System.Dll assembly.


Reflector

This is the other tool easily in my top 3 favourite tools. Its invaluable for easily finding types and members, your search can be an exact match or a partial. It also shows full xml comments for every type and member.
Probably the best feature is its ability to disassemble an assembly back into C# code. This is a great learning and debugging tool. I believe the pro edition is able to allow you to debug into a third party assembly although I have never tried. There is a free edition and a Pro edition that you can licence for a small fee considering the power, here's the web site.





NDepend
This is a very extensive tool, there's far more to say about it than just 2 or 3 sentences. Here's their website.

Suffice to say that it is an analysis tool for looking at types and assemblies at a high level. I have found it to be useful for two purposes: discovering and analysing an assembly or solution written by someone else upon which I am expected to add to. Secondly, its very useful to add polish to your own framework API's. It makes it easy to find members that should not be public, unused, or are too complex. I quite often use the"egg" diagram in Cyclomatic complexity mode (as a unit of measure instead of the default Lines of Code) this immediately shows me large sections that I need to refactor into more maintainable code.




The above diagram shows the "Egg" diagram in lines of code mode. You can see an excessively large method in the top left of the diagram. Sometimes lines of code is not the best measure of size of a method. Lots of lines of code might (emphasis on might) be acceptable but certainly not too much complexity. Thats where changing the mode to Cyclomatic complexity is very useful.


When refining and polishing a reusable DLL or framework I find take a good close look at the extensive report ndepend produces. In particular the grid that highlights any statistic that is in the top 15% for that stat for the DLL. These top 15% stats are highlighted in pink. Just because they are highlighted doesn't mean they are a problem, just need to be considered. This is the only complaint I have against this otherwise awesome tool. There is no way to annotate these and possibly ignore them for the next time the analysis is done.



Finally, the last feature I want to show is the CQL language built into ndepend. It gives you SQL like query syntax across your libraries looking for certain stats. For example:

You can see the CQL statement in the top left pane and the results in the below pane and also visually highlighting the selected methods. Custom queries can be built into the analyse run process.
This tool is not free, see their website for more info.


XamlPadX
This tool has limited use in my opinion but I have used it religiously for playing around with snippets of xaml and for extracting the xaml style code from the default Microsoft controls for control customisation. I quickly find that I would rather take my snippet of xaml I am working on and copy it into a Visual Studio test project instead. But Studio does not have support for extracting control styles like XamlpadX does. I did find some source code on the net to perform just this function however, but in this case I'm I'd rather not reinvent a good working tool.







This is a free tool, here's the link.


And thats it. Go forth and multiply.

Wednesday, April 28, 2010

Mvvm Resources

Here is a great list of Mvvm (Model, View, View-Model pattern) resources:

  1. Josh Smith's MSDN article on MVVM (Good introduction of Mvvm)
  2. Karl Schifflett's Begining to Explore WPF's MVVM and another validation demo, and one more, from Karl (good demo of validation and presentation of validation errors)
  3. Dan Crevier's Data-Model, View, View-Model series (nice explanation and demo of commands)
  4. Josh Smith's Using Tree View with Mvvm (and easy searching code)
  5. Josh Smith's Controlling focus from a view-model
  6. Josh Smith & Karl Schifflett Creating an Internationalised Wizard
  7. Karl Schifflett's BBQ Shack Demo Application (Excellent demo of what I would call a "marshalled" navigation strategy, and also Windows ALT-TAB style task switching).
  8. Another Josh Smith Article on Testing where the subject under test uses a Dispatch Timer.

Monday, April 19, 2010

Preventing IoC Configuration Chaos

After working with tightly coupled (read as "welded") existing code bases in many previous companies, when given a green fields opportunity, I want to ensure thorough use of IoC.  This is essential to allow automated testing. Automated testing is essential for efficient agile development.

The problem is, when you write a large system, that equates to a large config file.  Actually it will be an enormous config file.  This is unacceptable in my opinion for several reasons:

  1. Large XML / Config files are annoying to maintain, and easy to get wrong.
  2. There's no compile time checking and fast feedback for XML / Config files.
  3. Most of the time the config is static, no one will want to change it at runtime for a production system (excluding unit testing).
  4. The config is owned and stored in the app/web config but some of your DLL's are designed to be reused independently and whenever they are consumers need to find an example of config and copy and paste it.
Nasty.
So a good solution needs the following attributes:
  1. By default (with no consumer code or config) any individual DLL must own its own default config. If a consumer takes and consumes your DLL that's all they need.
  2. By default the IoC container must be preconfigured to return production instances.
  3. Any hard coded default must be override-able by config.
  4. It must be easy to trigger IoC container intitialisation on application startup.
  5. Ideally the consumer shouldn't have to maintain a list of DLL's to intitialise.
Here's some sample code that I have come up with that attempts to solves these with StructureMap.
The basic idea is that all interfaces and abstracts registered with IoC for the primary purpose of unit testing should be configured with code in a DLL specific "Startup" class.  Each Referenced assembly in the current AppDomain is "visited" using a psuedo-visitor pattern and its Startup class is located and executed.

Each assembly that contains IoC registrations that need to evaluated must be decorated with this custom attribute.

[assembly: AssemblyInitialization(typeof(MamalsStartup))]
This points to the startup class containing the code registrations. Here's an example of a Startup implementation:

public class MamalsStartup : IStartup
    {
        private readonly object syncRoot = new object();

        public bool IsInitialized { get; private set; }

        public void InitializeObjectFactory()
        {
            if (this.IsInitialized)
            {
                return;
            }

            lock (this.syncRoot)
            {
                if (this.IsInitialized)
                {
                    return;
                }

                var factory = ObjectFactory.Container;
                factory.Configure(config =>
                {
                    config.For<ICat>().Use<Cat>();
                    config.For<ICat>().Use<Cat>().Named("Felix").OnCreation(c => c.Name = "Felix");
                    config.For<IDog>().Use<Dog>();
                    config.For<IChapter>().Use<Chapter1>();
                });

                FrameworkInitialise.ObjectFactoryInitializeCompleted += this.OnObjectFactoryInitializeCompleted;
                this.IsInitialized = true;
            }
        }

        public void Shutdown()
        {
            // Trigger any shutdown / cleanup logic
        }

        private void OnObjectFactoryInitializeCompleted(object sender, System.EventArgs e)
        {
            // You can do any singleton registration here. Or any registrations that require other registrations have been completed.
            FrameworkInitialise.ObjectFactoryInitializeCompleted -= this.OnObjectFactoryInitializeCompleted;
        }
    }
Notice how I have made an event that fires once all IStartup.InitializeObjectFactory this is to allow complex registrations that require instances of standard objects or singletons to be available.  It also useful for kicking off any other intialisation that may be required.  There's also a ShutDown method, this is useful for tidying up any singleton registrations that implement IDisposable or other cleanup logic.

To trigger the whole process the consuming code only needs one line of code:


// Invoke Initialise to trigger the process of "visiting" all assemblies referenced and calling each assemblies
// startup class to configure the IoC container.
FrameworkInitialise.Initialise();

Here's the initialize method:

/// <summary>
        /// Initializes the AppDomain assemblies that have the <see cref="AssemblyInitializationAttribute"/>.
        /// </summary>
        public static void Initialize()
        {
            if (isInitialised)
            {
                return;
            }

            isInitialised = true;
            var current = AppDomain.CurrentDomain;
            current.AssemblyLoad += OnAssemblyLoad;
            current.ProcessExit += ProcessExit;

            var assemblies = (from assemblyName in Assembly.GetEntryAssembly().GetReferencedAssemblies()
                              where assemblyName.FullName.StartsWith(AssemblyPrefix, StringComparison.InvariantCultureIgnoreCase)
                              select Assembly.Load(assemblyName))
                             .AsParallel();

            var list = new Dictionary<int, AssemblyInitializationAttribute>();
            FindInitAttribute(assemblies, list);

            // Finding and accessing the AssemblyInitializationAttribute will instantiate it and its ctor will trigger that assembly's startup class.
            // By the time this point is reached all assemblies have had their Object Factories configured.
            var handler = ObjectFactoryInitializeCompleted;
            if (handler != null)
            {
                // Some Assemblies may be interested to be informed when all others have had their Object Factories configured.
                // This will allow them to perform any other intialisation code that requires dependent assemblies to be configured first.
                handler(null, EventArgs.Empty);
            }
        }

Sunday, April 11, 2010

WCF Callback aka Duplex

Resources:

In the code sample a IPC (inter-process communication) named pipe is used to communicate between client and service using a callback.  Named pipes are recommended for use when communication is only between processes on the same machine.  Here is a chart outlining selection of a binding from Soledad Pano's blog sourced from Juval Lowry's book Programming WCF Services.


There is only really two things to get right with WCF. One is configuration, and the other is serialisation of data.  Serialisation is a big topic on its own, but in my experience its best to make use of DTOs, and avoid the use of generics in DTOs.  DTO's are essentially specific objects created to transfer data from one place to another.  They only contain data properties.

Configuration:
Here is a sample WCF Server app.config. 

    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <configuration>
    3   <system.web>
    4     <compilation debug="true" />
    5   </system.web>
    6 
    7   <system.serviceModel>
    8     <services>
    9       <service
   10           name="WCFCallbacks.MessageService"
   11           behaviorConfiguration="WCFCallbacks.MessageBehavior">
   12         <host>
   13           <baseAddresses>
   14             <add baseAddress = "net.pipe://myservice" />
   15           </baseAddresses>
   16         </host>
   17         <endpoint
   18             address =""
   19             binding="netNamedPipeBinding"
   20             contract="WCFCallbacks.IMessage">
   21           <identity>
   22             <dns value="localhost"/>
   23           </identity>
   24         </endpoint>
   25         <endpoint
   26             address="http://localhost:8001/myservice"
   27             binding="mexHttpBinding"
   28             contract="IMetadataExchange"/>
   29       </service>
   30     </services>
   31 
   32     <behaviors>
   33       <serviceBehaviors>
   34         <behavior name="WCFCallbacks.MessageBehavior">
   35           <serviceMetadata httpGetEnabled="False"/>
   36           <serviceDebug includeExceptionDetailInFaults="True" />
   37         </behavior>
   38       </serviceBehaviors>
   39     </behaviors>
   40   </system.serviceModel>
   41 </configuration>


Config settings can also be set using the SvcConfigEditor.exe from within Visual Studio.

Here's a sample Client Config file:

    1 <?xml version="1.0" encoding="utf-8" ?>
    2 <configuration>
    3 
    4     <system.serviceModel>
    5         <bindings>
    6             <netNamedPipeBinding>
    7                 <binding name="NetNamedPipeBinding_IMessage" closeTimeout="00:01:00"
    8                     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
    9                     transactionFlow="false" transferMode="Buffered"transactionProtocol="OleTransactions"
   10                     hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
   11                     maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
   12                     <readerQuotas maxDepth="32" maxStringContentLength="8192"maxArrayLength="16384"
   13                         maxBytesPerRead="4096" maxNameTableCharCount="16384" />
   14                     <security mode="Transport">
   15                         <transport protectionLevel="EncryptAndSign" />
   16                     </security>
   17                 </binding>
   18             </netNamedPipeBinding>
   19         </bindings>
   20         <client>
   21             <endpoint address="net.pipe://myservice/" binding="netNamedPipeBinding"
   22                 bindingConfiguration="NetNamedPipeBinding_IMessage"contract="MessageService.IMessage"
   23                 name="NetNamedPipeBinding_IMessage">
   24                 <identity>
   25                     <dns value="localhost" />
   26                 </identity>
   27             </endpoint>
   28         </client>
   29     </system.serviceModel>
   30 </configuration>

It is recommended to only use config files to configure a service that needs to be changed at runtime. If for example the service does not need designed to be changed at runtime, rather programmatically set the service properties.

Hosting:
Options include:
  • IIS
    The advantage of using IIS, is management of service life cycle, and starting up on first call etc.  Only Http(s) bindings can be used.
  • WAS
    The Windows Activation Service can host any WCF service using any binding and still has all the advantages of IIS's life time management.  The WAS feature is only available in Vista and above.
  • Self Hosting using the ServiceHost class.
    This gives the developer the ability to host within a Windows service, console, or any other application.  The ServiceHost class is not available in the Client framework; ie not available in Silverlight.

Consuming a service client side there may be more limitations based on client environment.  If hosting within a Silverlight application, only Http(s) bindings are available, and wsDualHttpBinding is unavailable meaning callbacks are not possible.