gpt4 book ai didi

c# - 如何使用 Simple Injector 注册 AutoMapper 4.2.0

转载 作者:太空狗 更新时间:2023-10-29 17:39:01 26 4
gpt4 key购买 nike

已更新至 AutoMapper 4.2.0,并遵循此处提供的迁移指南:https://github.com/AutoMapper/AutoMapper/wiki/Migrating-from-static-API/f4784dac61b91a0df130e252c91a0efd76ff51de#preserving-static-feel .尝试将 StructureMap 页面上的代码转换为简单注入(inject)器。有人可以告诉我这段代码在 Simple Injector 中的样子吗?

结构图

public class AutoMapperRegistry : Registry
{
public AutoMapperRegistry()
{
var profiles =
from t in typeof (AutoMapperRegistry).Assembly.GetTypes()
where typeof (Profile).IsAssignableFrom(t)
select (Profile)Activator.CreateInstance(t);

var config = new MapperConfiguration(cfg =>
{
foreach (var profile in profiles)
{
cfg.AddProfile(profile);
}
});

For<MapperConfiguration>().Use(config);
For<IMapper>().Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
}

简单的注入(inject)器

?

最佳答案

这将是等价的:

container.RegisterInstance<MapperConfiguration>(config);
container.Register<IMapper>(() => config.CreateMapper(container.GetInstance));

关于c# - 如何使用 Simple Injector 注册 AutoMapper 4.2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35370733/

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