在运行时操作Automapper配置文件的映射表达式



我有一些Automapper配置文件,并根据情况在运行时创建两个不同的mapper实例。对于其中一个映射器实例,我需要在运行时忽略这些配置文件中映射的一些成员。考虑到下面的例子,我该如何做到这一点?

public class Foo
{
public object SomeField { get; set; }
}
public class FooDto
{
public object SomeField { get; set; }
}
public class FooProfile : Profile
{
public FooProfile()
{
CreateMap<Foo, FooDto>();
}
}

在运行时使用配置文件并使用不同的映射表达式创建多个映射器实例:

public class MapperFactory
{
public IMapper Create(bool isSomeFieldIgnored)
{
MapperConfiguration configuration;
if (isSomeFieldIgnored)
{
configuration = new MapperConfiguration(expression =>
{
expression.AddProfile<FooProfile>();
// ignore the SomeField here!
});
}
else
{
configuration = new MapperConfiguration(expression =>
{
expression.AddProfile<FooProfile>();
});
}
return configuration.CreateMapper();
}
}

我找不到在概要文件之外更改映射表达式的方法,也没有人回答。所以我不得不用另一种方法来解决这个问题。下面的示例显示了使用配置文件的构造函数中的参数将运行时条件应用于表达式的解决方案。

public class Foo
{
public object SomeField { get; set; }
}
public class FooDto
{
public object SomeField { get; set; }
}
public class FooProfile : Profile
{
public FooProfile()
: this(false)
{
// the profile must have a parameterless constructor.
}
public FooProfile(bool isSomeFieldIgnored)
{
var expression =
CreateMap<Foo, FooDto>();
// ignore the SomeField is necessary.
if (isSomeFieldIgnored)
expression.ForMember(fooDto => fooDto.SomeField,
options => options.Ignore());
}
}

配置文件在运行时被实例化,条件被传递给它的构造函数。因此,映射器工厂可以用于获得两个不同的映射器实例:

public class MapperFactory
{
public IMapper Create(bool isSomeFieldIgnored)
{
var configuration = new MapperConfiguration(expression =>
{
// instantiate the profile at runtime and pass the isSomeFieldIgnore as 
// its constructor argument.
var fooProfile =
(Profile)Activator.CreateInstance(typeof(FooProfile),
new object[] { isSomeFieldIgnored });
expression.AddProfile(fooProfile);
});
return configuration.CreateMapper();
}
}

也许不用opt。Ignore((您可以使用opt。条件((?如果你可以在静态条件下触发忽略成员,那就是

https://docs.automapper.org/en/stable/Conditional-mapping.html

"根据源、目标、源和目标成员有条件地映射此成员">

最新更新