Autofac - resolving runtime parameters without having to pass container around

后端 未结 3 948
时光取名叫无心
时光取名叫无心 2020-12-29 03:31

I have a simpler \"ServiceHelper\" class that takes two parameters in the constructor:

public ServiceHelper(ILogger log, string serviceN         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-29 04:08

    Resolve should occur for root composition objects only. Calling resolve is near to the same as "newing" up an object, which is a smell test. There are times when the resolution is dynamic and can only be determined on-the-fly, but most dependencies are deterministic and can be registered up front. How to do this with Autofac is the challenge. The awarded answer by @Christian Specht is a good answer, but it assumes everything is determined at runtime.

    To define a dependency chain at design time, see SO topic Autofac sub-dependencies chain registration...

提交回复
热议问题