Automapper - 它可以仅映射源对象和目标对象中的现有属性吗

作者:编程家 分类: 编程代码 时间:2025-08-21

Automapper是一个非常有用的库,它可以帮助我们在源对象和目标对象之间进行属性映射。它的一个主要功能是将源对象的属性值复制到目标对象中,以便我们可以方便地进行数据转换和处理。

在使用Automapper时,我们可以选择仅映射源对象和目标对象中的现有属性。这意味着,只有在源对象和目标对象中都存在的属性才会被映射。这对于我们只想复制一些特定属性或者只关注某些属性的情况非常有用。

案例代码:

假设我们有两个类,一个是源对象(Source),另一个是目标对象(Destination):

csharp

public class Source

{

public string Name { get; set; }

public int Age { get; set; }

public string Address { get; set; }

}

public class Destination

{

public string Name { get; set; }

public int Age { get; set; }

}

现在我们只想将源对象的Name和Age属性映射到目标对象中。我们可以使用Automapper来实现这个需求:

csharp

var config = new MapperConfiguration(cfg =>

{

cfg.CreateMap()

.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))

.ForMember(dest => dest.Age, opt => opt.MapFrom(src => src.Age));

});

var mapper = config.CreateMapper();

var source = new Source

{

Name = "John",

Age = 25,

Address = "123 Street"

};

var destination = mapper.Map(source);

Console.WriteLine($"Name: {destination.Name}");

Console.WriteLine($"Age: {destination.Age}");

在上面的代码中,我们首先创建了一个MapperConfiguration对象,并定义了Source和Destination之间的映射规则。我们使用ForMember方法来指定映射的属性,并使用MapFrom方法来指定源对象中的属性。然后我们创建了一个Mapper对象,并使用Map方法将源对象映射到目标对象。

输出结果将会是:

Name: John

Age: 25

使用标签进行分段

Automapper的强大之处在于它的灵活性。除了仅映射现有属性外,我们还可以进行更复杂的映射,如自定义映射、忽略属性、条件映射等。让我们来看一些更高级的用法。

自定义映射

有时候我们可能需要对映射进行一些额外的处理,例如计算属性的值或者进行数据转换。我们可以使用Automapper提供的自定义映射功能来实现这些需求。

csharp

public class Source

{

public int Price { get; set; }

}

public class Destination

{

public string FormattedPrice { get; set; }

}

var config = new MapperConfiguration(cfg =>

{

cfg.CreateMap()

.ForMember(dest => dest.FormattedPrice, opt => opt.MapFrom(src => $"${src.Price}"));

});

var mapper = config.CreateMapper();

var source = new Source

{

Price = 1000

};

var destination = mapper.Map(source);

Console.WriteLine($"Formatted Price: {destination.FormattedPrice}");

上面的代码中,我们定义了一个自定义映射规则,将Source对象的Price属性映射到Destination对象的FormattedPrice属性。在映射过程中,我们使用了MapFrom方法来指定FormattedPrice属性的值为Price属性的值加上一个美元符号。输出结果将会是:

Formatted Price: $1000

忽略属性

有时候我们可能希望在映射过程中忽略某些属性,这可以通过使用Ignore方法来实现。

csharp

public class Source

{

public string Name { get; set; }

public int Age { get; set; }

}

public class Destination

{

public string Name { get; set; }

}

var config = new MapperConfiguration(cfg =>

{

cfg.CreateMap()

.ForMember(dest => dest.Name, opt => opt.Ignore());

});

var mapper = config.CreateMapper();

var source = new Source

{

Name = "John",

Age = 25

};

var destination = mapper.Map(source);

Console.WriteLine($"Name: {destination.Name}");

在上面的代码中,我们显式地忽略了Destination对象的Name属性。输出结果将会是:

Name:

条件映射

有时候我们可能需要根据某些条件来进行映射,这可以通过使用条件操作符来实现。

csharp

public class Source

{

public int Age { get; set; }

}

public class Destination

{

public string AgeGroup { get; set; }

}

var config = new MapperConfiguration(cfg =>

{

cfg.CreateMap()

.ForMember(dest => dest.AgeGroup, opt => opt.MapFrom(src => src.Age >= 18 ? "Adult" : "Child"));

});

var mapper = config.CreateMapper();

var source1 = new Source

{

Age = 25

};

var destination1 = mapper.Map(source1);

Console.WriteLine($"Age Group: {destination1.AgeGroup}");

var source2 = new Source

{

Age = 10

};

var destination2 = mapper.Map(source2);

Console.WriteLine($"Age Group: {destination2.AgeGroup}");

在上面的代码中,我们定义了一个条件映射规则,根据Source对象的Age属性的值来确定Destination对象的AgeGroup属性的值。如果Age大于等于18,则AgeGroup为Adult,否则为Child。输出结果将会是:

Age Group: Adult

Age Group: Child

Automapper是一个功能强大的库,可以帮助我们轻松地进行对象属性映射。通过仅映射源对象和目标对象中的现有属性,我们可以灵活地控制映射过程。除此之外,Automapper还提供了许多其他高级功能,如自定义映射、忽略属性和条件映射等,使我们能够更好地满足各种映射需求。无论是简单的属性复制还是复杂的数据转换,Automapper都是一个值得推荐的工具。

以上是关于Automapper的介绍以及其仅映射现有属性的用法和示例代码。希望本文能够对你理解和使用Automapper有所帮助。