gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 05:27:27 24 4
gpt4 key购买 nike

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

IMyRepository
IAnotherRepository

他们都从 IRepository 继承(如果这有帮助)

如何让结构映射使用 IRegistryConvention 扫描器来注册我的具体类型,这些类型被命名为

SqlMyRepository
SqlAnotherRepository

最佳答案

我读过那篇文章,但它并没有给我我需要的东西。 AddAllTypesOf 针对 IRepositoryInterface 注册了所有具体类型,但我要求每个具体类型都针对具有等效命名的接口(interface)注册。
IE。

For<IMyRepository>().Use<SqlMyRepository>();

我还需要为测试存储库创建一些命名实例。
For<IMyRepository>().Use<TestMyRepository>().Named("Test");

这是我想出的东西,它似乎可以根据需要工作。
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);
}
}

并实现如下
Scan(cfg =>
{
cfg.TheCallingAssembly();
cfg.Convention<SqlRepositoryConvention>();
});

关于configuration - StructureMap IRegistrationConvention 注册非默认命名约定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2379680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com