开发者

how OnCreationForAll method works in structormap registry?

I need to register all implementation of an interface via structur map, the problem I have is that these instances does not have the default ctor , i need to inject a depend开发者_JAVA技巧ency when i want to create n instance but because hibernate need public or protected default ctor i had to have a protected default ctor like here:

 public class LengthRule : RuleBase
{
    protected LengthRule()
        : base(null)
    {
    }

    public LengthRule(ITextColouriser textColouriser)
        : base(textColouriser)
    {
    }
}

 public abstract class RuleBase : IPropertyRuleObject
{
    public virtual ITextColouriser TextColouriser { get; set; }

    public RuleBase(ITextColouriser textColouriser)
    {
        TextColouriser = textColouriser;
    }
}

and for registering this dependency i use this line

   x.For<IPropertyRuleObject>().OnCreationForAll((w,e)=>e.TextColouriser=w.GetInstance<ITextColouriser>());

in ObjectFactory.Initialize method

but when i want to get propertyType.RuleObjects // each rule object is one of IPropertyRuleObject implementation,, TextColouriser would be null

what is the problem and i needed to register these implementation by nae, so i have this line in initilaize method too:

 x.Scan(p =>
      {
          p.AddAllTypesOf<IPropertyRuleObject>().NameBy(c => c.Name);
      });


This is an old post I know, but I was faced with the same just today.

The solution was much easier than I first thought.

Just create a wiring of ITextColouriser and it will get injected magically :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