Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper and is*Specified properties

I have a bunch of XSD.exe-generated data contract classes which for all optional elements have a pair of C# properties like

int Amount {get; set;}
bool isAmountSpecified {get; set;}

On the other side of mapping arena I have a nullable int like

int? Amount {get; set;}

Ideally I'd like for AutoMapper to be able to recognize such patterns and know how to map things in both directions without me having to specify a mapping for each individual property. Is this possible?

like image 868
Igor Brejc Avatar asked Mar 09 '10 12:03

Igor Brejc


People also ask

How do I ignore properties in AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.

Why you should not use AutoMapper?

1. If you use the convention-based mapping and a property is later renamed that becomes a runtime error and a common source of annoying bugs. 2. If you don't use convention-based mapping (ie you explicitly map each property) then you are just using automapper to do your projection, which is unnecessary complexity.

Can AutoMapper set private properties?

AutoMapper will map property with private setter with no problem. If you want to force encapsulation, you need to use IgnoreAllPropertiesWithAnInaccessibleSetter. With this option, all private properties (and other inaccessible) will be ignored.


2 Answers

OK, yesterday I've had a brief discussion with Jimmy Bogard, author of AutoMapper, and basically what I'm looking for is currently not possible. Support for such conventions will be implemented some time in the future (if I understood him correctly :) ).

like image 95
Igor Brejc Avatar answered Oct 26 '22 05:10

Igor Brejc


I honestly have no idea whether AutoMapper will do that (since I don't use AutoMapper much), but I know that protobuf-net supports both those patterns, so you could use Serializer.ChangeType<,>(obj) to flip between them.

The current version is, however, pretty dependent on having attributes (such as [XmlElement(Order = n)]) on the members - I don't know if that causes an issue? The in progress version supports vanilla types (without attributes), but that isn't complete yet (but soon).

Example:

[XmlType]
public class Foo
{
    [XmlElement(Order=1)]
    public int? Value { get; set; }
}
[XmlType]
public class Bar
{
    [XmlElement(Order = 1)]
    public int Value { get; set; }
    [XmlIgnore]
    public bool ValueSpecified { get; set; }
}
static class Program
{
    static void Main()
    {
        Foo foo = new Foo { Value = 123 };
        Bar bar = Serializer.ChangeType<Foo, Bar>(foo);
        Console.WriteLine("{0}, {1}", bar.Value, bar.ValueSpecified);

        foo = new Foo { Value = null };
        bar = Serializer.ChangeType<Foo, Bar>(foo);
        Console.WriteLine("{0}, {1}", bar.Value, bar.ValueSpecified);

        bar = new Bar { Value = 123, ValueSpecified = true };
        foo = Serializer.ChangeType<Bar, Foo>(bar);
        Console.WriteLine(foo.Value);

        bar = new Bar { Value = 123, ValueSpecified = false };
        foo = Serializer.ChangeType<Bar, Foo>(bar);
        Console.WriteLine(foo.Value);
    }
}
like image 31
Marc Gravell Avatar answered Oct 26 '22 04:10

Marc Gravell