Browse Source

Added HasExtraPropertiesExtensions

pull/827/head
Halil ibrahim Kalkan 7 years ago
parent
commit
7dcc7d256f
  1. 43
      framework/src/Volo.Abp.Data/Volo/Abp/Data/HasExtraPropertiesExtensions.cs
  2. 18
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ExtraProperties_Tests.cs

43
framework/src/Volo.Abp.Data/Volo/Abp/Data/HasExtraPropertiesExtensions.cs

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp.Reflection;
namespace Volo.Abp.Data
{
public static class HasExtraPropertiesExtensions
{
public static bool HasProperty(this IHasExtraProperties source, string name)
{
return source.ExtraProperties.ContainsKey(name);
}
public static object GetProperty(this IHasExtraProperties source, string name)
{
return source.ExtraProperties?.GetOrDefault(name);
}
public static TProperty GetProperty<TProperty>(this IHasExtraProperties source, string name)
{
var value = source.GetProperty(name);
if (value == default)
{
return default;
}
if (TypeHelper.IsPrimitiveExtended(typeof(TProperty), includeEnums: true))
{
return (TProperty)Convert.ChangeType(value, typeof(TProperty), CultureInfo.InvariantCulture);
}
throw new AbpException("GetProperty<TProperty> does not support non-primitive types. Use non-generic GetProperty method and handle type casting manually.");
}
public static TSource SetProperty<TSource>(this TSource source, string name, object value)
where TSource : IHasExtraProperties
{
source.ExtraProperties[name] = value;
return source;
}
}
}

18
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/ExtraProperties_Tests.cs

@ -1,6 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Xunit;
@ -21,20 +21,20 @@ namespace Volo.Abp.TestApp.Testing
public async Task Should_Get_An_Extra_Property()
{
var london = await CityRepository.FindByNameAsync("London");
london.ExtraProperties.ContainsKey("Population").ShouldBeTrue();
london.ExtraProperties["Population"].To<int>().ShouldBe(10_470_000);
london.HasProperty("Population").ShouldBeTrue();
london.GetProperty<int>("Population").ShouldBe(10_470_000);
}
[Fact]
public async Task Should_Add_An_Extra_Property()
{
var london = await CityRepository.FindByNameAsync("London");
london.ExtraProperties["AreaAsKm"] = 1572;
london.SetProperty("AreaAsKm", 1572);
await CityRepository.UpdateAsync(london);
var london2 = await CityRepository.FindByNameAsync("London");
london2.ExtraProperties.ContainsKey("AreaAsKm").ShouldBeTrue();
london2.ExtraProperties["AreaAsKm"].To<int>().ShouldBe(1572);
london2.HasProperty("AreaAsKm").ShouldBeTrue();
london2.GetProperty<int>("AreaAsKm").ShouldBe(1572);
}
[Fact]
@ -46,8 +46,8 @@ namespace Volo.Abp.TestApp.Testing
await CityRepository.UpdateAsync(london);
var london2 = await CityRepository.FindByNameAsync("London");
london2.ExtraProperties.ContainsKey("Population").ShouldBeTrue();
london2.ExtraProperties["Population"].To<int>().ShouldBe(11_000_042);
london2.HasProperty("Population").ShouldBeTrue();
london2.GetProperty<int>("Population").ShouldBe(11_000_042);
}
}
}

Loading…
Cancel
Save