AutoMapper数据填充

发布于 2024-11-25 00:07:53 字数 447 浏览 4 评论 0原文

是否可以使用 AutoMapper 用另一个对象的详细信息填充一个对象?例如(假设之前的配置):

var foo = new Foo { PropA = "", PropB = "Foo" };
var bar = new Bar { PropA = "Bar", PropB = "" };

Mapper.Map<Foo, Bar>(foo, bar);

Console.WriteLine(bar.PropB); //Returns "Foo"

只是想知道是否有人尝试过这种公认的奇怪的映射用法,这更像是将一个对象的匹配数据合并到另一个对象中。

提前致谢!

更新:

看起来 ValueInjector 也更适合这种情况。 StackOverflow 上已经有一些关于 AutoMapper 与 ValueInjecter 的适当使用的精彩讨论。

Would it be possible to use AutoMapper in order to fill in an object with details from another object? For example (assuming previous configuration):

var foo = new Foo { PropA = "", PropB = "Foo" };
var bar = new Bar { PropA = "Bar", PropB = "" };

Mapper.Map<Foo, Bar>(foo, bar);

Console.WriteLine(bar.PropB); //Returns "Foo"

Just wondering if anyone has attempted this admittedly odd usage of mapping, which would be more like merging one object's matching data into another object.

Thanks in advance!

Update:

It looks like ValueInjector is a more appropriate too for this situation. There are some great discussions on appropriate uses for AutoMapper vs. ValueInjecter already on StackOverflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌面超妹 2024-12-02 00:07:53

如果属性名称匹配,那么它们将自动映射。如果由于某种原因他们不这样做,您可以自己指定映射。

因此,下面,PropA 与 PropertyA 不匹配,因此您必须指定映射。然而,PropB 匹配,所以你不匹配。

var foo = new Foo { PropA = "", PropB = "Foo" };
var bar = new Bar { PropertyA = "Bar", PropertyB = "" };

Mapper.CreateMap<Foo, Bar>()
      .ForMember(dest => dest.PropertyA, opt => opt.MapFrom(src => src.PropA));

Mapper.Map<Foo, Bar>(foo, bar);

If the property names match, then they will automatically map. If for some reason they don't you can specify the mapping yourself.

So below, PropA doesn't match PropertyA so you will have to specify the mapping. However, PropB matches, so you don't.

var foo = new Foo { PropA = "", PropB = "Foo" };
var bar = new Bar { PropertyA = "Bar", PropertyB = "" };

Mapper.CreateMap<Foo, Bar>()
      .ForMember(dest => dest.PropertyA, opt => opt.MapFrom(src => src.PropA));

Mapper.Map<Foo, Bar>(foo, bar);
爱殇璃 2024-12-02 00:07:53

好吧,使用 ValueInjecter,您可以做到这一点

bar.InjectFrom(foo);

,您的酒吧将是:

{PropA = "", ProbB = "Foo"}, 

与 Foo 完全相同
但如果您只想采用非空/空值来获取此值,

{PropA = "Foo", PropbB = "Bar"}

您可以创建一个新的注入

public class NonNullEmptyInj : ConventionInjection
{
      protected override bool Match(ConventionInfo c)
      {
        if (c.SourceProp.Name != c.TargetProp.Name
                           || c.SourceProp.Type != c.TargetProp.Type) return false;
        if(c.SourceProp.Value == null) return false;
        if (c.SourceProp.Type == typeof(string) && c.SourceProp.Value.ToString() == string.Empty) return false;
        return true;
       }
}

并按如下方式使用它:

bar.InjectFrom<NonNullEmptyInj>(foo);

well, with the ValueInjecter you can do

bar.InjectFrom(foo);

and your bar will be:

{PropA = "", ProbB = "Foo"}, 

exactly the way Foo was
but if you would like to take only the non null/empty values to get this

{PropA = "Foo", PropbB = "Bar"}

you can create a new Injection

public class NonNullEmptyInj : ConventionInjection
{
      protected override bool Match(ConventionInfo c)
      {
        if (c.SourceProp.Name != c.TargetProp.Name
                           || c.SourceProp.Type != c.TargetProp.Type) return false;
        if(c.SourceProp.Value == null) return false;
        if (c.SourceProp.Type == typeof(string) && c.SourceProp.Value.ToString() == string.Empty) return false;
        return true;
       }
}

and use it like this:

bar.InjectFrom<NonNullEmptyInj>(foo);
魂牵梦绕锁你心扉 2024-12-02 00:07:53

正如其他人所说,您在发帖之前确实应该多做功课。但请查看一下 Mapper.AssertConfigurationIsValid 方法。

    [Test]
    public void Mappings_Should_Map()
    {
        //configure mapping here

        //Assert
        Mapper.AssertConfigurationIsValid();
    }

这将告诉您映射是否有效,如果无效,它将告诉您哪些属性不适用。一旦您知道失败的映射是什么,您就可以使用 .ForMember 方法创建特定的映射。

As others have said, you should really do more homework before posting. But have a look at the Mapper.AssertConfigurationIsValid method while your at it.

    [Test]
    public void Mappings_Should_Map()
    {
        //configure mapping here

        //Assert
        Mapper.AssertConfigurationIsValid();
    }

This will tell you if the mapping works, if not it will tell you what properties do not apply. Once you know what the failed mappings are you can then create specific maps using the .ForMember Method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文