두 개의 유사한 물체를 비교하는 가장 좋은 방법은 무엇입니까?
주어진 FlintlockDTO
및 Flintlock
:
public class FlintlockDTO
{
public string GName { get; set; }
public string SharedPropertyName { get; set; }
...
}
과
public class Flintlock
{
public Flintlock(FlintlockDTO inflator)
{
this.GoodName = inflator.GName;
this.SharedPropertyName = inflator.SharedPropertyName;
...
}
public string GoodName { get; private set; }
public string SharedPropertyName { get; private set; }
...
}
두 클래스 모두 N 개의 속성 (예 :)을 공유 SharedPropertyName
하지만 동일하지만 이름이 다른 M 속성 (예 : GoodName
\ GName
.)에서는 다릅니다 .
fluentassert 와 같은 도구 는 속성 이름이 일치하면 거의 작동합니다.
flintlockDto.ShouldBeEquivalentTo(flintlock);
fluentassert 또는 다른 도구에서 깔끔하게 수행 할 수있는 방법이 있습니까?
이상적으로는
flintlockDto.IsTheSameAs(flintlock).WhenMapping("GName","GoodName");
StriplingWarrior가 언급 한 Likeness 에 대해 더 자세히 설명하기로 결정했습니다 . 너겟 패키지 로 사용할 수 있습니다 .
다음은 그 예입니다.
using NUnit.Framework;
using Ploeh.SemanticComparison;
using Ploeh.SemanticComparison.Fluent;
namespace Tests
{
[TestFixture]
class Tests2
{
[Test]
public void ObjectsShuldEqual()
{
var flintlockDto = new FlintlockDTO()
{
GName = "name",
AdditionalProperty = "whatever",
SharedPropertyName = "prop name"
};
var flintlock = new Flintlock(flintlockDto);
Likeness<Flintlock, FlintlockDTO> flintFlockDtoLikeness = flintlock
.AsSource().OfLikeness<FlintlockDTO>()
.With(dto => dto.GName).EqualsWhen((flintlock1, dto) => flintlock1.GoodName == dto.GName) // you can write an extension method to encapsulate it
.Without(dto => dto.AdditionalProperty);
// assert
flintFlockDtoLikeness.ShouldEqual(flintlockDto);
}
}
public class FlintlockDTO
{
public string GName { get; set; }
public string SharedPropertyName { get; set; }
public string AdditionalProperty { get; set; }
}
public class Flintlock
{
public Flintlock(FlintlockDTO inflator)
{
this.GoodName = inflator.GName;
this.SharedPropertyName = inflator.SharedPropertyName;
}
public string GoodName { get; private set; }
public string SharedPropertyName { get; private set; }
}
}
보시다시피 :
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다