Set AutoFac to use PropertiesAutowired (true) by default?

Is there a way AutoFac can use the PropertiesAutowired (true) property by default for all registered types.

i.e. I do not want to use ".PropertiesAutowired (true)" all the time

var builder = new ContainerBuilder();
builder.RegisterType<Logger>()
    .PropertiesAutowired(true)
    .SingleInstance();
+3
source share
2 answers

This can be done using a module, for example

class InjectPropertiesByDefaultModule : Autofac.Module {
    protected override void AttachToComponentRegistration(
        IComponentRegistration registration,
        IComponentRegistry registry) {
            registration.Activating += (s, e) => {
                e.Context.InjectProperties(e.Instance);
            };
    }
}

Then:

builder.RegisterModule<InjectPropertiesByDefaultModule>();

I think you may be mistaken in the parameter truebefore PropertiesAutowired- it determines how circular dependencies are supported, and probably it should stay false. To emulate a setting true, you can attach to Activatedinstead of Activatingabove.

, , "" , ILog. (, readonly), ( , .)

, , .

"Null Object" .

+9

, . , , , . builder.RegisterAssemblyTypes(..).

. , . @ .

0

Source: https://habr.com/ru/post/1757597/


All Articles