gpt4 book ai didi

c# - 使用 AutoMapper.Profile 创建实例(非静态)映射器

转载 作者:太空狗 更新时间:2023-10-30 00:31:13 24 4
gpt4 key购买 nike

我使用以下答案中描述的方法来创建映射器的实例:

var platformSpecificRegistry = AutoMapper.Internal.PlatformAdapter.Resolve<IPlatformSpecificMapperRegistry>();
platformSpecificRegistry.Initialize();

var autoMapperCfg = new AutoMapper.ConfigurationStore(new TypeMapFactory(), AutoMapper.Mappers.MapperRegistry.Mappers);
var mappingEngine = new AutoMapper.MappingEngine(_autoMapperCfg);

如以下问题所述:

AutoMapper How To Map Object A To Object B Differently Depending On Context .

我怎样才能使用[重用] Automapper profile像下面这样的类来创建映射器的实例?

public class ApiTestClassToTestClassMappingProfile : Profile
{
protected override void Configure()
{
base.Configure();
Mapper.CreateMap<ApiTestClass, TestClass>()
.IgnoreAllNonExisting();
}
}

只是为了让您了解为什么我需要这样的功能:我使用以下方法将所有 Automapper 配置文件类注册到我的 IoC 容器 [CaSTLeWindsor] 中:

    IoC.WindsorContainer.Register(Types.FromThisAssembly()
.BasedOn<Profile>()
.WithServiceBase()
.Configure(c => c.LifeStyle.Is(LifestyleType.Singleton)));

var profiles = IoC.WindsorContainer.ResolveAll<Profile>();

foreach (var profile in profiles)
{
Mapper.AddProfile(profile);
}

IoC.WindsorContainer.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));

虽然上面完全满足了初始化我的静态 Mapper 类的需要,但我真的不知道如何重新使用我的 Automapper 配置文件类来创建实例映射器 [使用非静态映射器]。

最佳答案

这就是您使用配置文件创建 MapperConfiguration 的方式

public static class MappingProfile
{
public static MapperConfiguration InitializeAutoMapper()
{
MapperConfiguration config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new WebMappingProfile()); //mapping between Web and Business layer objects
cfg.AddProfile(new BLProfile()); // mapping between Business and DB layer objects
});

return config;
}
}

配置文件示例

//Profile number one saved in Web layer
public class WebMappingProfile : Profile
{
public WebMappingProfile()
{
CreateMap<Question, QuestionModel>();
CreateMap<QuestionModel, Question>();
/*etc...*/
}
}

//Profile number two save in Business layer
public class BLProfile: Profile
{
public BLProfile()
{
CreateMap<BLModels.SubModels.Address, DataAccess.Models.EF.Address>();
/*etc....*/
}
}

将自动映射器连接到 DI 框架(这是 Unity)

public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
var mapper = MappingProfile.InitializeAutoMapper().CreateMapper();
container.RegisterInstance<IMapper>(mapper);

FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}

/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}

在你的类中使用 IMapper 进行映射

public class BaseService
{
protected IMapper _mapper;

public BaseService(IMapper mapper)
{
_mapper = mapper;
}

public void AutoMapperDemo(){
var questions = GetQuestions(token);
return _mapper.Map<IEnumerable<Question>, IEnumerable<QuestionModel>>(questions);
}

public IEnumerable<Question> GetQuestions(token){
/*logic to get Questions*/
}
}

我的原始帖子可以在这里找到:http://www.codeproject.com/Articles/1129953/ASP-MVC-with-Automapper-Profiles

关于c# - 使用 AutoMapper.Profile 创建实例(非静态)映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29080423/

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