StructureMap 2.5.4不再支持FindInterfaceThatCloses,新语法是什么?

时间:2010-02-01 13:43:13

标签: structuremap

以前我曾经:

public class PresentationModelConventionScanner : ITypeScanner
{
   public void Process(Type type, PluginGraph graph)
    {
        Type interfaceType = type.FindInterfaceThatCloses(typeof(IPresentationModel<>));
        if (interfaceType != null)
        {
            graph.AddType(interfaceType, type);
        }
    }

但2.5.4不再支持FindInterfaceThatCloses ...

似乎你必须实现IRegistrationConvention而不是ITypeScanner,因此Process方法语法也必须改变......

找不到任何例子......

1 个答案:

答案 0 :(得分:2)

我仍然在StructureMap源代码中看到 FindInterfaceThatCloses 类型扩展方法(在AssemblyScannerExtension.cs中)。

您可以使用新的 ConnectImplementationsToTypesClosing 方法替换所需的行为。

public interface IPresentationModel<T>{}
public class StringPresentationModel : IPresentationModel<string> {}
public class IntPresentationModel : IPresentationModel<int>{}

[TestFixture]
public class Structuremap_configuraiton
{
    [Test]
    public void connecting_implementations()
    {
        var container = new Container(cfg =>
        {
            cfg.Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.ConnectImplementationsToTypesClosing(typeof(IPresentationModel<>));
            });
        });

        container.GetInstance<IPresentationModel<string>>().ShouldBeOfType(typeof(StringPresentationModel));
        container.GetInstance<IPresentationModel<int>>().ShouldBeOfType(typeof(IntPresentationModel));
    }
}