Skip to content

Commit

Permalink
Merge pull request #55 from AutoMapper/HashingMembers
Browse files Browse the repository at this point in the history
GetHashCode call by type
  • Loading branch information
TylerCarlson1 authored Jul 12, 2017
2 parents 60bc1a0 + eda7e98 commit 07a6f58
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
78 changes: 78 additions & 0 deletions src/AutoMapper.Collection.Tests/MapCollectionWithEqualityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,84 @@ public void Should_Work_With_Null_Destination()
Mapper.Map<List<Thing>>(dtos).Should().HaveSameCount(dtos);
}

public void Should_Work_With_Comparing_String_Types()
{
Mapper.Initialize(cfg =>
{
cfg.AddCollectionMappers();
cfg.CreateMap<Charge, SaleCharge>()
.ForMember(d => d.SaleId, o => o.Ignore())
.EqualityComparison((c, sc) => sc.Category == c.Category && sc.Description == c.Description);
cfg.CreateMap<SaleCharge, Charge>()
.ConstructUsing(
(saleCharge => new Charge(saleCharge.Category, saleCharge.Description, saleCharge.Value)))
.EqualityComparison((sc, c) => sc.Category == c.Category && sc.Description == c.Description);
});

var dto = new Charge("catagory", "description", 5);
var entity = new SaleCharge { Category = dto.Category, Description = dto.Description };
var entityCollection = new List<SaleCharge> { entity };

Mapper.Map(new[] { dto }, entityCollection);

entity.ShouldBeEquivalentTo(entityCollection[0]);
}

public class Charge
{
public Charge(string category, string description, decimal value)
{
Category = category;
Description = description;
Value = value;
}

public string Category { get; }
public string Description { get; }
public decimal Value { get; }

public override string ToString()
{
return $"{Category}|{Description}|{Value}";
}

public override int GetHashCode()
{
return $"{Category}|{Description}|{Value}".GetHashCode();
}

public override bool Equals(object obj)
{
if (ReferenceEquals(this, obj))
{
return true;
}

if (ReferenceEquals(null, obj))
{
return false;
}

var _obj = obj as Charge;

if (_obj == null)
{
return false;
}

return Category == _obj.Category && Description == _obj.Description && Value == _obj.Value;
}
}

public class SaleCharge
{
public Guid SaleId { get; set; }
public string Category { get; set; }
public string Description { get; set; }
public decimal Value { get; set; }
}

public void Should_Be_Instanced_Based()
{
Mapper.Initialize(x =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,11 @@ public static Expression<Func<T, int>> GetHashCodeExpression<T>(this List<Expres
var returnTarget = Expression.Label(typeof(int));
var returnExpression = Expression.Return(returnTarget, Expression.Convert(hashVariable, typeof(int)), typeof(int));
var returnLabel = Expression.Label(returnTarget, Expression.Constant(-1));

var getHashCodeMethod = typeof(T).GetDeclaredMethod(nameof(GetHashCode));


var expressions = new List<Expression>();
foreach (var member in members)
{
var callGetHashCode = Expression.Call(member, getHashCodeMethod);
var callGetHashCode = Expression.Call(member, member.Type.GetDeclaredMethod(nameof(GetHashCode)));
var convertHashCodeToInt64 = Expression.Convert(callGetHashCode, typeof(long));
if (expressions.Count == 0)
{
Expand Down

0 comments on commit 07a6f58

Please sign in to comment.