【问题标题】:Scanning with StructureMap使用 StructureMap 进行扫描
【发布时间】:2013-08-05 19:26:13
【问题描述】:

我正在做的一个项目使用 StructureMap。我们有一个使用类似这样的代码段:

Scan(x =>
{
    try
    {
        x.Convention<SqlTypesConvention>();
        x.Assembly("ASSEMBLY NAME HERE");
    }
    catch
    {
    }
});

internal class SqlTypesConvention : IRegistrationConvention
{
    #region IRegistrationConvention Members

    public void Process(Type type, Registry registry)
    {
        string interfaceName = "I" + type.Name + "Data";
        Type interfaceType = type.GetInterface(interfaceName);

        if (type.IsAbstract || !type.IsClass || interfaceType.IsNullRef())
        {
            return;
        }
        registry.AddType(interfaceType, type);
    }

    #endregion IRegistrationConvention Members
}

这个扫描和约定代码有什么作用?

【问题讨论】:

  • @AhmedKRAIEM LOL..我已经阅读了官方网站上的 StructureMap 文档,但仍然无法理解公约的内容..这就是为什么我在这里发布问题希望有人能解释一下简单来说。

标签: c# structuremap


【解决方案1】:

Foo 类的接口经常被命名为IFoo。因此约定是在类名前加上I

代码示例中的约定是,对于给定的类Foo,其接口将命名为IFooData

StructureMap 将使用此约定来尝试查找所有类型的接口关系。因此,当类构造函数请求IFooData 时,StructureMap 将能够提供Foo 的实例。

【讨论】:

    【解决方案2】:

    如果有任何类“XXX”使用以“I”开头的接口“IXXX”,则结构映射将扫描所有程序集以查找“xxxx”类并自动实例化。有关结构图如何工作的详细信息,请参阅@how structure map works

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 2021-04-05
      • 2012-07-17
      • 2014-11-04
      • 2018-11-19
      相关资源
      最近更新 更多