2

i'm trying to use Automapper in this scenario. I've got an Entity (DDD entity object) that must have private setters for all the properties and collections and i have to map it to a simpler object that will be stored in DB using. The Entity have a code like that:

    public class TypeA : Entity
{
    private List<TypeB> _assignedItems;
    public IEnumerable<TypeB> AssignedItems
    {
        get { return _assignedItems.ToList(); }
    }

    public string Name { get; private set; }

    public string Description { get; private set; }

    ...etc...
}`

And the Persistence-friendly object

[Table("TypeA")]
public class TypeADao : EntityDao
{
    public string Name { get; set; }

    public string Description { get; set; }

    public ICollection<TypeBDao> AssignedItems { get; set; }
}

With Automapper can easily Map the Entity to the Dao, but i fail to do the opposite, as i need to map AssignedItems to the private backing field _assignedItems in the Entity. How can i do that? Is there a way to map the AssignedItems collection to the private field called _assignedItems? Many thanks to all

Gnegno
  • 366
  • 1
  • 5
  • 15
  • What about this: http://stackoverflow.com/questions/8355024/automapper-mapping-properties-with-private-setters – Wojciech Kulik Apr 16 '15 at 12:37
  • Also this http://stackoverflow.com/questions/16911950/is-automapper-supposed-to-work-with-private-setters-oob?lq=1 – Steve Mitcham Apr 16 '15 at 12:43
  • The first one suggests to use a constructor injection. But i have in the constructor of TypeA the possibility to inject a collection of TypeB, not of TypeBDao, and i cannot create in TypeA an ad-hoc constructor as it would break the ddd rule of making the entity agnostic of persistence (in fact the TypeBDao is a persistence-related object). The latter states that it successfully map to properties with private setters, but my backing field have a different name (_assignedItems vs AssignedItems) and it will not be mapped – Gnegno Apr 16 '15 at 14:41
  • I have found a workaround by using the constructor and mapping (on the constructor) the TypeBDao to TypeB, but it seems not so clean...any other ideas other than that? =) – Gnegno Apr 16 '15 at 14:49
  • 1
    Why not provide private setter for the property, which sets the backing field. Then it will work. – Sunny Milenov Apr 16 '15 at 15:03
  • I'm a dork...thanks for the simple solution =) – Gnegno Apr 16 '15 at 16:28
  • 2
    Solution: http://stackoverflow.com/a/37407912/1360907 – Chris Xue May 24 '16 at 08:20

1 Answers1

5

I know this might come a bit too late, but should still be helpful for people who might encounter this problem in the future.

Here is how I solved the mapping private field problem.

// Please refer to https://github.com/AutoMapper/AutoMapper/issues/600
// Please refer to https://github.com/AutoMapper/AutoMapper/issues/946
ShouldMapField = fieldInfo => !fieldInfo.IsDefined(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute));
ShouldMapProperty = propertyInfo => true;
HExit
  • 696
  • 7
  • 17
  • I was looking for something very similar, but just mapping everything to backing fields. In which case if anybody reaches this, you should only need: ShouldMapField = _ => true; ShouldMapProperty = _ => false; – Marc Cayuela Mar 07 '23 at 20:11