如何在服务上使用带有 IObjectMapper 的自定义映射器
How to use Custom mapper with IObjectMapper on the service
从事 aspnetboilerplate asp.net 核心项目,在自定义映射方面遇到困难。想要创建自定义地图并想像 AUTOMAP profiler.Follow documentation 一样多次使用它,但未能在我的项目中实现它。
我的步骤是
1)在XXX.Core
下创建一个classMyModule
[DependsOn(typeof(AbpAutoMapperModule))]
public class MyModule : AbpModule
{
public override void PreInitialize()
{
Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
{
config.CreateMap<CreateUserInput, CreatUserOutput>()
.ForMember(u => u.Password, options => options.Ignore())
.ForMember(u => u.OutputEmailAddress, options => options.MapFrom(input => input.EmailAddress));
});
}
}
public class CreateUserInput
{
public string Name { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
}
public class CreatUserOutput
{
public string OutputName { get; set; }
public string Surname { get; set; }
public string OutputEmailAddress { get; set; }
public string Password { get; set; }
}
2) 在 xxx.Application 上使用以上配置,服务如下
try
{
CreateUserInput te = new CreateUserInput
{
EmailAddress = "a@yahoo.com",
Name = "input",
Password = "test",
Surname = "sure"
};
CreatUserOutput ot = new CreatUserOutput();
var temp = _objectMapper.Map<CreatUserOutput>(te);
}
catch (System.Exception ex)
{
}
我不明白如何将自定义映射器与我在服务上注入的 IObjectMapper 一起使用。
最好创建单独的自动映射器配置文件。
您需要在 Application 层中创建文件
您可以将其命名为 AutoMapperProfile.cs
以下内容:
public class AutoMapperProfile : AutoMapper.Profile {
public AutoMapperProfile () {
CreateMap<CreateUserInput, CreatUserOutput> ()
.ForMember (u => u.Password, options => options.Ignore ())
.ForMember (u => u.OutputEmailAddress, options => options.MapFrom (input => input.EmailAddress));
}
}
要订购此代码,请确保您 ApplicationModule.cs
包含以下负责加载配置文件的代码。
public override void Initialize () {
var thisAssembly = typeof (LicenseManagerApplicationModule).GetAssembly ();
IocManager.RegisterAssemblyByConvention (thisAssembly);
Configuration.Modules.AbpAutoMapper ().Configurators.Add (
// Scan the assembly for classes which inherit from AutoMapper.Profile
cfg => cfg.AddProfiles (thisAssembly)
);
}
从事 aspnetboilerplate asp.net 核心项目,在自定义映射方面遇到困难。想要创建自定义地图并想像 AUTOMAP profiler.Follow documentation 一样多次使用它,但未能在我的项目中实现它。 我的步骤是
1)在XXX.Core
下创建一个classMyModule [DependsOn(typeof(AbpAutoMapperModule))]
public class MyModule : AbpModule
{
public override void PreInitialize()
{
Configuration.Modules.AbpAutoMapper().Configurators.Add(config =>
{
config.CreateMap<CreateUserInput, CreatUserOutput>()
.ForMember(u => u.Password, options => options.Ignore())
.ForMember(u => u.OutputEmailAddress, options => options.MapFrom(input => input.EmailAddress));
});
}
}
public class CreateUserInput
{
public string Name { get; set; }
public string Surname { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
}
public class CreatUserOutput
{
public string OutputName { get; set; }
public string Surname { get; set; }
public string OutputEmailAddress { get; set; }
public string Password { get; set; }
}
2) 在 xxx.Application 上使用以上配置,服务如下
try
{
CreateUserInput te = new CreateUserInput
{
EmailAddress = "a@yahoo.com",
Name = "input",
Password = "test",
Surname = "sure"
};
CreatUserOutput ot = new CreatUserOutput();
var temp = _objectMapper.Map<CreatUserOutput>(te);
}
catch (System.Exception ex)
{
}
我不明白如何将自定义映射器与我在服务上注入的 IObjectMapper 一起使用。
最好创建单独的自动映射器配置文件。 您需要在 Application 层中创建文件 您可以将其命名为 AutoMapperProfile.cs 以下内容:
public class AutoMapperProfile : AutoMapper.Profile {
public AutoMapperProfile () {
CreateMap<CreateUserInput, CreatUserOutput> ()
.ForMember (u => u.Password, options => options.Ignore ())
.ForMember (u => u.OutputEmailAddress, options => options.MapFrom (input => input.EmailAddress));
}
}
要订购此代码,请确保您 ApplicationModule.cs 包含以下负责加载配置文件的代码。
public override void Initialize () {
var thisAssembly = typeof (LicenseManagerApplicationModule).GetAssembly ();
IocManager.RegisterAssemblyByConvention (thisAssembly);
Configuration.Modules.AbpAutoMapper ().Configurators.Add (
// Scan the assembly for classes which inherit from AutoMapper.Profile
cfg => cfg.AddProfiles (thisAssembly)
);
}