Skip to content

Commit

Permalink
Ability to specify compilation type on more granular level - Registra…
Browse files Browse the repository at this point in the history
…tion #72
  • Loading branch information
anisimovyuriy committed Feb 1, 2016
1 parent dc18e59 commit b00c5d2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 55 deletions.
6 changes: 4 additions & 2 deletions ExpressMapper NET40/CompilationTypes.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace ExpressMapper
using System;

namespace ExpressMapper
{
[Flags]
public enum CompilationTypes
{
All = 1,
OnlySource = 2,
OnlyDestination = 4
}
Expand Down
4 changes: 4 additions & 0 deletions ExpressMapper NET40/DestinationTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public class DestinationTypeMapper<T, TN> : TypeMapperBase<T, TN>, ITypeMapper<T

public DestinationTypeMapper(IMappingService service, IMappingServiceProvider serviceProvider) : base(service, serviceProvider){}

public override CompilationTypes MapperType {
get { return CompilationTypes.OnlyDestination;}
}

protected override void InitializeRecursiveMappings(IMappingServiceProvider serviceProvider)
{
var mapMethod =
Expand Down
2 changes: 1 addition & 1 deletion ExpressMapper NET40/IMappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface IMappingService
BlockExpression MapProperty(Type srcType, Type destType, Expression srcExpression, Expression destExpression, bool newDest);
Expression GetMemberMappingExpression(Expression left, Expression right, bool newDest);
Expression GetMemberQueryableExpression(Type srcType, Type dstType);
void Compile();
void Compile(CompilationTypes compilationType);
}
}
1 change: 1 addition & 0 deletions ExpressMapper NET40/IMemberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public interface IMemberConfiguration<T, TN>
IMemberConfiguration<T, TN> Ignore<TMember>(Expression<Func<TN, TMember>> dest);
IMemberConfiguration<T, TN> Value<TNMember>(Expression<Func<TN, TNMember>> dest, TNMember value);
IMemberConfiguration<T, TN> CaseSensetiveMemberMap(bool caseSensitive);
IMemberConfiguration<T, TN> CompileTo(CompilationTypes compilationType);
}
}
4 changes: 3 additions & 1 deletion ExpressMapper NET40/ITypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ITypeMapper
Expression QueryableGeneralExpression { get; }
Func<object, object, object> GetNonGenericMapFunc();
Tuple<List<Expression>, ParameterExpression, ParameterExpression> GetMapExpressions();
void Compile();
void Compile(CompilationTypes compilationtype, bool forceByDemand = false);
}

/// <summary>
Expand All @@ -23,11 +23,13 @@ public interface ITypeMapper<T, TN> : ITypeMapper
TN MapTo(T src, TN dest);
void Ignore<TMember>(Expression<Func<TN, TMember>> left);
void CaseSensetiveMemberMap(bool caseSensitive);
void CompileTo(CompilationTypes compileType);
void MapMember<TMember, TNMember>(Expression<Func<TN, TNMember>> left, Expression<Func<T, TMember>> right);
void MapFunction<TMember, TNMember>(Expression<Func<TN, TNMember>> left, Func<T, TMember> right);
void InstantiateFunc(Func<T,TN> constructor);
void Instantiate(Expression<Func<T,TN>> constructor);
void BeforeMap(Action<T,TN> beforeMap);
void AfterMap(Action<T,TN> afterMap);
CompilationTypes MapperType { get; }
}
}
4 changes: 2 additions & 2 deletions ExpressMapper NET40/MappingServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public void Reset()
TypeMappers.Clear();
}

public void Compile()
public void Compile(CompilationTypes compilationType)
{
var typeMappers = new Dictionary<long, ITypeMapper>(TypeMappers);
foreach (var typeMapper in typeMappers)
{
typeMapper.Value.Compile();
typeMapper.Value.Compile(compilationType);
}
}

