使用Ninject自定义实例提供程序成功绑定,使用工厂方法参数进行解析

时间:2013-04-07 03:20:48

标签: ninject-extensions abstract-factory open-generics ninject-conventions

我一直在研究this accepted answer一个类似的问题,其中我认为是具体的工厂返回一个基于工厂方法的字符串参数的实现,匹配具体实现的命名绑定。

当工厂是一个抽象工厂时,我正在努力让一个稍微复杂的例子正常工作,我希望使用基于Ninject约定的绑定。考虑以下测试:

[Fact]
public void VehicleBuilderFactory_Creates_Correct_Builder_For_Specified_Client()
{
    // arrange
    StandardKernel kernel = new StandardKernel();
    kernel.Bind(typeof (IVehicleBuilderFactory<,>))
        .ToFactory(() => new UseFirstArgumentAsNameInstanceProvider())
        .InSingletonScope();

    kernel.Bind(scanner => scanner
                    .FromThisAssembly()
                    .SelectAllClasses()
                    .WhichAreNotGeneric()
                    .InheritedFrom(typeof(IVehicleBuilder<>))
                    .BindAllInterfaces());

    var bicycleBuilderFactory = 
        kernel.Get<IVehicleBuilderFactory<IVehicleBuilder<BlueBicycle>, BlueBicycle>>();
    string country = "Germany";
    string localizedColor = "blau";

    // act
    var builder = bicycleBuilderFactory.Create<IVehicleBuilder<BlueBicycle>>(country);
    Bicycle Bicycle = builder.Build(localizedColor);

    // assert
    Assert.IsType<BlueBicycleBuilder_Germany>(builder);
    Assert.IsType<BlueBicycle>(Bicycle);
    Assert.Equal(localizedColor, Bicycle.Color);
}

这是我尝试用火把和杂物玩杂耍的地方因为我曾经在互联网上看到它的原因:

public class UseFirstArgumentAsNameInstanceProvider : StandardInstanceProvider
{
    protected override string GetName(MethodInfo methodInfo, object[] arguments) {
        return methodInfo.GetGenericArguments()[0].Name + "Builder_" + (string)arguments[0];
        // ex: Germany -> 'BlueBicycle' + 'Builder_' + 'Germany' = 'BlueBicyleBuilder_Germany'
    }

    protected override ConstructorArgument[] GetConstructorArguments(MethodInfo methodInfo, object[] arguments) {
        return base.GetConstructorArguments(methodInfo, arguments).Skip(1).ToArray();
    }
}

当我尝试为bicycleBuilderFactory分配此错误时,我被刺伤并点燃:

System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'Castle.Proxies.ObjectProxy' to type 'Ninject.Extensions.Conventions.Tests.IVehicleBuilderFactory`2[Ninject.Extensions.Conventions.Tests.IVehicleBuilder`1[Ninject.Extensions.Conventions.Tests.BlueBicycle],Ninject.Extensions.Conventions.Tests.BlueBicycle]'.
  Source=System.Core
  StackTrace:
       at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
       at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
       at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:\Projects\Ninject\ninject\src\Ninject\Syntax\ResolutionExtensions.cs:line 37
       at Ninject.Extensions.Conventions.Tests.NinjectFactoryConventionsTests.VehicleBuilderFactory_Creates_Correct_Builder_For_Specified_Client() in C:\Programming\Ninject.Extensions.Conventions.Tests\NinjectFactoryConventionsTests.cs:line 40
  InnerException: 

是否可以使用 ToFactory() 方法和自定义提供程序进行绑定,使用工厂方法参数"Germany"以及通用类型参数IVehicleBiulder<BlueBicycle>, BlueBicycle来解析类型?

以下是测试的其余代码,尽可能紧凑和可读。

public interface IVehicleBuilderFactory<T, TVehicle> 
    where T : IVehicleBuilder<TVehicle> where TVehicle : IVehicle
{
    T Create<T>(string country);
}

VehicleBuilder实施

public interface IVehicleBuilder<T> where T : IVehicle { T Build(string localizedColor); }

abstract class BicycleBuilder<T> : IVehicleBuilder<T> where T : Bicycle
{
    public abstract T Build(string localizedColor);
}

public abstract class RedBicycleBuilder : IVehicleBuilder<RedBicycle>
{
    private readonly RedBicycle _Bicycle;
    public RedBicycleBuilder(RedBicycle Bicycle) { _Bicycle = Bicycle; }
    public RedBicycle Build(string localizedColor)
    {
        _Bicycle.Color = localizedColor;
        return _Bicycle;
    }
}
public abstract class GreenBicycleBuilder : IVehicleBuilder<GreenBicycle>
{
    private readonly GreenBicycle _Bicycle;
    public GreenBicycleBuilder(GreenBicycle Bicycle) { _Bicycle = Bicycle; }
    public GreenBicycle Build(string localizedColor)
    {
        _Bicycle.Color = localizedColor;
        return _Bicycle;
    }
}
public abstract class BlueBicycleBuilder : IVehicleBuilder<BlueBicycle>
{
    private readonly BlueBicycle _Bicycle;
    public BlueBicycleBuilder(BlueBicycle Bicycle) { _Bicycle = Bicycle; }
    public BlueBicycle Build(string localizedColor)
    {
        _Bicycle.Color = localizedColor;
        return _Bicycle;
    }
}

