!2497 feat(#I4WV52): add GetKeyValue method on Utility extension class

* refactor: 合并单元测试
* test: 增加单元测试
* feat: 增加缓存方法
* feat: 增加 Lambda 扩展方法获得 Key 标签属性值
This commit is contained in:
Argo 2022-03-08 07:44:11 +00:00
parent 75838fdc98
commit 60d625dab6
4 changed files with 62 additions and 0 deletions

View File

@ -3,6 +3,7 @@
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components;
using System.ComponentModel.DataAnnotations;
using System.Linq.Expressions;
using System.Reflection;
@ -600,4 +601,24 @@ public static class LambdaExtensions
return false;
}
#endregion
/// <summary>
/// 获得 指定模型标记 <see cref="KeyAttribute"/> 的属性值
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <returns></returns>
public static Expression<Func<TModel, TValue>> GetKeyValue<TModel, TValue>(TModel model)
{
var type = model is not null ? model.GetType() : typeof(TModel);
Expression<Func<TModel, TValue>> ret = _ => default!;
var property = type.GetRuntimeProperties().FirstOrDefault(p => p.IsDefined(typeof(KeyAttribute)));
if (property != null)
{
var param = Expression.Parameter(typeof(TModel));
var body = Expression.Property(Expression.Convert(param, type), property);
ret = Expression.Lambda<Func<TModel, TValue>>(Expression.Convert(body, typeof(TValue)), param);
}
return ret;
}
}

View File

@ -301,6 +301,25 @@ internal class CacheManager : ICacheManager
});
invoker(model, value);
}
/// <summary>
/// 获得 指定模型标记 <see cref="KeyAttribute"/> 的属性值
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public static TValue GetKeyValue<TModel, TValue>(TModel model)
{
var type = model is object o ? o.GetType() : typeof(TModel);
var cacheKey = ($"Lambda-GetKeyValue-{type.FullName}", typeof(TModel));
var invoker = Instance.GetOrCreate(cacheKey, entry =>
{
entry.SetDynamicAssemblyPolicy(type);
return LambdaExtensions.GetKeyValue<TModel, TValue>(model).Compile();
});
return invoker(model);
}
#endregion
#region Lambda Sort

View File

@ -4,6 +4,7 @@
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq.Expressions;
using System.Reflection;
@ -31,6 +32,15 @@ public static class Utility
/// <returns></returns>
public static string GetDisplayName(Type modelType, string fieldName) => CacheManager.GetDisplayName(modelType, fieldName);
/// <summary>
/// 获得 指定模型标记 <see cref="KeyAttribute"/> 的属性值
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TValue"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public static TValue GetKeyValue<TModel, TValue>(TModel model) => CacheManager.GetKeyValue<TModel, TValue>(model);
/// <summary>
///
/// </summary>

View File

@ -18,6 +18,18 @@ public class UtilityTest : BootstrapBlazorTestBase
};
}
[Fact]
public void GetKeyValue_Ok()
{
var foo = new Foo() { Id = 1 };
var v = Utility.GetKeyValue<Foo, int>(foo);
Assert.Equal(1, v);
object foo1 = new Foo() { Id = 2 };
v = Utility.GetKeyValue<object, int>(foo1);
Assert.Equal(2, v);
}
[Fact]
public void GetPropertyValue_Ok()
{