From b00c5d20d6cbe768b4e9dbed8bcc4dee4d2e1c6c Mon Sep 17 00:00:00 2001 From: Yuriy Anisimov Date: Sun, 31 Jan 2016 18:22:39 -0800 Subject: [PATCH] Ability to specify compilation type on more granular level - Registration #72 --- ExpressMapper NET40/CompilationTypes.cs | 6 +- ExpressMapper NET40/DestinationTypeMapper.cs | 4 ++ ExpressMapper NET40/IMappingService.cs | 2 +- ExpressMapper NET40/IMemberConfiguration.cs | 1 + ExpressMapper NET40/ITypeMapper.cs | 4 +- ExpressMapper NET40/MappingServiceBase.cs | 4 +- ExpressMapper NET40/MappingServiceProvider.cs | 57 +++++-------------- ExpressMapper NET40/MemberConfiguration.cs | 9 +++ ExpressMapper NET40/SourceMappingService.cs | 2 +- ExpressMapper NET40/SourceTypeMapper.cs | 4 ++ ExpressMapper NET40/TypeMapperBase.cs | 26 +++++++-- 11 files changed, 64 insertions(+), 55 deletions(-) diff --git a/ExpressMapper NET40/CompilationTypes.cs b/ExpressMapper NET40/CompilationTypes.cs index dc71a24..2778db9 100644 --- a/ExpressMapper NET40/CompilationTypes.cs +++ b/ExpressMapper NET40/CompilationTypes.cs @@ -1,8 +1,10 @@ -namespace ExpressMapper +using System; + +namespace ExpressMapper { + [Flags] public enum CompilationTypes { - All = 1, OnlySource = 2, OnlyDestination = 4 } diff --git a/ExpressMapper NET40/DestinationTypeMapper.cs b/ExpressMapper NET40/DestinationTypeMapper.cs index 1159f92..facf74d 100644 --- a/ExpressMapper NET40/DestinationTypeMapper.cs +++ b/ExpressMapper NET40/DestinationTypeMapper.cs @@ -15,6 +15,10 @@ public class DestinationTypeMapper : TypeMapperBase, ITypeMapper IMemberConfiguration Ignore(Expression> dest); IMemberConfiguration Value(Expression> dest, TNMember value); IMemberConfiguration CaseSensetiveMemberMap(bool caseSensitive); + IMemberConfiguration CompileTo(CompilationTypes compilationType); } } diff --git a/ExpressMapper NET40/ITypeMapper.cs b/ExpressMapper NET40/ITypeMapper.cs index d5c011b..903a9f9 100644 --- a/ExpressMapper NET40/ITypeMapper.cs +++ b/ExpressMapper NET40/ITypeMapper.cs @@ -9,7 +9,7 @@ public interface ITypeMapper Expression QueryableGeneralExpression { get; } Func GetNonGenericMapFunc(); Tuple, ParameterExpression, ParameterExpression> GetMapExpressions(); - void Compile(); + void Compile(CompilationTypes compilationtype, bool forceByDemand = false); } /// @@ -23,11 +23,13 @@ public interface ITypeMapper : ITypeMapper TN MapTo(T src, TN dest); void Ignore(Expression> left); void CaseSensetiveMemberMap(bool caseSensitive); + void CompileTo(CompilationTypes compileType); void MapMember(Expression> left, Expression> right); void MapFunction(Expression> left, Func right); void InstantiateFunc(Func constructor); void Instantiate(Expression> constructor); void BeforeMap(Action beforeMap); void AfterMap(Action afterMap); + CompilationTypes MapperType { get; } } } diff --git a/ExpressMapper NET40/MappingServiceBase.cs b/ExpressMapper NET40/MappingServiceBase.cs index fe73a60..9c1d84e 100644 --- a/ExpressMapper NET40/MappingServiceBase.cs +++ b/ExpressMapper NET40/MappingServiceBase.cs @@ -30,12 +30,12 @@ public void Reset() TypeMappers.Clear(); } - public void Compile() + public void Compile(CompilationTypes compilationType) { var typeMappers = new Dictionary(TypeMappers); foreach (var typeMapper in typeMappers) { - typeMapper.Value.Compile(); + typeMapper.Value.Compile(compilationType); } } diff --git a/ExpressMapper NET40/MappingServiceProvider.cs b/ExpressMapper NET40/MappingServiceProvider.cs index 896d152..04f72d0 100644 --- a/ExpressMapper NET40/MappingServiceProvider.cs +++ b/ExpressMapper NET40/MappingServiceProvider.cs @@ -49,7 +49,7 @@ public IQueryable Project(IQueryable source) var mapper = typeMapper as ITypeMapper; if (mapper.QueryableExpression == null) { - mapper.Compile(); + mapper.Compile(CompilationTypes.OnlySource); } return source.Select(mapper.QueryableExpression); } @@ -106,7 +106,7 @@ public void Compile() { foreach (var mappingService in _mappingServices) { - mappingService.Compile(); + mappingService.Compile(CompilationTypes.OnlySource | CompilationTypes.OnlyDestination); } } } @@ -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); } } } @@ -160,27 +139,19 @@ public void PrecompileCollection(CompilationTypes compilationType) { foreach (var mappingService in _mappingServices) { - switch (compilationType) + if ((CompilationTypes.OnlySource & compilationType) == CompilationTypes.OnlySource) + { + if (!mappingService.DestinationSupport) + { + mappingService.PrecompileCollection(); + } + } + if ((CompilationTypes.OnlyDestination & compilationType) == CompilationTypes.OnlyDestination) { - case CompilationTypes.All: + if (mappingService.DestinationSupport) + { mappingService.PrecompileCollection(); - break; - case CompilationTypes.OnlySource: - { - if (!mappingService.DestinationSupport) - { - mappingService.PrecompileCollection(); - } - break; - } - case CompilationTypes.OnlyDestination: - { - if (mappingService.DestinationSupport) - { - mappingService.PrecompileCollection(); - } - break; - } + } } } } diff --git a/ExpressMapper NET40/MemberConfiguration.cs b/ExpressMapper NET40/MemberConfiguration.cs index cfbad3f..476b852 100644 --- a/ExpressMapper NET40/MemberConfiguration.cs +++ b/ExpressMapper NET40/MemberConfiguration.cs @@ -141,5 +141,14 @@ public IMemberConfiguration CaseSensetiveMemberMap(bool caseSensitive) } return this; } + + public IMemberConfiguration CompileTo(CompilationTypes compilationType) + { + foreach (var typeMapper in _typeMappers) + { + typeMapper.CompileTo(compilationType); + } + return this; + } } } diff --git a/ExpressMapper NET40/SourceMappingService.cs b/ExpressMapper NET40/SourceMappingService.cs index ba753a3..0782a89 100644 --- a/ExpressMapper NET40/SourceMappingService.cs +++ b/ExpressMapper NET40/SourceMappingService.cs @@ -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; } diff --git a/ExpressMapper NET40/SourceTypeMapper.cs b/ExpressMapper NET40/SourceTypeMapper.cs index 6523d38..ea7ce62 100644 --- a/ExpressMapper NET40/SourceTypeMapper.cs +++ b/ExpressMapper NET40/SourceTypeMapper.cs @@ -14,6 +14,10 @@ public class SourceTypeMapper : TypeMapperBase, ITypeMapper 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 = diff --git a/ExpressMapper NET40/TypeMapperBase.cs b/ExpressMapper NET40/TypeMapperBase.cs index cc21145..73c127d 100644 --- a/ExpressMapper NET40/TypeMapperBase.cs +++ b/ExpressMapper NET40/TypeMapperBase.cs @@ -24,7 +24,8 @@ public abstract class TypeMapperBase protected Dictionary AutoMembers = new Dictionary(); protected List> CustomMembers = new List>(); protected List> CustomFunctionMembers = new List>(); - public Expression> QueryableExpression { get; protected set; } + public Expression> QueryableExpression { get; protected set; } + public abstract CompilationTypes MapperType { get; } public Expression QueryableGeneralExpression { @@ -62,7 +63,10 @@ protected TypeMapperBase(IMappingService service, IMappingServiceProvider servic protected Expression> ConstructorExp { get; set; } protected Func 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 @@ -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; @@ -126,7 +141,7 @@ public Tuple, ParameterExpression, ParameterExpression> GetMapE return new Tuple, ParameterExpression, ParameterExpression>(new List(RecursiveExpressionResult), SourceParameter, DestFakeParameter); } - Compile(); + Compile(MapperType); return new Tuple, ParameterExpression, ParameterExpression>(new List(ResultExpressionList), SourceParameter, DestFakeParameter); ; } @@ -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);