in

Dé specialist in .NET trainingen en consultancy

Thomas Huijer

  • Modifying the generated code from RIA Services

    Today at a client I had the need to modify the code that RIA Services generates. It is certainly possible, but not as easy as modifying a T4 template. It turns out you have to derive a class from System.ServiceModel.DomainServices.Server.CodeProcessor. If you put the DomainIdentifierAttribute on your DomainService class, you can specify your own CodeProcessor.

    The CodeProcessor gets an tree with the CodeDOM of the code that is going to be generated. The MSBuild task responsible for generating the code, hands that to your CodeProcessor so you can add, remove or change anything you want. See an example of a CodeProcessor here.

    I’ve read somewhere that the next version of RIA Services is going to support T4 Templates, but for now I can live with it.

  • Configure Windows to Log in to TFS Automatically

    After typing my TFS credentials about a million times, today I finally took the time to figure out how to log on to TFS automatically if the PC that I’m working on is not part of a (or not the same) Active Directory than the TFS server is. Turns out it’s pretty simple: http://blog.benday.com/archive/2007/10/10/23162.aspx

    Why haven’t I searched for that before?

  • Custom Dictionary for Code Analysis

    At the DevDays 2010 in my session on Building Maintainable Applications, a question came up how to add custom words to Code Analysis. I’ve done that a couple of times, but couldn’t remember it from the top of my had. So, this is just a note-to-self.

    1. Add a XML file (e.g. CustomDictionary.xml) to your project
    2. Set the Build Action to CodeAnalysisDictionary
    3. Set Copy to Output Directory to Do not copy

    If you put the xml file in the root install folder of Visual Studio, it’ll be applied to all projects. Alternatively, you could share a dictionary file using the Add as Link when adding files to a project.

  • Software Quality: External versus Internal

    I’m doing quite a lot of software quality audits lately. Some customers want to know what the quality is of a product that was developed off-shore. Some just want to know how long they should keep developing the current version or if they should start developing a new version in a new environment. Often products that have been around for long have architectures that are not kept into shape. Or new requirements that just won’t fit into the current architecture. Of requirements that are just too hard to do in the current environment, for example communicating with advanced webservices that require certificates, SSL, custom authentication or reliable sessions. For products developed with for example VB6, this is just extremely time-consuming to develop. While this is plain easy in WCF.

    Anyway, whatever the reason is that customers have to request an quality audit, I always first explain to them the difference between what I call the external and internal quality of a software product.

    • External quality
      This is the quality of the product that is perceived by the users of the product. This includes the stability of the product, the ease of use of it, the interaction design and the way it conforms to the requirements
    • Internal quality
      This is the quality of the source code, the quality of the architecture, the quality of the development process around the product, etc. Anything that is not perceivable by the users of the software.

    We at Oosterkamp training | consultancy are specialized in auditing the internal quality of software. We have developed our own techniques for quickly and accurately determining the internal quality of software. For example, we look at how clean the code is. We also inspect software for possible defects in software using IfSQ. We look at the architecture of the software, the consistency of used solutions, etc., etc.

    Call us if you’re interested in an audit or are interested in reviews of audits we did for our customers.

  • MVP C# in 2010

    I was notified today by email that Microsoft awarded me the 2010 Microsoft MVP Award for C#.

    Just great!

  • MSTest: Cannot start more than one local run

    When I run my tests in Visual Studio and one or more tests fail, I hit the Debug Selected Test so see what is going on. But at times, I get this error message:

    image

    I don’t know what trigger this, but I had to restart Visual Studio to be able to Run or Debug tests again. After some searching on the net, I found out that there’s some sort of communication error between Visual Studio and the executable that actually runs the tests. So my resolution now is to kill that process and try again. The name of the executable to look for is VSTestHost.exe. Kill the process and you’re good to go…

    image

  • Chad Hower’s story

    A friend of mine, Chad Hower aka Kudzu, is in trouble. Chad has developed many internet-related components for Delphi, with Indy (being shipped with Delphi) being the most well known. Chad is also a speaker and writer and now works for Microsoft. He’s accused of kidnapping his son. Although there’s really strong evidence that it’s totally impossible for him to have done so. For example, how can you kidnap a son you have custody over? It’s a heartbreaking and quite unbelievable story. Watch the 5 minute at www.freechad.org and spread the word!

  • What’s new in WCF 4.0: brief summary

    Below are the notes I took at the PDC about what’s new in WCF 4.0.

    • Default bindings can be created. If no bindings are specified for a service, these bindings are used.
    • WCf supports configuration inheritance, so bindings could also be specified at various levels including the machine.config.
    • Same applies to behaviors
    • Default bindings/behaviors are the bindings/behaviors that have no name or name=”” in the configuration
    • Configuration based activation allows services without *.svc files
    • ETW (Event Tracing for Windows) is used by WCF
    • AppFabric (FKA “Dublin”) stores that data in a Sql Server database for easy retrieval and querying.
    • Routing is a completely new feature in WCF which allows service aggregation, protocol bridging and versioning.
    • MessageFilters are used to determine destination(s).
    • Routing table can be changed at runtime.
    • Security can also be bridged.
    • Alternate endpoints can be specified for fail-over-safety.
    • Discovery is another new feature. It allows for location agility, dynamic / self-healing apps
    • Discovery adheres to WS-Discovery standards
    • Two mode for discovery: Ad-hoc and managed
    • Ad-hoc discovery uses broadcasts
    • Managed discovery is like UDDI with a Discovery Proxy.
  • Compile-time checked region names in Prism

    In a R&D project of mine, I’m using Prism to create a composite WPF application. I can recommend anyone writing WPF or Silverlight applications to take a look at it if you’re unfamiliair with Prism.

    One thing I dislike about it though, is that is uses strings as region names. Meaning that there’s no compile-time checking if your region names in the Shell and the names you’re using when registering the views are actually aligned.

    Here’s a small trick to get compile-time checking for your region names in Prism:

    1. Define an Enum containing the names of all known regions:

          public enum RegionName
          {
              ToolBar,
              MessageList,
              Details
          }

    2. Define a MarkupExtension for each value of the enum. This sounds like a lot of work, but with a base class like the one below, it’s no more than a few lines.

          public class BaseRegionExtension : MarkupExtension
          {
              private RegionName _regionName;
       
              public BaseRegionExtension(RegionName regionName)
              {
                  _regionName = regionName;
              }
       
              public override object ProvideValue(IServiceProvider serviceProvider)
              {
                  return _regionName.ToString();
              }
       
              public RegionName Name
              {
                  get
                  {
                      return _regionName;
                  }
                  set
                  {
                      _regionName = value;
                  }
              }


      Defining a MarkupExtension now becomes really simple:

         public class ToolBarRegionExtension : BaseRegionExtension
          {
              public ToolBarRegionExtension()
                  : base(RegionName.ToolBar)
              {
              }
          }

    3. When registering your view, just call the ToString() of the enum value you want to use:

        _regionManager.RegisterViewWithRegion(RegionName.ToolBar.ToString(), typeof(MainToolbarView));

    4. When defining regions in your Shell Xaml file, use the right MarkupExtension to reference a value in your enum:

      <ToolBarPanel>
           <ItemsControl cal:RegionManager.RegionName="{ui:ToolBarRegion}">           
           </ItemsControl>
      </ToolBarPanel>

    Have fun!

  • No regions when generating code

    Just wished I had found this option about 5 years earlier….

    image

  • Programming with a guitar…

    My long-time friend Mark Miller of DevExpress has created another challenge for himself. A couple of years ago at the PDC he challenged developers to write some code together. The developer would have a full keyboard and mouse and a standard Visual Studio. Mark would have a keyboard too, but he had to program with chopsticks. His Visual Studio would have CodeRush installed though. And yeah, Mark was faster…

    Well, this year Mark has an even greater challenge. He’s not going to use a keyboard at all. He’s going to use a guitar to write code. Yeah…a guitar…to write code…

    Watch Mark in action on his guitar: http://tv.devexpress.com/GuitarCodeInterview.movie

    Posted okt 21 2009, 03:51 by Thomas with no comments
    Filed under: ,
  • Great free tool to manage TFS WorkItems

    Internally we use TFS to manage our own projects. And also at client-site we recommend using TFS for managing the application lifecycle. But Visual Studio Team Explorer is not as easy to use as I’d like it to be. Some things are just too hard. For example, try searching for a workitem that you know has a certain term in its Title or its Description. If you’re nodding your head now, you know what I’m talking about…

    Yesterday I came across a great tool from Telerik, a tool and component-vendor. They offer a FREE tool to manage TFS workitems. I recommend anyone using TFS to check out Telerik’s TFS Work Item Manager. These features are advertised by Telerik. Needless to say I really like the last one…

    • Work Item grid filtering, grouping, and aggregation
    • Area and Iteration filtering using single and multi select modes
    • Filter query results using a tree of areas or iterations
    • Unique Task board view of work items independent from any process template
    • Print work item cards for the board in your room
    • Iteration schedule
    • Paste clipboard contents into a work item
    • "New Query by example” saves your query for other team members
    • Search the title and description of query results as you type
  • Getting started with the Managed Extensibility Framework

    I recently started exploring the new Visual Studio 2010 code editor. The great thing about the new editor is that it supports plugins. You’re able to write additions to the Visual Studio editor that formats the code in any way you like. Microsoft put up some samples on CodePlex. Visual Studio uses the Managed Extensibility Framework (MEF) as the base for writing plugins. In this post I’d like to give you the basics of the Managed Extensibility Framework. After that, I’ll explore writing plugins for Visual Studio 2010.

    What problems does MEF solve?

    MEF presents a simple solution for the runtime extensibility problem. Until now, any application that wanted to support a plugin model needed to create its own infrastructure from scratch. Those plugins would often be application-specific and could not be reused across multiple implementations.
    • MEF provides a standard way for the host application to expose itself and consume external extensions. Extensions, by their nature, can be reused amongst different applications. However, an extension could still be implemented in a way that is application-specific. Extensions themselves can depend on one another and MEF will make sure they are wired together in the correct order (another thing you won't have to worry about).
    • MEF offers a set of discovery approaches for your application to locate and load available extensions.
    • MEF allows tagging extensions with additional metadata which facilitates rich querying and filtering

    I copied the above text from the CodePlex MEF homepage. So basically, MEF is a framework that will let you write applications that support plugins very easily. MEF exposes classes, interfaces and services to easily let you write applications that support plugins.

    How does MEF work?

    Basically, this is how it works:

    • Some components (parts of your application, not System.ComponentModel.Component) expose capabilities (or services if you will).
    • Some components are dependent on other capabilities.

    The goal of MEF is to glue the capabilities and dependencies together in your application. This allows for a very modular design. Exposing a capability in MEF is called an Export and a dependency on an other capability is called in Import.

    Let’s take a look at how this works. Suppose we have an application that is able to write tracing messages to some log. One class is going to want to log a message, the other class is going to take care of that. Of course we don’t want having a direct reference from one class to the other, so we’ll use an interface. Just like you’d do in any well designed application.

        public interface ITraceMessageWriter
        {
            void WriteToLog( string message );
        }

    The next step is to create a class that implements this interface. This class will take care of actually logging the message:

        [Export(typeof(ITraceMessageWriter))]
        public class TraceMessageWriter : ITraceMessageWriter
        {
            public void WriteToLog( string message )
            {
                // write here to any data store (file/database)
            }
        }

    The only thing we added is an MEF attribute: Export. The argument of the Export attribute is the name of a contract. This can be a string or a Type. The purpose of this attribute is to link the right services to the right dependencies. So the next piece of code shows how to import a dependency:

        public partial class Window1 : Window
        {
            [Import]
            protected ITraceMessageWriter _writer;
     
            

    To make sure the _writer field actually gets a reference to an instance, we make sure MEF is going to ‘compose’ our object. To do this we call a method called Compose in the constructor. This method is one we’re defining later and will make sure that MEF fills our field. After that we can call methods on it. Note that there’s no guarantee that the field will actually be filled. In the case that there’s no Export (service/capability) found for this dependency, the field would still be null.

            public Window1()
            {
                InitializeComponent();
     
                Compose();
                _writer.WriteToLog("Hello World");
     
            }
     
     

    The Compose method does all the MEF magic. In this Compose method, we need to do a couple of things.

    • Create a CompositionContainer. This container is used when composing objects.
    • Tell which object or objects needs composing (have their dependencies resolved).
      To do this, we need a CompositionBatch. An object which holds references to all objects that need to be composed.
    • Tell which extensions/plugins need to be searched for Exports
      In MEF we need a Catalog for this. A Catalog defines the location of one or more extensions. In this example, I’ll be using an AssemblyCatalog. A Catalog referring to an already loaded assembly

    Fortunately, the resulting code is pretty easy:

            private void Compose()
            {
                AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
     
                CompositionContainer _container = new CompositionContainer(catalog);
     
                CompositionBatch batch = new CompositionBatch();
                batch.AddPart(this);
     
                _container.Compose(batch);
            }

    This is what we did:

    • We created a catalog for the current executing assembly
    • We created a container to do the composition
    • We added ourselves (since ‘this’ has the _writer field) to a CompositionBatch.
    • We called Compose on the container passing in the CompositionBatch.

    That’s all. Our dependency is resolved by MEF and we’re ready to go. Admittedly, this is a extremely basic example. But remember that the dependencies we have may resolved from plugins that are loaded dynamically from a given directory. Or from registered plugins in a config file.

    Isn’t MEF just another Dependency Injection framework?

    No, not really. Although MEF does have some capabilities that DI-frameworks have, MEF’s focus is on composition. Some classes import services from other parts of the application, and some classes export services. The goal of MEF is to seamlessly glue those together. One other difference is that normally when using a DI-framework, you are aware of all the components used in the application. In MEF, this may not be the case. You load up some extensions or plugins or whatever you want to call them, and MEF discovers all the exports and imports of services in those extensions. Therefore, the actual functionality of the application is dependent of the extensions that are loaded at a given time. This is uncommon in ‘normal’ DI scenario’s.

    Where and when is MEF available?

    Whenever you use the .NET Framework 4.0. As MSDN says: “MEF is an integral part of the .NET Framework 4 Beta 1, and is available wherever the .NET Framework is used. You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET. In addition, there are plans to add MEF support for Silverlight applications.”

    So it’s going to be a part of the Framework 4.0. Until then, MEF can be downloaded from here.

    Where do we go from here?

    If you’re interested in MEF, I’d suggest you read at least this overview on MSDN and Brad Abrams Simple Example of MEF in Silverlight. That should give you a good headstart.

  • First Dutch Surface User Group meeting

    The Dutch Surface Usergroup had its first meeting on June 25th, 2009. See the video of that meeting here.

    The usergroup also has its own website located at http://surface.dotned.nl/

  • Interview with Niklaus Wirth

    Although I mostly develop in C#, I do have a long history developing in Delphi. I just stumbled upon an interview with Niklaus Wirth, the language designer of the original Pascal. Niklaus Wirth is a very interesting man. As a real academic, he has some views on today’s programming languages and programs that are non-conventional. I found it an interesting read. Oh, and the history of languages described in this interview IS real by the way

    Read the interview here: http://www.simple-talk.com/opinion/geek-of-the-week/niklaus-wirth-geek-of-the-week/

    Posted jul 02 2009, 09:48 by Thomas with no comments
    Filed under:
More Posts Next page »