zcz527 / autofac

Automatically exported from code.google.com/p/autofac
Other
0 stars 0 forks source link

PropertiesAutowired() does not work with enums. #327

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
public interface ICar
{
    Transmission Transmission { get; set; }
    void Drive();
}
public enum Transmission { Automatic, Manual, CVT, };
public class Car : ICar
{
    public Transmission Transmission { get; set; }
    public void Drive()
    {
    }
}

static void Main()
{
    var builder = new ContainerBuilder();
    builder.Register<Transmission>(c => Transmission.Manual).As<Transmission>();
    builder.RegisterType<Car>().As<ICar>().PropertiesAutowired();

    var container = builder.Build();

    var car = container.Resolve<ICar>();
//  car.Transmission == Automatic, but supposed to be Manual
}

Autofac 2.4.5, .NET 3.5.

Please provide any additional information below.

Original issue reported on code.google.com by tahir.ah...@gmail.com on 24 May 2011 at 1:02

GoogleCodeExporter commented 8 years ago
As a workaround, you can use the following PropertiesAutowired_WithEnums 
extension method instead of PropertiesAutowired:

        public static IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> PropertiesAutowired_WithEnums<TLimit, TActivatorData, TRegistrationStyle>(this IRegistrationBuilder<TLimit, TActivatorData, TRegistrationStyle> This)
        {
            This.PropertiesAutowired();

            This.OnActivated(e =>
            {
                var props = e.Instance.GetType().GetProperties();
                foreach (var oneProp in props)
                {
                    if (oneProp.CanWrite && oneProp.PropertyType.IsEnum)
                    {
                        if(e.Context.IsRegistered(oneProp.PropertyType))
                        {
                            var enumValue = e.Context.Resolve(oneProp.PropertyType);
                            oneProp.SetValue(e.Instance, enumValue, null);
                        }
                    }
                }
            });

            return This;
        }

Original comment by tahir.ah...@gmail.com on 24 May 2011 at 2:31

GoogleCodeExporter commented 8 years ago
Thanks for raising this. The behaviour is by design, but the original exclusion 
of value types from autowiring was possibly a premature optimisation. I can't 
see any harm in removing the restriction.

Cheers,
Nick

Original comment by nicholas...@gmail.com on 15 Jun 2011 at 10:09

GoogleCodeExporter commented 8 years ago
Auto-wiring (and registrations for that matter) don't really make any sense for 
any value type other than the enum. Having said that, I don't see a reason to 
disable any type to be auto-wired - if people want to shoot themselves in the 
foot by registering and auto-wiring int's and long's, who are we to stop them? 
:)

Tahir

Original comment by tahir.ah...@gmail.com on 15 Jun 2011 at 12:29

GoogleCodeExporter commented 8 years ago
:) to minimise risk I've enabled this for enums only right now, let's leave 
other value types for consideration in the future.

Original comment by nicholas...@gmail.com on 3 Jul 2011 at 5:37

GoogleCodeExporter commented 8 years ago
Thank you!

Original comment by tahir.ah...@gmail.com on 3 Jul 2011 at 11:21