Property injection with Unity

时间:2015-06-15 14:32:48

标签: c#-4.0 dependency-injection unity-container

i encoutered problem with unity, i want to use property injection, here is what i had in my code : config of the container :

public static void RegisterTypes(IUnityContainer container)
    {
        container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
                 .RegisterType<IUnitOfWork, UnitOfWorkGT>()
                 .RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())                     
                 .RegisterType<ISocieteServices, SocieteServices>() ;
 }

SocieteService Class :

 public class SocieteServices : ISocieteServices
{
    private IUnitOfWork UnitOfWork;

    public SocieteServices(IUnitOfWork unitOfWork)
    {
        UnitOfWork = unitOfWork;
    }

}

i tried to use property injection (i can't use constructor injection with custom data annotation) and here what i had done :

   public class CodeSocieteUniqueAttribute : ValidationAttribute
{
     [Dependency]
    public ISocieteServices SocieteService {get; set;} 

     [InjectionMethod]
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {

        string codeSociete = value as string;

        var societe = SocieteService.getSocieteByCode(codeSociete);
        if (societe == null) return ValidationResult.Success;
        else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

    }
}

the problem is that the societeService in CodeSocieteUniqueAttribute class is not injected.

1 个答案:

答案 0 :(得分:2)

假设您注册类型的类是公共可访问的并且具有IUnityContainer对象,即:

public static class Resolver
{

    public static IUnityContainer Container { get; set; }
    public static void RegisterTypes(IUnityContainer container)
    {
        // type registrations here
        container.RegisterType<GTModelContainer, GTModelContainer>(new HttpContextLifetimeManager<GTModelContainer>())
             .RegisterType<IUnitOfWork, UnitOfWorkGT>()
             .RegisterType<ILogger, Logger>(new ContainerControlledLifetimeManager())                     
             .RegisterType<ISocieteServices, SocieteServices>() ;

        // Now, set the container
        Container = container;
    }
}

您可以访问您构建的容器,并在方法执行期间解析这些类型。

例如,

public class CodeSocieteUniqueAttribute : ValidationAttribute
{
     [Dependency]
    public ISocieteServices SocieteService { get; set; } 

     [InjectionMethod]
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var societeServices = Resolver.Container.Resolve<ISocieteServices>();
        SocieteService = societeServices; // Or, you know, just use this since it's resolved.
        string codeSociete = value as string;

        var societe = SocieteService.getSocieteByCode(codeSociete);
        if (societe == null) return ValidationResult.Success;
        else return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));

    }
}

这实际上是非常标准的做法,this MSDN article描述了在运行时解析项目。

另一种选择是将分辨率弹出为默认构造函数,如下所示:

public class CodeSocieteUniqueAttribute : ValidationAttribute
{
    [Dependency]
    public ISocieteServices SocieteService {get; set;} 
    public CodeSocieteUniqueAttribute()
    {
        var societeServices = Resolver.Container.Resolve<ISocieteServices>();
        SocieteService = societeServices; 
    }
     // the rest of the class omitted for brevity
}