Skip to content

The project is similar to AutoMapper, but implements projections with LinqExpression not Reflection

Notifications You must be signed in to change notification settings

baio/DataAvail.LinqMapper

Repository files navigation

Important!

Some time ago I moved it in this project, https://github.com/baio/DataAvail.Framework. Latest version is now there, the project now is a part of DataAvail.Framework solution. This solution integrate utilities to work with data Services such as CORS, JSON support, web service autorization via standard ASP auth mechanism and other utilities. You can separate LinqMapper project from there if you want. In case of any questions feel free to contact!

Problem

The Jimmy Bogard’s library AutoMapper is an excellent tool but unfortunately it doesn’t support projections via Linq - see description of the problem by Jimmy himself here. This library is attempt to naive solution of the problem defined. The top layer of this library is intentionally made similar to one of AutoMapper, but internals are very simplified and now supports only scare of the whole projections available in LINQ. Create projections

Similar to AutoMapper library the mapping by projection is created via

LinqMapper.CreateMap<SrcType, DestType>()

The line above create default mapping between type SrcType and DestType. Default mapping follow these rules:

  1. Properties with the same name and type (simple) are mapped.
  2. Properties with the same name and type (complex, if there is defined mapping for these types) are mapped.
  3. Properties with the same name and both derived from IEnumerable of complex type (where defined mapping between them) are mapped

Now you can add additional mapping rules to the defaults.

Custom mapping rules

###MapFrom Map destination property from source’s property defined in option. LinqMapper.CreateMap<SrcType, DestType>() .MapFrom(p => p.DestName, opt => opt.MapFrom(s => s.SrcName));

###Ignore Ignore mapping for property. LinqMapper.CreateMap<SrcType, DestType>() .MapFrom(p => p.DestName, opt => opt.Ignore());

###ResolveUsing Custom mapping between source and destination. LinqMapper.CreateMap<SrcType, DestType>() .MapFrom(p => p.DestName, opt => opt.ResolveUsing( s => s.SrcName + “I see you”));

Custom mapping should be defined in fashion known by underlying LINQ provider. Mapping

IQueryable<TSrc> dest = LinqMapper.Map<TSrc, TDest>(IQueryable<TSrc> Src, params string [] Expands)

Map source query to destination by defined rules.

The second parameter define which complex properties should be projected for query. For example if Item source entity has Parent property (also entity) - one should be defined in the list of the Expands in order to be selected from underlying data source (same for the properties with enumerable types).

LinqMapper.Map<TSrc, TDest>(srcQueryable, “Parent”)

About

The project is similar to AutoMapper, but implements projections with LinqExpression not Reflection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages