-
Notifications
You must be signed in to change notification settings - Fork 26
Registration
Note 1: All registrations are one-directional. If you want to be able to Map a DTO back to Entity, you must register exclusively.
Note 2: All RegisterMap methods override existing registrations for same types -if exists.
- We can call non-generic method to register type mappings.
public IMapDefinition RegisterMap(Type inType, Type outType)
Here is how we create a mapping between Customer and CustomerDTO:
config.RegisterMap(typeof(Customer), typeof(CustomerDTO),
builder => builder.SkipMember("Address").MapMember("Phone", "WorkPhone")
);
- We can call generic register method and this way we can intercept the code-generation with expressions.
public IMapDefinition<TIn, TOut> RegisterMap<TIn, TOut>(Action<MapBuilder<TIn, TOut>> buildAction = null)
config.RegisterMap<Customer, CustomerDTO>(
builder => builder
.SkipMember(c => c.Address)
.MapMember(c => c.Phone, (c, mc) => c.WorkPhone)
);
Here, we excluded the "Address" member from mapping and fixed naming problem for "Phone".
- We can set our mapping code exclusively.
public IMapDefinition<TIn, TOut> RegisterMap<TIn, TOut>(Expression<Func<TIn, MapContext, TOut>> expression)
config.RegisterMap<Customer, CustomerDTO>((c, mc) => new CustomerDTO {
Id = c.Id,
Address = c.Address,
Phone = c.WorkPhone,
Detail = mc.Map<CustomerDetail, CustomerDetailDTO>(c.Detail)
});
mc is MapContext object. We can manually map navigation properties (like "Detail") or we can use MapContext. When we use MapContext, BatMap can exclude those navigations from query projections at runtime.
i.e. Let's say we have an IQueryable<Customer> and we convert it to IQueryable<CustomerDTO>. BatMap checks this query's included navigation properties (by default) and only expands those properties. For this feature to work, we must use MapContext methods:
var query1 = context.Customers.Include(c => c.Detail);
var dtoQuery1 = query.ProjectTo<CustomerDTO>(); // Detail will be included
var query2 = context.Customers;
var dtoQuery2 = query.ProjectTo<CustomerDTO>(); // Detail won't be included
- We can ask for a MapDefinition. When requested mapping is not registered yet, it checks MapConfiguration's dynamicMapping option, and behave accordingly.
public IMapDefinition<TIn, TOut> GetMapDefinition<TIn, TOut>()
- We can force BatMap to create a MapDefinition for us. This method does not have side effects (like GetMapDefinition method's registering a mapping dynamically).
public IMapDefinition<TIn, TOut> GenerateMapDefinition<TIn, TOut>(Action<MapBuilder<TIn, TOut>> buildAction = null)
Mapper static class has all of the methods above except GetMapDefinition and GenerateMapDefinition.
⏭ Mapping
Developed with ❤️ at Doğuş Teknoloji