5

为什么我无法将SetterPropertyvia注入StructureMap到 MVC ActionFilter?

public class LockProjectFilter : ActionFilterAttribute
    {
        [SetterProperty]
        public ISecurityService SecurityService { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var loggedinStaffId = SecurityService.GetLoggedInStaffId();
            if (loggedinStaffId == 1)
                throw new ArgumentNullException();
            base.OnActionExecuting(filterContext);
        }
    }


    public static IContainer Initialize()
    {
        ObjectFactory.Initialize(x =>
                         {
                             x.Scan(scan =>
                                            {
                                                scan.TheCallingAssembly();
                                                scan.WithDefaultConventions();
                                                scan.AssemblyContainingType<ISecurityService>();
                                            });
                             x.SetAllProperties(p => p.OfType<ISecurityService>());
                             //x.ForConcreteType<LockProjectFilter>().Configure
                                // .Setter(c => c.SecurityService).IsTheDefault();
                         });
        return ObjectFactory.Container;
    }
4

1 回答 1

9

您需要利用 ObjectFactory 的“BuildUp”方法。

http://docs.structuremap.net/ConstructorAndSetterInjection.htm#section4

[Test]
    public void create_a_setter_rule_and_see_it_applied_in_BuildUp_through_ObjectFactory()
    {
        var theGateway = new DefaultGateway();
        ObjectFactory.Initialize(x =>
        {
            x.ForRequestedType<IGateway>().TheDefault.IsThis(theGateway);

            // First we create a new Setter Injection Policy that
            // forces StructureMap to inject all public properties
            // where the PropertyType is IGateway
            x.SetAllProperties(y =>
            {
                y.OfType<IGateway>();
            });
        });

        // Create an instance of BuildUpTarget1
        var target = new BuildUpTarget1();

        // Now, call BuildUp() on target, and
        // we should see the Gateway property assigned
        ObjectFactory.BuildUp(target);

        target.Gateway.ShouldBeTheSameAs(theGateway);
    }

然后你可以像这样创建一个新的 FilterAttributeFilterProvider :

public class DependencyResolverFilterProvider : FilterAttributeFilterProvider
{
    public override IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        var filters = base.GetFilters(controllerContext, actionDescriptor);

        foreach (var filter in filters)
        {
            //DI via Setter Injection
            DependencyResolver.BuildUp(filter.Instance);
        }

        return filters;
    }
}

然后最后将您的自定义过滤器提供程序添加到 .net 管道。

private static void RegisterProviderAndFilters()
    {
        var oldProvider = FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider);
        FilterProviders.Providers.Remove(oldProvider);
        FilterProviders.Providers.Add(new DependencyResolverFilterProvider());

        RegisterGlobalFilters(GlobalFilters.Filters);
    }

希望这可以帮助!

wm

于 2013-12-20T21:35:55.167 回答