posté @ Friday, February 19, 2010 3:46 PM

As this post is about a scenario that is not discussed very much even in English, I won’t write this post in French in order to have a broader audience.

WPF and Silverlight are very good candidates for applying IoC concepts. Coupled with MVVM pattern, IoC make it really easier to build testable and robust applications. However, one limitation is that the Xaml language itself does not play well with it (as it’s basically an object graph Serialization / Deserialization language and so it only works with concrete types). So to instantiate data contexts from an IoC container, we need to write code-behind code. This problem is even more annoying for the WF use of Xaml (as with WF4.0, we do not have Code-Behind anymore).

Fortunately, with .Net 4.0, the xaml readers and writers have been completely refactored in a very extensible way. So we are able to provide our own XamlSchemaContext that provides the XamlNodes instantiation logic. The resolution of a type in the .net 4.0 Xaml reader is done through an abstraction layer:

From a given xaml namespace / xaml element name, the XamlSchemaContext is responsible of resolving a XamlType which represents a Clr Type. This XamlType provide a TypeInvoker responsible of the instantiation of a XamlType. By creating a custom XamlSchemaContext that replace the default XamlType resolution by a unity-enabled XamlType / XamlTypeInvoker whenever it finds that the default one is not capable of creating the requested object, we can rely on the container to resolve these types.

So we’ll be able to parse things like that:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:my="clr-namespace:XamlIoCDemo;assembly=XamlIoCDemo" >

    <StackPanel>

        <ContentControl x:Name="barContent">

            <my:IBar />

        </ContentControl>

        <ContentControl Content="{Binding Content.OtherService, ElementName=barContent}" />

    </StackPanel>

</Window>

 

With IBar being an interface. I won’t go trough the details of the implementation, but here is what I wrote to parse this Xaml and display it :

IUnityContainer container = new UnityContainer();

container.RegisterType<IBar, BarImpl>();

container.RegisterType<IOtherService, OtherServiceImpl>();

 

var ctx = new WpfUnityEnabledXamlSchemaContext(container);

 

var reader = new XamlXmlReader("SampleWindow.xaml",ctx);

var result = System.Windows.Markup.XamlReader.Load(reader) as Window;

result.ShowDialog();

You can see that I wrote a WPF-specific xaml schema context (which is needed for the Wpf-specific features, like binding expressions). I also provided a more generic one for general purpose Xaml. I did not check it against WF 4.0, but I’ll try it in my way back from the MVP summit.

Here is the source code for all of that:

http://www.simonferquel.net/blog/XamlIOC.zip

 

 

Special thanks to Rob Relyea who made great demos about Xaml 2009 this week at the MVP Global Summit


Commentaires :

# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Thomas le 2/20/2010 6:52 AM
Super, Microsoft va dans le bon sens en permettant l'extensibilité.
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par primes des jeux le 5/21/2010 2:13 PM
Yes, the runtime XAML engine enables a number of scenarios, but it is difficult to communicate these flexibilities to designers and compilers.Clearly, enabling this flexibility without having to throw away compilation would be ideal.
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Ian Randall le 7/14/2010 12:45 PM
Merci bien, Simon!
This is *exactly* what I was looking for... My UserControls all (currently) use SL pattern to find my container, and resolve their ViewModels... Using this technique, I can simply inject the ViewModels directly into the UserControl constructors, and remove that annoying singleton Ioc.Container lurking in my code base! Thank you so much!
/Ian
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par blackjackdeal le 2/6/2011 12:01 PM
I totally agree here with the second comment, it would be ideal indeed. Why not do that then ?

Regardless i very much liked this blog and its insight.
Keep up the good work.
;)
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Supra For Sale le 6/7/2011 11:52 AM
With IBar being an interface. I won’t go trough the details of the implementation,
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par mulberry bags le 7/26/2011 5:09 AM
There is no doubt that Ed hardy clothing has become popular over the clothes market. The other important thing is that this Ed hardy clothing line has become a new street fashion. Walk on street, you will find that there are so many Ed hardy stores and Ed hardy clothes selling well. Due to the unique design and vigorously promoting, Ed hardy clothing becomes more and more popular. Youcan find Ed hardy clothes or other Christian Audigier accessories on any Ed hardy stores online.
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Discount Kenny Chesney Tickets le 8/3/2011 11:49 AM
I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them.
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Jersey Loans le 8/12/2011 2:56 AM
Very useful,thanks for sharing keep on going another one.You have gained a new reader.Thanks for the info.
# re: [Xaml] IoC-enabled Xaml parser
Ecrit par Recruitment to Recruitment Londo le 8/12/2011 3:35 AM
I am happy to read a lot of useful information here in the post. Thanks for sharing it here.
# dream vacation network
Ecrit par Erik le 9/6/2011 11:22 AM
improve the quality, safety and efficiency of healthcare services; reduce healthcare disparities; engage patients and their families; improve the coordination of care; improve population.

Ecrire un commentaire :

Titre :*
Nom *
Email
Url
Commentaire : *  


Please add 8 and 6 and type the answer here: