ninject - WhenTargetHas<> Binding Does not Work as Expected -


i've got class constructor:

public abstractaddon([configuration]object configuration) {     this.configuration = configuration; } 

as can see, has constructor parameter configurationattribute. configurationattribute is:

[attributeusage(attributetargets.property | attributetargets.parameter, inherited=true)] public class configurationattribute : attribute { } 

my module as:

public override void load() {     this.bind(b => b.fromassembliesmatching("*")         .selectallclasses()         .inheritedfrom(typeof(uiextensibility.abstractaddon))         .bindallbaseclasses()         .configure(c => c.insingletonscope())     );      this.bind<object>().toprovider<configurationprovider>().whentargethas<uiextensibility.configurationattribute>(); } 

and then, object provider is:

private class addonprovider : iprovider<object> {      public object create(icontext context)     {         return "configuration settings";     }  } 

when perform kernel.getall<uiextensibility.abstractaddon>()), expect provider used create "configuration" object instances - provider never called.

could me, please? i'll appreciate alot help.

thanks all.

an attribute of parameter not "inherited" constructor of deriving class. there's no inheriting taking place, instead constructor of deriving class overriding (and extending) constructor of base class.

using inherited class-attribute instead

you can use instead:

[attributeusage(attributetargets.class, inherited=true)] public class configurationattribute : attribute { }  [configuration] public abstract class abstractaddon {     private readonly object configuration;      protected abstractaddon(object configuration)     {         this.configuration = configuration;     } }      public class fooaddon : abstractaddon {     public fooaddon(object configuration)         : base(configuration)     {     } } 

and bindings:

this.bind(b => b.fromassembliesmatching("*")     .selectallclasses()     .inheritedfrom(typeof(uiextensibility.abstractaddon))     .bindallbaseclasses()     .configure(c => c.insingletonscope()) );  this.bind<object>().toprovider<configurationprovider>()     .whenclasshas<uiextensibility.configurationattribute> 

other attribute-based methods

the ninject wiki describes other attribute based methods:

  • whentargethas<someattribute> = checks if parameter being injected has attribute
  • whentargethas<someattribute> = checks whether property being injected has attribute

now properties , attributes can inherited, possible solution:

public abstract class abstractaddon {     [inject]     [configuration]     public object configuration { get; set;} }  this.bind<object>().toprovider<configurationprovider>()     .whenmemberhas<configurationattribute> 

attribute-less convention

instead of checking attribute, since of types want inject config deriving abstractaddon anyway, why not base convention on that? doesn't need attribute @ all. suspect should work:

this.bind<object>().toprovider<configurationprovider>()     .wheninjectedinto<abstractaddon>(); 

if doesn't work, can roll own convention using:

this.bind<object>().toprovider<configurationprovider>()     .when(... condition goes here...); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -