Property injection

Jan 4, 2012 at 2:51 PM

Hello! Is it possible to utilize property injection with Sonic.Net? If so, how?

I have created a singleton repository class that has a property for each repository and each property is marked with [Dependency] attribute. But it seems that I'm doing something wrong since the properties are always null. One property looks like this:

[Dependency]
public IUserRepository UserRepository { get; set; }         

 

I'm configuring DI via XAML (I intend to download DI config file from the server in the future):

<u:Unity xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:u="clr-namespace:UnityConfiguration;assembly=sonic.UnityConfiguration.Silverlight">

    <u:TypeAliases>
        <u:TypeAlias Alias="IUserRepository"
                     Type="Infrastructure.Repositories.IUserRepository, Infrastructure, Version=1.0.0.0" />
    </u:TypeAliases>
    
    <u:Containers>
        <u:Container>
            <u:Types>
                
                <u:UnityType TypeName="IUserRepository"
                             MapToName="Infrastructure.Repositories.UserRepository, Infrastructure, Version=1.0.0.0" />
            </u:Types>
        </u:Container>
    </u:Containers>
    
</u:Unity>

 

The types are correctly mapped (the Configure() method of the Container passes without errors) but it seems that the dependencies are not injected into properties.

Thanks!

Coordinator
Jan 4, 2012 at 3:29 PM
Edited Jan 4, 2012 at 3:30 PM

Hi Luke,

The UnityConfiguration library has only the concern of configuring your Container. The most probable cause your dependencies are not being resolved is that you should let the unity container instantiate the "the singleton repository class". For example if you named your master class MyRepositoryCatoalog. Then you could configure it via xaml to be a singleton. Each time you request the "IRepositoryCatalog" via UnityContainer all your dependencies will be resolved, plus you will get the same instance without implementing the singleton pattern yourself.

public interface IRepositoryCatoalog
{
    IUserRepository UserRepository { get; set; }
    ISomeOtherRepository OtherRepository { get; set; } 
}

public class MyRepositoryCatoalog : IRepositoryCatoalog 
{
    [Dependency]
    public IUserRepository UserRepository { get; set; }
    [Dependency]
    public ISomeOtherRepository OtherRepository { get; set; }
}
<u:Unity xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:u="clr-namespace:UnityConfiguration;assembly=sonic.UnityConfiguration.Silverlight">

    <u:TypeAliases>
        <u:TypeAlias Alias="singleton"          Type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity.Silverlight, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <u:TypeAlias Alias="IRepositoryCatalog" Type="Infrastructure.Repositories.IRepositoryCatalog, Infrastructure, Version=1.0.0.0" />
        <u:TypeAlias Alias="IUserRepository"    Type="Infrastructure.Repositories.IUserRepository, Infrastructure, Version=1.0.0.0" />
    </u:TypeAliases>
    
    <u:Containers>
        <u:Container>
            <u:Types>
                <u:UnityType TypeName="IRepositoryCatalog" MapToName="Infrastructure.Repositories.MyRepositoryCatalog, Infrastructure, Version=1.0.0.0">
                        <u:Lifetime TypeName="singleton" />
                </u:UnityType>

                <u:UnityType TypeName="IUserRepository" MapToName="Infrastructure.Repositories.UserRepository, Infrastructure, Version=1.0.0.0" />
            </u:Types>
        </u:Container>
    </u:Containers>
    
</u:Unity>

Cheers,

Constantinos

Jan 5, 2012 at 1:07 PM

Thank you! This works perfectly. Now only to figure out how to dynamically load a DLL at runtime using Unity. :)

Coordinator
Jan 5, 2012 at 9:00 PM

Hi luke,

glad I could help with this one.

As far as dynamic loading is concearned you can always use Prism  and the modularity feature. It plays nicely with unity and the configuration feature and takes care of asynchronous assembly loading.

Cheers

Jan 9, 2012 at 11:53 AM

Thank you, Constantinos! I'm looking into Prism, it seems that it provides what I'm looking for.