Thursday, February 25, 2010

Generating GUIDs with Powershell

Something I ran across the other day, was to use Powershell to generate a GUID.
This is quite useful since you’re not always working in a .NET application when you need one. And you don’t always want to compile an application to get it. 

One way to do this, is to talk to the .NET runtime in Powershell.  You can simply execute the following command :

[System.Guid]::NewGuid()

This will generate a simple Guid, however now you will still have to copy/paste it from your command line to be able to use it.
It would be very handy to immediately have the Guid available in your clipboard.

You can do this using the following powershell script :

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.Clipboard]::SetText([System.Guid]::NewGuid().ToString())

This script will load the the Windows Forms assembly and get the Clipboard class to use the SetText method.  And … magic we have the Guid in our clipboard.

Wednesday, February 3, 2010

Silverlight : MEF and MVVM

With the upcoming release of Silverlight 4, I had a look at the Managed Extensibility Framework, since it is shipped together with Silverlight 4 once it is released.

What is MEF ?

According to the codeplex project :

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). 
source : http://mef.codeplex.com/

Basically you can export members of a class, these will be viewed by MEF as parts, which in turn can be imported by other classes.

When you want to import those parts to you Silverlight view you have to use, PartInitializer which will satisfy all imports on the class with the exported types.

MEF and MVVM

This functionality gave me the idea to import a ViewModel to a View to there is no link between the both in either direction. Allowing to easily change ViewModels and Views and reuse them.

Defining the contract

To be able to export and import our ViewModel parts, we have to define a contract for them.

We will simply add the following empty interface that we must implement on all our ViewModels :

public interface IViewModelBase { }

public class ViewModel : IViewModelBase
{
public string Value
{
   get
   {
     return "Some Display Value";
   }
}
}

Exporting our ViewModel

The next step would be to export our ViewModel and expose it so it can be imported by our view.

To do this we could provide a custom MetaModel class to make sure we can differentiate our ViewModels when assigning them to our View, however since we are only interested in a single identifier, we can create a custom export attribute to automatically do this.

To do this we first create a contract that will be implemented as the MetaData.

public interface IViewModelMetaData
{
string Identifier { get; }
}

Next we will create our custom export attribute, we should make sure we inherit the original ExportAttribute and mark the class as metadata so the metadata contract can be interpreted when we are importing.

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ViewModelExportAttribute : ExportAttribute, IViewModelMetaData
{
public ViewModelExportAttribute(): base(typeof(IViewModelBase))
{}

public string Identifier { get; set; }
}

We can apply this to our ViewModel by adding the following attribute above the class definition.

[ViewModelExport(Identifier = "MyViewModelIdentifier")]

Importing the ViewModel

At this point we want to be able to link the exported ViewModel to our View, to do this we can create a abstract class we can use as a base for all our views, implementing some basic functionality to enable the import.

public abstract class ViewBase : UserControl

   protected ViewBase() 
  
       PartInitializer.SatisfyImports(this); 
   }  

   public abstract string ViewModelIdentifier { get; }  

   [ImportMany
   public Lazy<IViewModelBase,IViewModelMetaData>[] ViewDataContext 
  
       set 
      
           DataContext = value
           .Where(viewModel => viewModel.Metadata.Identifier == ViewModelIdentifier)
           .First().Value; 
       
    }
}

So what exactly did we do here ?   To start ,we have created a ViewDataContext property of type Lazy<IViewModelBase,IViewModelData> and marked it with the ImportMany attribute.

Once the StatisfyImports method is called, MEF will initialize all properties marked with the Import attribute, in our case it will initialize the ‘ViewDataContext’ property.  Since MEF will import all Parts of the given IViewModelBase contract, we need a way to identify which ViewModel we want to use on our View, we can do this by exposing an abstract property ‘ViewModelIdentifier’ that will have to implemented by all views. 

Doing this we can check the exported metadata for the Identifier and compare it to the ViewModelIdentifier defined in the view, when a match has been found, the ViewModel is assigned to the DataContext of the view and we can do all the usual MVVM magic.