LinqExpressionsMapper is LINQ extensions library for EntityFramework and other .NET LINQ ORMs. Here is sample project LinqExpressionsMapper.Samples You can find more info in wiki. Project is available on nuget.org.
- Projection Expression Factory
`ISelectExpression`
— Class implementing this interface implements `Expression> GetExpression()` method and in fact is factory of projection expression. If this method is used via _Mapper_ Class its result executed only _once_ because it is cached. `IDynamicSelectExpression`
— Class implementing this interface also implements `Expression> GetExpression()`. If this method is used via _Mapper_ Class than only _Factory_ _Delegate_ is cached.
`ISelectExpression`
— Class implementing this interface implements `Expression> GetExpression(TParam param)` method and in fact is factory of projection expression. If this method is used via _Mapper_ Class than _Factory_ _Delegate_ is cached. And this Expression can be received from _Mapper_ via `WithParam(param)` methods. 2. **Properties Mapping Delegate** `IPropertiesMapper` — Class implementing this interface implements `void MapProperties(TSource source, TDest dest)` method. If this method is used via _Mapper_ Class than mapping _Delegate_ is cached.
- Mapper.Register()
`Mapper.Register(/*IPropertiesMapper*/mapper);` or `Mapper.Register(/*ISelectExpression*/mapper);` or `Mapper.Register(/*IDynamicSelectExpression*/mapper);`
— Registers PropertiesMapper or SelectExpression in _Cache_, registered Mapping Delegates and Projection Expression Factories can be used later. 2. **Mapper.RegisterAll()**
`Mapper.RegisterAll(/*IMultipleMappings*/mapper);`
— Registers all implemented by _mapper_ _Type_ `IPropertiesMapper`,
`ISelectExpression`, ... interfaces in _Cache_. 3. **Mapper...GetExpression()**
`var expr = Mapper.From().To().Using().GetExpression();` or
`Expression> e = Mapper.From().To().Using();`
— Gets Expression from _Cache_ or receives _Expression_ _Factory_ from _TSelect_ instance. _TSelect_ should implement `ISelectExpression`.
`var expr = Mapper.From().To().GetExpression();` or
`Expression> expr = Mapper.From().To();`
— Gets Expression from _Cache_ or tries to receive _Expression_ _Factory_ from _TDest_ instance (if `ISelectExpression` is implemented). 4. **Mapper...Map()**
`var dest = Mapper.From(source).To().Using().Map();` or
`TDest dest = Mapper.From(source).To().Using();` or
`Mapper.From(source).To(dest).Using().Map();`
— Maps source to dest. Gets mapping Delegate from _Cache_ or receives it _Delegate_ from _TMapper_ instance. _TMapper_ should implement `IPropertiesMapper`.
`var dest = Mapper.From(source).To().Map();` or
`TDest dest = Mapper.From(source).To();` or
`Mapper.From(source).To(dest).Map();`
— Maps source to dest. Gets mapping Delegate from _Cache_ or tries to receive it from _TSource_ or _TDest_ instance (if `IPropertiesMapper` is implemented). 5. **Enumerable.Map().To()**
`IEnumerable result = enumerable.Map().To(m=>m.Using());`
— Maps IEnumerable of sources to IEnumerables of destanations. Gets mapping Delegate from _Cache_ or receives it from _TMapper_ instance. _TMapper_ should implement `IPropertiesMapper`.
`IEnumerable result = enumerable.Map().To();`
— Maps IEnumerable of sources to IEnumerables of destanations. Gets mapping Delegate from _Cache_ or tries to receive it from _TSource_ or _TDest_ instance (if `IPropertiesMapper` is implemented). 6. **Queryable.Project().To()**
`IQueryable reult = queryable.Project().To(p=>p.Using());`
— Projects IQueryable of sources to IQueryable of destanations. Gets Projection expression from _Cache_ or receives it from _TSelect_ instance. _TSelect_ should implement `ISelectExpression`.
`IQueryable result = queryable.Project().To();`
— Projects IQueryable of sources to IQueryable of destanations. Gets Projection expression from _Cache_ or tries to receive it from _TDest_ instance (if `ISelectExpression` is implemented).
Example:
Expression<Func<Student, StudentWithCoursesModel>> select = student =>
new StudentWithCoursesModel
{
MaxGade = student.Enrollments.Max(e => e.Grade),
Courses = Mapper.From<Course>().To<CourseBaseModel>().GetExpression()
.InvokeEnumerable(student.Enrollments.Select(er => er.Course)),
};
select = select.ApplyExpressions();
select = select.AddMemberInit(
s => s.Enrollments.Where(e => e.Grade <= Grade.C).Select(e => e.Course),
s => s.PositiveGradedCourses,
Mapper.From<Course>().To<CourseBaseModel>().GetExpression());
select = select.InheritInit(Mapper.From<Student>().To<StudentBaseModel>().GetExpression());
Result
//select = student => new StudentWithCoursesModel
//{
// StudentId = student.ID,
// FullName = student.FirstMidName + " " + student.LastName,
// MaxGade = student.Enrollments.Max(e => e.Grade),
// Courses = student.Enrollments.Select(er => er.Course)
// .Select(course => new CourseBaseModel
// {
// CourseId = course.CourseID,
// CourseName = course.Title,
// Credits = course.Credits
// }),
// PositiveGradedCourses = student.Enrollments.Where(e => e.Grade <= Grade.C)
// .Select(e => e.Course)
// .Select(course => new CourseBaseModel
// {
// CourseId = course.CourseID,
// CourseName = course.Title,
// Credits = course.Credits
// })
//};
- InheritInit
`select = select.InheritInit(baseInit);`
— Combines two Projection expressions into one. Projection of child class is extended with Projection of entity (base entity) on base class. Projection of members initialized in `baseInit` expression and not not initialized in init expression will be copied. For example: `StudentId = student.ID` from `baseExpression`. 2. **AddMemberInit**
` select = select.AddMemberInit(`
` s => s.Enrollments.Select(e => e.Course),/*sourceMember*/`
` s => s.PositiveGradedCourses,/*member*/`
` Mapper.From().To().GetExpression()/*memberInit*/);` — Adds Target _Single_(_Enumerable_) `member` initialization from _Single_(_Enumerable_) `sourceMember` using `memberInit` projection expression. 3. **ApplyExpressions** expressions **Invoke**, **InvokeEnumerable**
select = student => new StudentWithCoursesModel
{
Courses = student.Enrollments.Select(er => memberInit.Invoke(er.Course))
};
select = select.ApplyExpressions();
— expr.Invoke(param)
(expr.InvokeEnumerable(enumerableParam)
) creates placeholder for expr
invokation. It means that when ApplyExpressions()
method is called all Invoke
, InvokeEnumerable
methods are replaced with expr
body with param
argument.
LinqExpressionsMapper is LINQ extensions library for EntityFramework and other .NET LINQ ORMs. Here is sample project LinqExpressionsMapper.Samples You can find more info in wiki. Project is available on nuget.org.