5

Neden SetterPropertyStructureMap aracılığıyla MVC ActionFilter'a enjekte edemiyorum?SetterProperty enjeksiyonu, ASP.NET MVC ActionFilter öğesinden yararlanmaProperty

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; 
    } 
+0

soru benim sorunum ben bana yardımcı Sevgili i [SetterProperty] benim kodunda özledim ve bunu tam detay var benim kod sonra bağlantıyı takip görmek istiyorsanız o iş http://stackoverflow.com/questions/23386344/asp-net-mvc-5-custom-action-filter-with-structuremap – Developerzzz

cevap

9

ObjectFactory'den 'BuildUp' yöntemini kullanmanız gerekir.

[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); 
    } 

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

Sonra böyle bir yeni FilterAttributeFilterProvider oluşturabilirsiniz:
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; 
    } 
} 

Sonunda .net boru hattına özel filtre sağlayıcı ekleyin.

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); 
    } 

Bu yardımcı olur umarız!

wm

+0

Enjekte edilen mülkün kamuya açık olması koşuluyla, bu bir çekicilik gibi çalışır (bunun üzerine yarım saat harcanır, enjeksiyon yoksa sessizce başarısız olur. özellik ayarlanmış olarak bulunur). –