1

我一直在研究一个类似问题的公认答案,我认为具体工厂返回一个实现,该实现基于工厂方法上的字符串参数匹配具体实现上的命名绑定。

当工厂是抽象工厂并且我希望使用基于 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 { }
4

1 回答 1

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); }
}
于 2014-02-23T19:38:15.060 回答