Using Dependency Injection frameworks for classes with many dependencies

后端 未结 6 646
深忆病人
深忆病人 2021-01-30 07:05

I have been looking at various dependency injection frameworks for .NET as I feel the project I am working on would greatly benefit from it. While I think I have a good grasp of

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 07:49

    To partially answer my first question, I've just found a blog post by Jeremy Miller, showing how Structure Map and setter injection can be used to auto-populate public properties of your objects. He uses ILogger as an example:

    var container = new Container(r =>
    {
        r.FillAllPropertiesOfType().TheDefault.Is
            .ConstructedBy(context => new Logger(context.ParentType));
    });
    

    This means that any classes with an ILogger property, e.g.:

    public class ClassWithLogger
    {
        public ILogger Logger { get; set; }
    }
    
    public class ClassWithLogger2
    {
        public ILogger Logger { get; set; }
    }
    

    will have their Logger property automatically set up when constructed:

    container.GetInstance();
    

提交回复
热议问题