Browse Source

Object Extensions supports Enum property

pull/15332/head
liangshiwei 4 years ago
parent
commit
aa297abf20
  1. 7
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs
  2. 2
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs
  3. 3
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/ExtensibleObject_Tests.cs
  4. 8
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions_Tests.cs
  5. 6
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPerson.cs

7
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/Data/HasExtraPropertiesExtensions.cs

@ -45,6 +45,11 @@ public static class HasExtraPropertiesExtensions
return (TProperty)TypeDescriptor.GetConverter(conversionType).ConvertFromInvariantString(value.ToString());
}
if (conversionType.IsEnum)
{
return (TProperty)value;
}
return (TProperty)Convert.ChangeType(value, conversionType, CultureInfo.InvariantCulture);
}
@ -129,7 +134,7 @@ public static class HasExtraPropertiesExtensions
{
Check.NotNull(source, nameof(source));
Check.NotNull(other, nameof(other));
return source.ExtraProperties.HasSameItems(other.ExtraProperties);
}
}

2
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/AbpObjectExtendingTestModule.cs

@ -21,9 +21,11 @@ public class AbpObjectExtendingTestModule : AbpModule
.AddOrUpdateProperty<ExtensibleTestPerson, int>("Age")
.AddOrUpdateProperty<ExtensibleTestPerson, string>("NoPairCheck", options => options.CheckPairDefinitionOnMapping = false)
.AddOrUpdateProperty<ExtensibleTestPerson, string>("CityName")
.AddOrUpdateProperty<ExtensibleTestPerson, ExtensibleTestEnumProperty>("EnumProperty")
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>("Name")
.AddOrUpdateProperty<ExtensibleTestPersonDto, int>("ChildCount")
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>("CityName")
.AddOrUpdateProperty<ExtensibleTestPersonDto, ExtensibleTestEnumProperty>("EnumProperty")
.AddOrUpdateProperty<ExtensibleTestPersonWithRegularPropertiesDto, string>("Name")
.AddOrUpdateProperty<ExtensibleTestPersonWithRegularPropertiesDto, int>("Age");
});

3
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/ExtensibleObject_Tests.cs

@ -16,11 +16,13 @@ public class ExtensibleObject_Tests : AbpObjectExtendingTestBase
person.HasProperty("Age").ShouldBeTrue();
person.HasProperty("NoPairCheck").ShouldBeTrue();
person.HasProperty("CityName").ShouldBeTrue();
person.HasProperty("EnumProperty").ShouldBeTrue();
person.GetProperty<string>("Name").ShouldBeNull();
person.GetProperty<int>("Age").ShouldBe(0);
person.GetProperty<string>("NoPairCheck").ShouldBeNull();
person.GetProperty<string>("CityName").ShouldBeNull();
person.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(default);
}
[Fact]
@ -32,5 +34,6 @@ public class ExtensibleObject_Tests : AbpObjectExtendingTestBase
person.HasProperty("Age").ShouldBeFalse();
person.HasProperty("NoPairCheck").ShouldBeFalse();
person.HasProperty("CityName").ShouldBeFalse();
person.HasProperty("EnumProperty").ShouldBeFalse();
}
}

8
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions_Tests.cs

@ -18,7 +18,8 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
.SetProperty("ChildCount", 2)
.SetProperty("Sex", "male")
.SetProperty("NoPairCheck", "test-value")
.SetProperty("CityName", "Adana");
.SetProperty("CityName", "Adana")
.SetProperty("EnumProperty", (int)ExtensibleTestEnumProperty.Value1);
_personDto = new ExtensibleTestPersonDto()
.SetProperty("ExistingDtoProperty", "existing-value");
@ -31,6 +32,7 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_personDto.GetProperty<string>("Name").ShouldBe("John"); //Defined in both classes
_personDto.GetProperty<string>("CityName").ShouldBe("Adana"); //Defined in both classes
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1); //Defined in both classes
_personDto.GetProperty<string>("NoPairCheck").ShouldBe("test-value"); //CheckPairDefinitionOnMapping = false
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
_personDto.HasProperty("Age").ShouldBeFalse(); //Not defined on the destination
@ -44,6 +46,7 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_person.MapExtraPropertiesTo(_personDto, ignoredProperties: new[] { "CityName" });
_personDto.GetProperty<string>("Name").ShouldBe("John"); //Defined in both classes
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1); //Defined in both classes
_personDto.GetProperty<string>("NoPairCheck").ShouldBe("test-value"); //CheckPairDefinitionOnMapping = false
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
_personDto.GetProperty<string>("CityName").ShouldBeNull(); //Ignored, but was set to the default in the constructor
@ -59,6 +62,7 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_personDto.GetProperty<string>("Name").ShouldBe("John"); //Defined in both classes
_personDto.GetProperty<string>("CityName").ShouldBe("Adana"); //Defined in both classes
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1); //Defined in both classes
_personDto.GetProperty<int>("Age").ShouldBe(42); //Defined in source
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
_personDto.GetProperty<int>("ChildCount").ShouldBe(0); //Not defined in the source, but was set to the default in the constructor
@ -72,6 +76,7 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_personDto.GetProperty<string>("Name").ShouldBe("John"); //Defined in both classes
_personDto.GetProperty<string>("CityName").ShouldBe("Adana"); //Defined in both classes
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1); //Defined in both classes
_personDto.GetProperty<int>("ChildCount").ShouldBe(2); //Defined in destination
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
_personDto.HasProperty("Age").ShouldBeFalse(); //Not defined in destination
@ -88,6 +93,7 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_personDto.GetProperty<int>("Age").ShouldBe(42);
_personDto.GetProperty<int>("ChildCount").ShouldBe(2);
_personDto.GetProperty<string>("Sex").ShouldBe("male");
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1);
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
}
}

6
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/TestObjects/ExtensibleTestPerson.cs

@ -13,3 +13,9 @@ public class ExtensibleTestPerson : ExtensibleObject
}
}
public enum ExtensibleTestEnumProperty
{
Value1,
Value2
}

Loading…
Cancel
Save