namespace NetAdmin.Domain;
///
/// 数据基类
///
public abstract record DataAbstraction
{
///
/// 如果数据校验失败,抛出异常
///
/// NetAdminValidateException
public void ThrowIfInvalid()
{
var validationResult = this.TryValidate();
if (!validationResult.IsValid) {
throw new NetAdminValidateException(validationResult.ValidationResults.ToDictionary( //
x => x.MemberNames.First() //
, x => new[] { x.ErrorMessage }));
}
}
///
public override string ToString()
{
return this.ToJson();
}
///
/// 截断所有字符串属性 以符合[MaxLength(x)]特性
///
public void TruncateStrings()
{
foreach (var property in GetType()
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(x => x.PropertyType == typeof(string))) {
var maxLen = property.GetCustomAttribute(true)?.Length;
if (maxLen is null or 0) {
continue;
}
var value = property.GetValue(this);
if (value is not string s || s.Length < maxLen) {
continue;
}
s = s.Sub(0, maxLen.Value);
property.SetValue(this, s);
}
}
}