StructureMap IRegistrationConvention注册非默认命名约定?

Sco*_*kay 8 structuremap configuration

我目前有一堆像这样的存储库

IMyRepository
IAnotherRepository

它们都继承自IRepository(如果这有帮助的话)

如何让structuremap使用IRegistryConvention扫描程序来注册我命名的具体类型

SqlMyRepository
SqlAnotherRepository

Sco*_*kay 16

我读过这篇文章,但它并没有给我所需要的东西.AddAllTypesOf针对IRepositoryInterface注册了所有具体类型,但我要求每个具体类型都使用等效命名注册到接口.即.

For<IMyRepository>().Use<SqlMyRepository>();
Run Code Online (Sandbox Code Playgroud)

此外,我需要为测试存储库创建一些命名实例.

For<IMyRepository>().Use<TestMyRepository>().Named("Test");
Run Code Online (Sandbox Code Playgroud)

这就是我想出来的东西,它看起来像我需要的那样起作用.

public class SqlRepositoryConvention : StructureMap.Graph.IRegistrationConvention
{
    public void Process(Type type, Registry registry)
    {
        // only interested in non abstract concrete types that have a matching named interface and start with Sql           
        if (type.IsAbstract || !type.IsClass || type.GetInterface(type.Name.Replace("Sql", "I")) == null)
            return;

        // Get interface and register (can use AddType overload method to create named types
        Type interfaceType = type.GetInterface(type.Name.Replace("Sql","I"));
        registry.AddType(interfaceType, type);
    }
}
Run Code Online (Sandbox Code Playgroud)

并实施如下

Scan(cfg =>
            {
                cfg.TheCallingAssembly();
                cfg.Convention<SqlRepositoryConvention>();
            });
Run Code Online (Sandbox Code Playgroud)