3

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.

jdscolam
  • 988
  • 8
  • 20
  • 1
    AutoMapper will map one object to another as long as the property name matches. This is exactly what AutoMapper was made for – boca Jul 19 '11 at 19:25
  • It should only take you a minute to test this yourself before you ask somebody else too... FWIW: this is exactly the sort of problem Automapper is designed to solve... – Scrappydog Jul 19 '11 at 19:34
  • Well it looks like what I was more looking for was value injection ala ValueInjecter. It looks like it is a more appropriate solution to this problem. Thanks for the helpful advice where offered. – jdscolam Jul 19 '11 at 20:55

3 Answers3

5

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);
drneel
  • 2,887
  • 5
  • 30
  • 48
  • I think ValueInjecter worked better for my particular case, but I also like your answer; so accepted! – jdscolam Jul 19 '11 at 21:00
2

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);
Omu
  • 69,856
  • 92
  • 277
  • 407
  • I like ValueInjecter quite a bit, however I agree with some people's comments that some of these default conventions need to be included out of the box. It would really help with adoption. Thanks for all your hard work on VI! – jdscolam Jul 21 '11 at 16:44
  • @jdscolam which default conventions ? – Omu Jul 21 '11 at 18:21
0

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.

Ed Charbeneau
  • 4,501
  • 23
  • 23