public class RedBicycleBuilder_USA : RedBicycleBuilder {
    public RedBicycleBuilder_USA(RedBicycle Bicycle) : base(Bicycle) { }
}

public class RedBicycleBuilder_Germany : RedBicycleBuilder {
    public RedBicycleBuilder_Germany(RedBicycle Bicycle) : base(Bicycle) { }
}
public class RedBicycleBuilder_France : RedBicycleBuilder {
    public RedBicycleBuilder_France(RedBicycle Bicycle) : base(Bicycle) { }
}
public class RedBicycleBuilder_Default : RedBicycleBuilder {
    public RedBicycleBuilder_Default(RedBicycle Bicycle) : base(Bicycle) { }
}

public class GreenBicycleBuilder_USA : GreenBicycleBuilder {
    public GreenBicycleBuilder_USA(GreenBicycle Bicycle) : base(Bicycle) { }
}
public class GreenBicycleBuilder_Germany : GreenBicycleBuilder {
    public GreenBicycleBuilder_Germany(GreenBicycle Bicycle) : base(Bicycle) { }
}
public class GreenBicycleBuilder_France : GreenBicycleBuilder {
    public GreenBicycleBuilder_France(GreenBicycle Bicycle) : base(Bicycle) { }
}
public class GreenBicycleBuilder_Default : GreenBicycleBuilder {
    public GreenBicycleBuilder_Default(GreenBicycle Bicycle) : base(Bicycle) { }
}

public class BlueBicycleBuilder_USA : BlueBicycleBuilder
{
    public BlueBicycleBuilder_USA(BlueBicycle Bicycle) : base(Bicycle) { }
}
public class BlueBicycleBuilder_Germany : BlueBicycleBuilder {
    public BlueBicycleBuilder_Germany(BlueBicycle Bicycle) : base(Bicycle) { }
}
public class BlueBicycleBuilder_France : BlueBicycleBuilder
{
    public BlueBicycleBuilder_France(BlueBicycle Bicycle) : base(Bicycle) { }
}
public class BlueBicycleBuilder_Default : BlueBicycleBuilder
{
    public BlueBicycleBuilder_Default(BlueBicycle Bicycle) : base(Bicycle) { }
}

车辆实施:

public interface IVehicle { string Color { get; set; } }
public abstract class Vehicle : IVehicle { public string Color { get; set; } }
public abstract class Bicycle : Vehicle { }
public class RedBicycle : Bicycle { }
public class GreenBicycle : Bicycle { }
public class BlueBicycle : Bicycle { }

1 个答案:

答案 0 :(得分:1)

基于@LukeN的评论,我重构了Bicycle类,因此通过构造函数注入IColorSetter来设置它的颜色。 IColorSetter实现具有通用Color类型,并且每个Color实现都通过构造函数注入IColorLocalizer<T>来“本地化”。

通过这种方式,没有一个班级似乎对逻辑上的任何东西都有所了解(我认为)。

但是,我需要更多地考虑这一点,看看下面显示的重构类如何用于显示如何使用Ninject自定义实例提供程序现在可以用来选择属性IColorLocalizer<T>,因为这是唯一一个了解颜色和语言的课程;来自其泛型类型的颜色,以及来自实现本身名称的语言。

自从询问原始帖子以来,我已经不再使用IoC容器来做出这样的选择,而是选择以编程方式为代码添加一个用于选择实现的开关,并为任何未处理的异常情况选择默认实现。但是我不确定它是否主要是为了超越那些困扰我的东西,或者因为以这种方式依靠IoC容器是一个糟糕的选择。

我需要更多地更新这个答案。

<强>车辆

public abstract class Vehicle {
    public abstract string Color { get; internal set; }
    public abstract string Move();
}

public class Bicycle : Vehicle {
    public Bicycle(IColorSetter colorSetter) { colorSetter.SetColor(this); }
    public override string Color { get; internal set; }
    public override string Move() { return "Pedaling!"; }
}

色彩设定者

public interface IColorSetter { void SetColor(Vehicle vehicle); }

public class ColorSetter<T> : IColorSetter where T : Color
{
    private readonly T _color;
    public ColorSetter(T color) { _color = color; }

    public void SetColor(Vehicle vehicle) { vehicle.Color = _color.Name; }
}

颜色定位器

public interface IColorLocalizer<in T> where T : Color {
    void LocalizeColor(T color);
}

public class GermanBlueLocalizer : IColorLocalizer<Blue> {
    public void LocalizeColor(Blue color) { color.Name = "blau"; }
}

public class EnglishBlueLocalizer : IColorLocalizer<Blue> {
    public void LocalizeColor(Blue color) { color.Name = "blue"; }
}

<强>颜色

public abstract class Color { public string Name { get; internal set; } }

public class Red : Color {
    public Red(IColorLocalizer<Red> colorLocalizer) {
        colorLocalizer.LocalizeColor(this); }
}

public class Green : Color {
    public Green(IColorLocalizer<Green> colorLocalizer) {
        colorLocalizer.LocalizeColor(this); }
}

public class Blue : Color {
    public Blue(IColorLocalizer<Blue> colorLocalizer) {
        colorLocalizer.LocalizeColor(this); }
}