Skip to content

Commit 074d4fd

Browse files
Merge pull request #11 from TylerCarlson1/5.0Compatible
Moving to IObjectMapExpression
2 parents 14708f4 + 6b2b286 commit 074d4fd

15 files changed

+101
-154
lines changed

global.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"sdk": {
33
"version": "1.0.0-preview1-002702"
4-
}
4+
},
5+
"projects": []
56
}

src/AutoMapper.Collection-Signed/project.json

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@
1818
},
1919

2020
"dependencies": {
21-
"AutoMapper": "4.2.1"
21+
"AutoMapper": "5.0.0"
2222
},
2323

2424
"frameworks": {
2525
"net45": {
26+
"dependencies": {
27+
}
2628
},
2729
"netstandard1.1": {
2830
"dependencies": {
29-
"NETStandard.Library": "1.5.0-rc2-24027",
30-
"System.Linq.Expressions": "4.0.11-rc2-24027"
31+
"System.Threading": "4.0.11"
3132
},
3233
"imports": [
3334
"portable-net45+win8+dnxcore50",
3435
"portable-net45+win8"
3536
]
37+
},
38+
"netstandard1.3": {
39+
"dependencies": {
40+
"System.Threading": "4.0.11"
41+
}
42+
}
3643
}
37-
}
38-
}
44+
}

src/AutoMapper.Collection.EntityFramework/Extensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ public static class Extensions
1515
/// <typeparam name="TSource">Source table type to be updated</typeparam>
1616
/// <param name="source">DbSet to be updated</param>
1717
/// <returns>Persistance object to Update or Remove data</returns>
18-
[Obsolete("Use version that passes instance of IMapper")]
1918
public static IPersistance Persist<TSource>(this DbSet<TSource> source)
2019
where TSource : class
2120
{
22-
return new Persistance<TSource>(source, null);
21+
return new Persistance<TSource>(source, Mapper.Instance);
2322
}
2423

2524
/// <summary>

src/AutoMapper.Collection.EntityFramework/GenerateEntityFrameworkPrimaryKeyEquivilentExpressions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ public class GenerateEntityFrameworkPrimaryKeyEquivilentExpressions<TDatabaseCon
1616
/// </summary>
1717
/// <param name="mapper">IMapper used to find TypeMap between classes</param>
1818
public GenerateEntityFrameworkPrimaryKeyEquivilentExpressions(IMapper mapper)
19-
: base(new GenerateEntityFrameworkPrimaryKeyPropertyMaps<TDatabaseContext>(mapper))
19+
: base(new GenerateEntityFrameworkPrimaryKeyPropertyMaps<TDatabaseContext>(mapper.ConfigurationProvider))
2020
{
2121
}
2222

2323
/// <summary>
2424
/// Generate EquivilencyExpressions based on EnityFramework's primary key
2525
/// Uses static API's Mapper for finding TypeMap between classes
2626
/// </summary>
27-
[Obsolete("Use version that passes instance of IMapper")]
2827
public GenerateEntityFrameworkPrimaryKeyEquivilentExpressions()
29-
: base(new GenerateEntityFrameworkPrimaryKeyPropertyMaps<TDatabaseContext>(null))
28+
: base(new GenerateEntityFrameworkPrimaryKeyPropertyMaps<TDatabaseContext>(Mapper.Configuration))
3029
{
3130
}
3231
}