Expand Down
57 changes: 14 additions & 43 deletions ExpressMapper NET40/MappingServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IQueryable<TN> Project<T, TN>(IQueryable<T> source)
var mapper = typeMapper as ITypeMapper<T, TN>;
if (mapper.QueryableExpression == null)
{
mapper.Compile();
mapper.Compile(CompilationTypes.OnlySource);
}
return source.Select(mapper.QueryableExpression);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public void Compile()
{
foreach (var mappingService in _mappingServices)
{
mappingService.Compile();
mappingService.Compile(CompilationTypes.OnlySource | CompilationTypes.OnlyDestination);
}
}
}
Expand All @@ -117,28 +117,7 @@ public void Compile(CompilationTypes compilationType)
{
foreach (var mappingService in _mappingServices)
{
switch (compilationType)
{
case CompilationTypes.All:
mappingService.Compile();
break;
case CompilationTypes.OnlySource:
{
if (!mappingService.DestinationSupport)
{
mappingService.Compile();
}
break;
}
case CompilationTypes.OnlyDestination:
{
if (mappingService.DestinationSupport)
{
mappingService.Compile();
}
break;
}
}
mappingService.Compile(compilationType);
}
}
}
Expand All @@ -160,27 +139,19 @@ public void PrecompileCollection<T, TN>(CompilationTypes compilationType)
{
foreach (var mappingService in _mappingServices)
{
switch (compilationType)
if ((CompilationTypes.OnlySource & compilationType) == CompilationTypes.OnlySource)
{
if (!mappingService.DestinationSupport)
{
mappingService.PrecompileCollection<T, TN>();
}
}
if ((CompilationTypes.OnlyDestination & compilationType) == CompilationTypes.OnlyDestination)
{
case CompilationTypes.All:
if (mappingService.DestinationSupport)
{
mappingService.PrecompileCollection<T, TN>();
break;
case CompilationTypes.OnlySource:
{
if (!mappingService.DestinationSupport)
{
mappingService.PrecompileCollection<T, TN>();
}
break;
}
case CompilationTypes.OnlyDestination:
{
if (mappingService.DestinationSupport)
{
mappingService.PrecompileCollection<T, TN>();
}
break;
}
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions ExpressMapper NET40/MemberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,14 @@ public IMemberConfiguration<T, TN> CaseSensetiveMemberMap(bool caseSensitive)
}
return this;
}

public IMemberConfiguration<T, TN> CompileTo(CompilationTypes compilationType)
{
foreach (var typeMapper in _typeMappers)
{
typeMapper.CompileTo(compilationType);
}
return this;
}
}
}
2 changes: 1 addition & 1 deletion ExpressMapper NET40/SourceMappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Expression GetMemberQueryableExpression(Type srcType, Type dstType)
var typeMapper = TypeMappers[cacheKey];
if (typeMapper.QueryableGeneralExpression == null)
{
typeMapper.Compile();
typeMapper.Compile(CompilationTypes.OnlySource);
}
return typeMapper.QueryableGeneralExpression;
}
Expand Down
4 changes: 4 additions & 0 deletions ExpressMapper NET40/SourceTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class SourceTypeMapper<T, TN> : TypeMapperBase<T, TN>, ITypeMapper<T, TN>

public SourceTypeMapper(IMappingService service, IMappingServiceProvider serviceProvider) : base(service, serviceProvider) {}

public override CompilationTypes MapperType {
get { return CompilationTypes.OnlySource;}
}

protected override void InitializeRecursiveMappings(IMappingServiceProvider serviceProvider)
{
var mapMethod =
Expand Down
26 changes: 21 additions & 5 deletions ExpressMapper NET40/TypeMapperBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public abstract class TypeMapperBase<T, TN>
protected Dictionary<MemberInfo, MemberInfo> AutoMembers = new Dictionary<MemberInfo, MemberInfo>();
protected List<KeyValuePair<MemberExpression, Expression>> CustomMembers = new List<KeyValuePair<MemberExpression, Expression>>();
protected List<KeyValuePair<MemberExpression, Expression>> CustomFunctionMembers = new List<KeyValuePair<MemberExpression, Expression>>();
public Expression<Func<T, TN>> QueryableExpression { get; protected set; }
public Expression<Func<T, TN>> QueryableExpression { get; protected set; }
public abstract CompilationTypes MapperType { get; }

public Expression QueryableGeneralExpression
{
Expand Down Expand Up @@ -62,7 +63,10 @@ protected TypeMapperBase(IMappingService service, IMappingServiceProvider servic
protected Expression<Func<T, TN>> ConstructorExp { get; set; }
protected Func<object, object, object> NonGenericMapFunc { get; set; }
protected bool CaseSensetiveMember { get; set; }
protected bool CaseSensetiveOverride { get; set; }
protected bool CaseSensetiveOverride { get; set; }

protected CompilationTypes CompilationTypeMember { get; set; }
protected bool CompilationTypeOverride { get; set; }

#endregion

Expand All @@ -72,10 +76,21 @@ public void CaseSensetiveMemberMap(bool caseSensitive)
{
CaseSensetiveMember = caseSensitive;
CaseSensetiveOverride = true;
}

public void CompileTo(CompilationTypes compileType)
{
CompilationTypeMember = compileType;
CompilationTypeOverride = true;
}

public void Compile()
public void Compile(CompilationTypes compilationType, bool forceByDemand = false)
{
if (!forceByDemand && ((CompilationTypeOverride && (MapperType & CompilationTypeMember) != MapperType) || (!CompilationTypeOverride && (MapperType & compilationType) != MapperType)))
{
return;
}

if (_compiling)
{
return;
Expand Down Expand Up @@ -126,7 +141,7 @@ public Tuple<List<Expression>, ParameterExpression, ParameterExpression> GetMapE
return new Tuple<List<Expression>, ParameterExpression, ParameterExpression>(new List<Expression>(RecursiveExpressionResult), SourceParameter, DestFakeParameter);
}

Compile();
Compile(MapperType);
return new Tuple<List<Expression>, ParameterExpression, ParameterExpression>(new List<Expression>(ResultExpressionList), SourceParameter, DestFakeParameter); ;
}

Expand Down Expand Up @@ -308,7 +323,8 @@ public TN MapTo(T src, TN dest)
{
lock (_lockObject)
{
Compile();
// force compilation by client code demand
Compile(MapperType, forceByDemand: true);
}
}
return ResultMapFunction(src, dest);
Expand Down

0 comments on commit b00c5d2

Please sign in to comment.