ExtensibleObject should set default values for defined properties.

pull/3831/head
Halil İbrahim Kalkan 6 years ago
parent 8a318cb1ea
commit 55016d8446

@ -131,5 +131,20 @@ namespace Volo.Abp.Reflection
type == typeof(TimeSpan) ||
type == typeof(Guid);
}
public static object GetDefaultValue<T>()
{
return GetDefaultValue(typeof(T));
}
public static object GetDefaultValue(Type type)
{
if (type.IsValueType)
{
return Activator.CreateInstance(type);
}
return null;
}
}
}

@ -25,8 +25,9 @@ namespace Volo.Abp.Domain.Entities
protected AggregateRoot()
{
ExtraProperties = new Dictionary<string, object>();
ConcurrencyStamp = Guid.NewGuid().ToString("N");
ExtraProperties = new Dictionary<string, object>();
this.SetDefaultsForExtraProperties();
}
protected virtual void AddLocalEvent(object eventData)
@ -85,15 +86,17 @@ namespace Volo.Abp.Domain.Entities
protected AggregateRoot()
{
ExtraProperties = new Dictionary<string, object>();
ConcurrencyStamp = Guid.NewGuid().ToString("N");
ExtraProperties = new Dictionary<string, object>();
this.SetDefaultsForExtraProperties();
}
protected AggregateRoot(TKey id)
: base(id)
{
ExtraProperties = new Dictionary<string, object>();
ConcurrencyStamp = Guid.NewGuid().ToString("N");
ExtraProperties = new Dictionary<string, object>();
this.SetDefaultsForExtraProperties();
}
protected virtual void AddLocalEvent(object eventData)

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Volo.Abp.ObjectExtending;
using Volo.Abp.Reflection;
namespace Volo.Abp.Data
@ -49,5 +50,34 @@ namespace Volo.Abp.Data
source.ExtraProperties.Remove(name);
return source;
}
public static TSource SetDefaultsForExtraProperties<TSource>(this TSource source, Type objectType = null)
where TSource : IHasExtraProperties
{
var properties = ObjectExtensionManager.Instance
.GetProperties(objectType ?? typeof(TSource));
foreach (var property in properties)
{
if (source.HasProperty(property.Name))
{
continue;
}
source.ExtraProperties[property.Name] = TypeHelper.GetDefaultValue(property.Type);
}
return source;
}
public static void SetDefaultsForExtraProperties(object source, Type objectType)
{
if (!(source is IHasExtraProperties))
{
throw new ArgumentException($"Given {nameof(source)} object does not implement the {nameof(IHasExtraProperties)} interface!", nameof(source));
}
((IHasExtraProperties) source).SetDefaultsForExtraProperties(objectType);
}
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Volo.Abp.Data;
using Volo.Abp.DynamicProxy;
namespace Volo.Abp.ObjectExtending
{
@ -11,8 +12,19 @@ namespace Volo.Abp.ObjectExtending
public Dictionary<string, object> ExtraProperties { get; protected set; }
public ExtensibleObject()
: this(true)
{
}
public ExtensibleObject(bool setDefaultsForExtraProperties)
{
ExtraProperties = new Dictionary<string, object>();
if (setDefaultsForExtraProperties)
{
this.SetDefaultsForExtraProperties(ProxyHelper.UnProxy(this).GetType());
}
}
public virtual IEnumerable<ValidationResult> Validate(ValidationContext validationContext)

@ -0,0 +1,37 @@
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending.TestObjects;
using Xunit;
namespace Volo.Abp.ObjectExtending
{
public class ExtensibleObject_Tests : AbpObjectExtendingTestBase
{
[Fact]
public void Should_Set_Default_Values_For_Defined_Properties_On_Create()
{
var person = new ExtensibleTestPerson();
person.HasProperty("Name").ShouldBeTrue();
person.HasProperty("Age").ShouldBeTrue();
person.HasProperty("NoPairCheck").ShouldBeTrue();
person.HasProperty("CityName").ShouldBeTrue();
person.GetProperty<string>("Name").ShouldBeNull();
person.GetProperty<int>("Age").ShouldBe(0);
person.GetProperty<string>("NoPairCheck").ShouldBeNull();
person.GetProperty<string>("CityName").ShouldBeNull();
}
[Fact]
public void Should_Not_Set_Default_Values_For_Defined_Properties_If_Requested()
{
var person = new ExtensibleTestPerson(false);
person.HasProperty("Name").ShouldBeFalse();
person.HasProperty("Age").ShouldBeFalse();
person.HasProperty("NoPairCheck").ShouldBeFalse();
person.HasProperty("CityName").ShouldBeFalse();
}
}
}

@ -2,6 +2,15 @@
{
public class ExtensibleTestPerson : ExtensibleObject
{
public ExtensibleTestPerson()
{
}
public ExtensibleTestPerson(bool setDefaultsForExtraProperties)
: base(setDefaultsForExtraProperties)
{
}
}
}

Loading…
Cancel
Save