Is there a way i can set AutoFac to use PropertiesAutowired(true) to be the default for all types being registered.
i.e. I dont want to use ".PropertiesAutowired(true)" all the time
var builder = new ContainerBuilder();
builder.RegisterType<Logger>()
.PropertiesAutowired(true)
.SingleInstance();
This can be done with a module, e.g.
Then:
I think you might be misunderstanding the
true
paramerter toPropertiesAutowired
- it determines how circular dependencies are supported and should probably remainfalse
. To emulate thetrue
setting you can attach toActivated
instead ofActivating
above.However, if at all possible, use constructor injection even for "optional" dependencies like your
ILog
. It leads to cleaner components (e.g. fields can be madereadonly
) and dependencies are more understandable (they're all in the constructor, and there's no guessing about the meaning of individual properties.)Only consider using property injection when there are multiple configurations of the application and in some configurations the dependency will be truly absent.
Even in these cases, the "Null Object" pattern is usually a better fit.
No, there isn't. Though, if you register types in bulk or by convention it will be easier, e.g. using
builder.RegisterAssemblyTypes(..)
.Update: Yes there is, see @Nicholas answer.