Property Injection with internal setter

可紊 提交于 2019-12-06 03:34:31

You cannot inject internal setters with autofac, because the AutowiringPropertyInjector class is only looking for public properties (see source).

However a logic in the AutowiringPropertyInjector is very simple so you can create your own version which does injection for non public properties:

public static class AutowiringNonPublicPropertyInjector
{
     public static void InjectProperties(IComponentContext context, 
            object instance, bool overrideSetValues)
     {
          if (context == null)
              throw new ArgumentNullException("context");
          if (instance == null)
              throw new ArgumentNullException("instance");
          foreach (
             PropertyInfo propertyInfo in 
                 //BindingFlags.NonPublic flag added for non public properties
                 instance.GetType().GetProperties(BindingFlags.Instance |
                                                  BindingFlags.Public |
                                                  BindingFlags.NonPublic))
         {
             Type propertyType = propertyInfo.PropertyType;
             if ((!propertyType.IsValueType || propertyType.IsEnum) &&
                 (propertyInfo.GetIndexParameters().Length == 0 &&
                     context.IsRegistered(propertyType)))
             {
                 //Changed to GetAccessors(true) to return non public accessors
                 MethodInfo[] accessors = propertyInfo.GetAccessors(true);
                 if ((accessors.Length != 1 || 
                     !(accessors[0].ReturnType != typeof (void))) &&
                      (overrideSetValues || accessors.Length != 2 ||
                      propertyInfo.GetValue(instance, null) == null))
                 {
                     object obj = context.Resolve(propertyType);
                     propertyInfo.SetValue(instance, obj, null);
                 }
            }
        }
    }
}

And now you can use this class in the OnActivated event

var builder = new ContainerBuilder();
builder.RegisterType<MyPublicClass>();
builder.RegisterType<BaseContext>()
    .OnActivated(args =>   
          AutowiringNonPublicPropertyInjector
              .InjectProperties(args.Context, args.Instance, true));

However the above listed solution now injects all kind of properties so even private and protected ones so you may need to extend it with some additional checks to make sure that you will only inject the properties what you would expect.

I'm using a solution like this:

builder.RegisterType<MyPublicClass>();
builder.RegisterType<BaseContext>()
       .OnActivating(CustomPropertiesHandler);

With a handler like this:

//If OnActivated: Autofac.Core.IActivatedEventArgs
public void CustomPropertiesHandler<T>(Autofac.Core.IActivatingEventArgs<T> e)
{
    var props = e.Instance.GetType()
        .GetTypeInfo().DeclaredProperties //Also "private prop" with "public set"
        .Where(pi => pi.CanWrite) //Has a set accessor.
        //.Where(pi => pi.SetMethod.IsPrivate) //set accessor is private
        .Where(pi => e.Context.IsRegistered(pi.PropertyType)); //Type is resolvable

    foreach (var prop in props)
        prop.SetValue(e.Instance, e.Context.Resolve(prop.PropertyType), null);
}

Since both IActivatingEventArgs and IActivatedEventArgs has instance and context, you might want to use wrapping methods that uses those parameters on CustomPropertiesHandler instead.

Also we can write @nemesv implementation as an extension method.

public static class AutofacExtensions
{
    public static void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }
        if (instance == null)
        {
            throw new ArgumentNullException(nameof(instance));
        }

        foreach (var propertyInfo in instance.GetType().GetProperties(BindingFlags.Instance |
                                                                      BindingFlags.Public |
                                                                      BindingFlags.NonPublic))
        {
            var propertyType = propertyInfo.PropertyType;

            if ((!propertyType.IsValueType || propertyType.IsEnum) && (propertyInfo.GetIndexParameters().Length == 0) && context.IsRegistered(propertyType))
            {
                var accessors = propertyInfo.GetAccessors(true);
                if (((accessors.Length != 1) ||
                     !(accessors[0].ReturnType != typeof(void))) &&
                    (overrideSetValues || (accessors.Length != 2) ||
                     (propertyInfo.GetValue(instance, null) == null)))
                {
                    var obj = context.Resolve(propertyType);
                    propertyInfo.SetValue(instance, obj, null);
                }
            }
        }
    }

    public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> InjectPropertiesAsAutowired<TLimit, TActivatorData, TRegistrationStyle>(
        this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> registration)
    {
        return registration.OnActivated(args => InjectProperties(args.Context, args.Instance, true));
    }

To Use;

protected override void Load(ContainerBuilder builder)
{
    builder.RegisterType<StartupConfiguration>().As<IStartupConfiguration>().AsSelf().InjectPropertiesAsAutowired().AsImplementedInterfaces().SingleInstance();
}

Current version of Autofac defined optional IPropertySelector parameter for PropertiesAutowired which is used to filter out injectable properties.

default implementation for IPropertySelector is DefaultPropertySelector, which filters non public properties.

public virtual bool InjectProperty(PropertyInfo propertyInfo, object instance)
{
   if (!propertyInfo.CanWrite || propertyInfo.SetMethod?.IsPublic != true)
   {
       return false;
   }
   ....
 }

define custom IPropertySelector which allows injection to non public properties

public class AccessRightInvariantPropertySelector : DefaultPropertySelector
{
    public AccessRightInvariantPropertySelector(bool preserveSetValues) : base(preserveSetValues)
    { }

    public override bool InjectProperty(PropertyInfo propertyInfo, object instance)
    {
        if (!propertyInfo.CanWrite)
        {
            return false;
        }

        if (!PreserveSetValues || !propertyInfo.CanRead)
        {
            return true;
        }
        try
        {
            return propertyInfo.GetValue(instance, null) == null;
        }
        catch
        {
            // Issue #799: If getting the property value throws an exception
            // then assume it's set and skip it.
            return false;
        }
    }
}

Use

builder.RegisterType<AppService>()
      .AsImplementedInterfaces()
      .PropertiesAutowired(new AccessRightInvariantPropertySelector(true));

Alternatively

Install

PM> Install-Package Autofac.Core.NonPublicProperty

Use

builder.RegisterType<AppService>()
      .AsImplementedInterfaces()
      .AutoWireNonPublicProperties();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!