Property Injection with internal setter

巧了我就是萌 提交于 2019-12-22 10:16:21

问题


I have an existing application that I am modifying to use Autofac Property Injection. It seems regardless of which method I use to register my types with properties, the properties are always null unless they have public setters. With other IoC containers (e.g. Structuremap) it's possible to scope the setter internal and make it available using the InternalsVisibleTo attribute on the assembly. This would seem nice to restrict clients from modifying the assignment.

Is this possible with Autofac? Or is there another approach when working with property injection to keep the assignments secure?

I've tried using reflection with PropertiesAutoWired() as well as resolving .WithParameter() from my WebApi Global.asax - specifying the specific parameter to be set with no success as an internal setter.

[assembly: InternalsVisibleTo("MyWebAPI.dll")]
[assembly: InternalsVisibleTo("Autofac.dll")]
[assembly: InternalsVisibleTo("Autofac.Configuration.dll")]
namespace My.Namespace
{
    public class BaseContext
    {
        public MyPublicClass _dbHelper { get; internal set; }

        public BaseContext()
        {

        }

        protected string DbConnectionString
        {
            get
            {
                return _dbHelper.DbConn; //<-Always null unless setter is public
            }
        }
    }
}

回答1:


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.




回答2:


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.




回答3:


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();
}



回答4:


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();


来源:https://stackoverflow.com/questions/18188427/property-injection-with-internal-setter

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