This project is read-only.

Configurating Generic Types

Dec 28, 2010 at 1:20 PM

Hi everybody.

I must say that really good work is done by sonic team! I hope one time to see this work under Prism.

Thank U guys!

The issue I've faced is related to initialization of Generic types/aliases through XAML config.

I've tried a similar to Spring.NET initialization concept (it supports generic types initialization through config) but failed - got exception on the level of Unity.CreateFromXaml(...) which says:

Failed to create a 'System.Type' from the text 'Modules.Core.MVVM.IView<Modules.FooModule1.ViewModels.IFooViewModel>, Modules.Core, Version=, Culture=neutral, PublicKeyToken=null'.

Below is a config snippet:

        <!-- Lifetime manager types -->
        <u:TypeAlias Alias="default" 
                     Type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity.Silverlight, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <!-- User-defined type aliases -->
        <u:TypeAlias Alias="FooView" 
                     Type="Modules.Core.MVVM.IView&lt;Modules.FooModule1.ViewModels.IFooViewModel&gt;, Modules.Core, Version=, Culture=neutral, PublicKeyToken=null" />
                <u:UnityType Name="FooView" 
                             MapToName="Modules.FooModule1.Views.FooView, Modules.FooModule1, Version=, Culture=neutral, PublicKeyToken=null">
                    <u:Lifetime TypeName="default" />

However, if I replace initialization with dummy interface:


public interface IFooView : IView<IFooViewModel>


and config node to:

<u:TypeAlias Alias="FooView" Type="Modules.FooModule1.Views.IFooView, Modules.FooModule1, Version=, Culture=neutral, PublicKeyToken=null" />

Registration of object and object resolution works fine! :)

Can anyone explain where is the trick?
May be I'm missing something in Generic types initialization?



Dec 30, 2010 at 11:18 AM
Edited Jan 2, 2011 at 11:52 AM

Hi alex,

The parsing of types in .net is done by the static method call Type.GetType that takes as input a string representation of the type. In order to find out which is the correct string to provide to the configuration the safest way to go is do a typeof(IView<IFooViewModel>).AssemblyQualifiedName in your immediate window. In Sonic.Net we have some inteligence there to construct missing parts of information about the type such as when there is no strong name in your assemblies you can simply use class and assemply name and skip version, culture and PublicKeyToken. Otherwize you need the full AssemblyQualifiedName. So basically we are supporting the .net way to get a type from string.

As for the string representation of generic types in Sonic.Net unity configuration that would be:

in case the generic type and its generic parameter axist in the same Assembly:

<u:TypeAlias Alias="FooView"
                     Type="Modules.Core.MVVM.IView`1[Modules.FooModule1.ViewModels.IFooViewModel], Modules.Core, Version=, Culture=neutral, PublicKeyToken=null" />

in case they are in different assemblies (a bit ugly but will do the trick):

<u:TypeAlias Alias="FooView"
                     Type="Modules.Core.MVVM.IView`1[[Modules.FooModule1.ViewModels.IFooViewModel, Modules.SomeOtherAssembly, Version=, Culture=neutral, PublicKeyToken=null]], Modules.Core, Version=, Culture=neutral, PublicKeyToken=null" />

However, you got a point that this should be easier and there is work to be done to make the notation more friendly like in Spring.Net. Hope to do in a future release.

Happy new Year & happy coding!

Jan 3, 2011 at 12:30 PM

Many thanks to U, cleftheris! :)

Seems like I've been working with Spring.NET DI IoC Container for a really long period and forgot the real .NET way of Generic types string presentation... :)