Automapper: Private/internal destination properties

Created on 6 Oct 2014  路  4Comments  路  Source: AutoMapper/AutoMapper

Hi,
is it possible to have private or at least internal destination properties in DTOs?

For example:

``` C#
public class Company{
public int ID {get;set;}
}

public class CompanyDTO{
public int ID {get;set;}
}

public class User {
public int ID {get;set;}
public Company Company {get;set;}
}

public class UserDTO{
public int ID {get;set;}
internal CompanyDTO Company{get;set;}
public int? CompanyID{
get { return (this.Company != null) ? this.Company.ID : (int?)null;
}
}
```

Currently it only works if I set the CompanyDTO property to public.
Mapping is done in the same assembly.

I'm using this approach because of NHibernate and lazy loading. I have a custom resolver which ignores uninitialized objects and doesn't trigger lazy load on them.

Most helpful comment

for even newer versions
```
public class PrivateInternalProfile : Profile {
public PrivateInternalProfile() {
ShouldMapField = fieldInfo => true;
ShouldMapProperty = propertyInfo => true;
CreateMap(); //etc
}
}

All 4 comments

Yes, you can. In your base Profile, set the BindingFlags to something else before creating your maps:

public class PrivateInternalProfile {
    protected override Configure() {
        BindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
        CreateMap<User, UserDto>(); //etc
    }
}

For newer versions:

public class PrivateInternalProfile {
    protected override Configure() {
        ShouldMapField = fieldInfo => true;
        ShouldMapProperty = propertyInfo => true;
        CreateMap<User, UserDto>(); //etc
    }
}

for even newer versions
```
public class PrivateInternalProfile : Profile {
public PrivateInternalProfile() {
ShouldMapField = fieldInfo => true;
ShouldMapProperty = propertyInfo => true;
CreateMap(); //etc
}
}

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings