Castle Windsor BuildUp

时间:2010-11-08 17:37:11

标签: dependency-injection castle-windsor

Castle Windsor是否与Unity的BuildUp()等效,您可以使用属性修饰类属性并让容器将值传递给现有对象实例?

e.g。在Unity中:

Statistics existingInstance = new Statistics();  
container.BuildUp(existingInstance)

class Statistics  
{  
  [Dependency]  
  public ICalculator Calculator { get; set; }  

2 个答案:

答案 0 :(得分:3)

但您可以自己编写此功能。例如,这是一个ASP.NET MVC FilterAttributeFilterProvider,用于将属性注入属性操作过滤器。

  public class AttributeFilterProvider : FilterAttributeFilterProvider
  {
    public AttributeFilterProvider(IKernel kernel)
    {
      _kernel = kernel;
    }

    private readonly IKernel _kernel;

    protected override IEnumerable<FilterAttribute> GetControllerAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
      var attributes = base.GetControllerAttributes(controllerContext, actionDescriptor);
      BuildUpAttributeDependancies(attributes);
      return attributes;
    }

    protected override IEnumerable<FilterAttribute> GetActionAttributes(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
      var attributes = base.GetActionAttributes(controllerContext, actionDescriptor);
      BuildUpAttributeDependancies(attributes);
      return attributes;
    }

    private void BuildUpAttributeDependancies(IEnumerable<FilterAttribute> attributes)
    {
      foreach (var attribute in attributes)
      {
        var propInfos = attribute.GetType().GetProperties().Where(x => x.GetValue(attribute) == null).AsEnumerable();
        foreach (var pi in propInfos)
        {
          if (_kernel.HasComponent(pi.PropertyType))
          {
            var service = _kernel.Resolve(pi.PropertyType);
            pi.SetValue(attribute, service);
          }
        }
      }
    }
  }

在BuildUpAttributeDependancies方法中,我们查找未初始化(null)属性,然后检查该类型是否已在Castle Windsor中注册。如果有,我们设置属性。

通过在global.asax文件中用我们自定义的(上面)替换默认的FilterAttributeFilterProvider,我们现在可以使用Castle Windsors DI功能将任何类型注入我们的MVC应用程序中的任何Action Filter。为了完整地完成这个答案,下面是一个global.asax应用程序类的示例,其中Castle Windsor设置为Controller(在实例化时)和ActionFilter(在使用时)依赖注入:

public class MvcApplication : System.Web.HttpApplication
{
  private static IWindsorContainer _container;

  private static void BootstrapContainer()
  {
    _container = new WindsorContainer()
        .Install(FromAssembly.This());

    var controllerFactory = new ControllerFactory(_container.Kernel);
    ControllerBuilder.Current.SetControllerFactory(controllerFactory);
  }

  private static void BootstrapFilters()
  {
    var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
    FilterProviders.Providers.Remove(oldProvider);

    var provider = new AttributeFilterProvider(_container.Kernel);
    FilterProviders.Providers.Add(provider);
  }

  protected void Application_Start()
  {
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    BootstrapContainer();
    BootstrapFilters();
  }
}

答案 1 :(得分:1)

看起来答案是Castle Windsor无法通过属性或注入方法将依赖项注入现有对象实例:

Duplication Question