src/AutoMapper.Collection.EntityFramework/GenerateEntityFrameworkPrimaryKeyPropertyMatches.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ namespace AutoMapper.EntityFramework
1212
public class GenerateEntityFrameworkPrimaryKeyPropertyMaps<TDatabaseContext> : IGeneratePropertyMaps
1313
where TDatabaseContext : IObjectContextAdapter, new()
1414
{
15-
private readonly IMapper _mapper;
15+
private readonly IConfigurationProvider _configurationProvider;
1616

1717
private readonly TDatabaseContext _context = new TDatabaseContext();
1818
private readonly MethodInfo _createObjectSetMethodInfo = typeof(ObjectContext).GetMethod("CreateObjectSet", Type.EmptyTypes);
1919

20-
public GenerateEntityFrameworkPrimaryKeyPropertyMaps(IMapper mapper)
20+
public GenerateEntityFrameworkPrimaryKeyPropertyMaps(IConfigurationProvider configurationProvider)
2121
{
22-
_mapper = mapper;
22+
_configurationProvider = configurationProvider;
2323
}
2424

2525
public IEnumerable<PropertyMap> GeneratePropertyMaps(Type srcType, Type destType)
2626
{
27-
var typeMap = _mapper == null
28-
? (Mapper.Configuration as IConfigurationProvider).ResolveTypeMap(srcType, destType)
29-
: _mapper.ConfigurationProvider.ResolveTypeMap(srcType, destType);
27+
var typeMap = _configurationProvider.ResolveTypeMap(srcType, destType);
3028
var propertyMaps = typeMap.GetPropertyMaps();
3129
var createObjectSetMethod = _createObjectSetMethodInfo.MakeGenericMethod(destType);
3230
dynamic objectSet = createObjectSetMethod.Invoke(_context.ObjectContext, null);

src/AutoMapper.Collection.LinqToSQL/GetLinqToSQLPrimaryKeyExpression.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ public class GetLinqToSQLPrimaryKeyExpression : GenerateEquivilentExpressionsBas
1010
/// </summary>
1111
/// <param name="mapper">IMapper used to find TypeMap between classes</param>
1212
public GetLinqToSQLPrimaryKeyExpression(IMapper mapper)
13-
: base(new GetLinqToSQLPrimaryKeyProperties(mapper))
13+
: base(new GetLinqToSQLPrimaryKeyProperties(mapper.ConfigurationProvider))
1414
{
1515
}
1616

1717
/// <summary>
1818
/// Generate EquivilencyExpressions based on LinqToSQL's primary key
1919
/// Uses static API's Mapper for finding TypeMap between classes
2020
/// </summary>
21-
[Obsolete("Use version that passes instance of IMapper")]
2221
public GetLinqToSQLPrimaryKeyExpression()
23-
: base(new GetLinqToSQLPrimaryKeyProperties(null))
22+
: base(new GetLinqToSQLPrimaryKeyProperties(Mapper.Configuration))
2423
{
2524
}
2625
}

src/AutoMapper.Collection.LinqToSQL/GetLinqToSQLPrimaryKeyProperties.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace AutoMapper.Collection.LinqToSQL
99
{
1010
public class GetLinqToSQLPrimaryKeyProperties : IGeneratePropertyMaps
1111
{
12-
private readonly IMapper _mapper;
13-
14-
public GetLinqToSQLPrimaryKeyProperties(IMapper mapper)
12+
private readonly IConfigurationProvider _configurationProvider;
13+
14+
public GetLinqToSQLPrimaryKeyProperties(IConfigurationProvider configurationProvider)
1515
{
16-
_mapper = mapper;
16+
if (configurationProvider == null)
17+
throw new ArgumentNullException(nameof(configurationProvider));
18+
_configurationProvider = configurationProvider;
1719
}
1820

1921
public IEnumerable<PropertyMap> GeneratePropertyMaps(Type srcType, Type destType)
2022
{
21-
var typeMap = _mapper == null
22-
? (Mapper.Configuration as IConfigurationProvider).ResolveTypeMap(srcType, destType)
23-
: _mapper.ConfigurationProvider.ResolveTypeMap(srcType, destType);
23+
var typeMap = _configurationProvider.ResolveTypeMap(srcType, destType);
2424
var propertyMaps = typeMap.GetPropertyMaps();
2525

2626
var primaryKeyPropertyMatches = destType.GetProperties().Where(IsPrimaryKey).Select(m => propertyMaps.FirstOrDefault(p => p.DestinationProperty.Name == m.Name)).ToList();

src/AutoMapper.Collection.LinqToSQL/PersistanceExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ public static class PersistanceExtensions
1515
/// <typeparam name="TSource">Source table type to be updated</typeparam>
1616
/// <param name="source">Table to be updated</param>
1717
/// <returns>Persistance object to Update or Remove data</returns>
18-
[Obsolete("Use version that passes instance of IMapper")]
1918
public static IPersistance Persist<TSource>(this Table<TSource> source)
2019
where TSource : class
2120
{
22-
return new Persistance<TSource>(source, null);
21+
return new Persistance<TSource>(source, Mapper.Instance);
2322
}
2423

2524
/// <summary>

src/AutoMapper.Collection/Equivilency Expression/CustomExpressionVisitor.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Linq.Expressions;
55
using System.Reflection;
6+
using AutoMapper.Execution;
67

78
namespace AutoMapper.EquivilencyExpression
89
{
@@ -29,11 +30,8 @@ protected override Expression VisitMember(MemberExpression node)
2930
var matchPM = _propertyMaps.FirstOrDefault(pm => pm.DestinationProperty.MemberInfo == node.Member);
3031
if (matchPM == null)
3132
throw new Exception("No matching PropertyMap");
32-
var sourceValueResolvers = matchPM.GetSourceValueResolvers();
33-
if (!sourceValueResolvers.All(r => r is IMemberGetter))
34-
throw new Exception("Not all member getters");
35-
36-
var memberGetters = sourceValueResolvers.OfType<IMemberGetter>();
33+
var memberGetters = matchPM.SourceMembers;
34+
3735
var memberExpression = Expression.Property(Visit(node.Expression), memberGetters.First().MemberInfo as PropertyInfo);
3836

3937
foreach (var memberGetter in memberGetters.Skip(1))

src/AutoMapper.Collection/Equivilency Expression/EquivilentExpression.cs

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Linq.Expressions;
33
using System.Reflection;
4+
using AutoMapper.Execution;
45

56
namespace AutoMapper.EquivilencyExpression
67
{
@@ -12,61 +13,33 @@ static EquivilentExpression()
1213
{
1314
BadValue = new EquivilentExpression();
1415
}
15-
16-
public bool IsEquivlent(object source, object destination)
17-
{
18-
throw new NotImplementedException();
19-
}
2016
}
2117

22-
internal class EquivilentExpression<TSource,TDestination> : IEquivilentExpression , IToSingleSourceEquivalentExpression
18+
internal class EquivilentExpression<TSource,TDestination> : IEquivilentExpression<TSource, TDestination>
2319
where TSource : class
2420
where TDestination : class
2521
{
2622
private readonly Expression<Func<TSource, TDestination, bool>> _equivilentExpression;
23+
private readonly Func<TSource, TDestination, bool> _equivilentFunc;
2724

2825
public EquivilentExpression(Expression<Func<TSource,TDestination,bool>> equivilentExpression)
2926
{
3027
_equivilentExpression = equivilentExpression;
28+
_equivilentFunc = _equivilentExpression.Compile();
3129
}
3230

33-
public bool IsEquivlent(object source, object destination)
31+
public bool IsEquivlent(TSource source, TDestination destination)
3432
{
35-
if(!(source is TSource))
36-
throw new EquivilentExpressionNotOfTypeException(source.GetType(), typeof(TSource));
37-
if (!(destination is TDestination))
38-
throw new EquivilentExpressionNotOfTypeException(destination.GetType(), typeof(TDestination));
39-
return _equivilentExpression.Compile()(source as TSource, destination as TDestination);
33+
return _equivilentFunc(source, destination);
4034
}
4135

42-
public Expression ToSingleSourceExpression(object source)
36+
public Expression<Func<TDestination, bool>> ToSingleSourceExpression(TSource source)
4337
{
4438
if (source == null)
4539
throw new Exception("Invalid somehow");
46-
return GetSourceOnlyExpresison(source as TSource);
47-
}
4840

49-
internal Expression GetSourceOnlyExpresison(TSource source)
50-
{
5141
var expression = new ParametersToConstantVisitor<TSource>(source).Visit(_equivilentExpression) as LambdaExpression;
52-
return Expression.Lambda(expression.Body, _equivilentExpression.Parameters[1]);
53-
}
54-
55-
private class EquivilentExpressionNotOfTypeException : Exception
56-
{
57-
private readonly Type _objectType;
58-
private readonly Type _expectedType;
59-
60-
public EquivilentExpressionNotOfTypeException(Type objectType, Type expectedType)
61-
{
62-
_objectType = objectType;
63-
_expectedType = expectedType;
64-
}
65-
66-
public override string Message
67-
{
68-
get { return string.Format("{0} does not equal or inherit from {1}", _objectType.Name, _expectedType.Name); }
69-
}
42+
return Expression.Lambda<Func<TDestination, bool>>(expression.Body, _equivilentExpression.Parameters[1]);
7043
}
7144
}
7245

0 commit comments

Comments
 (0)