Skip to content

Commit 387f70c

Browse files
committed
feat: 增加 SupportComplexProperty 参数
1 parent 88dfa18 commit 387f70c

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -513,56 +513,53 @@ public static bool TryGetProperty(Type modelType, string fieldName, [NotNullWhen
513513
return propertyInfo != null;
514514
}
515515

516-
public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fieldName)
516+
public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fieldName, bool supportComplexProperty) => (model is IDynamicColumnsObject d)
517+
? (TResult)d.GetValue(fieldName)!
518+
: GetValue<TModel, TResult>(model, fieldName, supportComplexProperty);
519+
520+
private static TResult GetValue<TModel, TResult>(TModel model, string fieldName, bool supportComplexProperty)
517521
{
518522
if (model == null)
519523
{
520524
throw new ArgumentNullException(nameof(model));
521525
}
522526

523-
return (model is IDynamicColumnsObject d)
524-
? (TResult)d.GetValue(fieldName)!
525-
: GetValue();
526-
527-
TResult GetValue()
527+
var type = model.GetType();
528+
var cacheKey = $"{CacheKeyPrefix}-Lambda-Get-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TResult)}-{supportComplexProperty}";
529+
var invoker = Instance.GetOrCreate(cacheKey, entry =>
528530
{
529-
var type = model.GetType();
530-
var cacheKey = $"{CacheKeyPrefix}-Lambda-Get-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TResult)}";
531-
var invoker = Instance.GetOrCreate(cacheKey, entry =>
531+
if (type.Assembly.IsDynamic)
532532
{
533-
if (type.Assembly.IsDynamic)
534-
{
535-
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
536-
}
533+
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
534+
}
537535

538-
return LambdaExtensions.GetPropertyValueLambda<TModel, TResult>(model, fieldName).Compile();
539-
});
540-
return invoker(model);
541-
}
536+
return LambdaExtensions.GetPropertyValueLambda<TModel, TResult>(model, fieldName, supportComplexProperty).Compile();
537+
});
538+
return invoker(model);
542539
}
543540

544-
public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldName, TValue value)
541+
public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldName, TValue value, bool supportComplexProperty)
545542
{
546-
if (model == null)
547-
{
548-
throw new ArgumentNullException(nameof(model));
549-
}
550-
551543
if (model is IDynamicColumnsObject d)
552544
{
553545
d.SetValue(fieldName, value);
554546
}
555547
else
556548
{
549+
if (model == null)
550+
{
551+
throw new ArgumentNullException(nameof(model));
552+
}
553+
557554
var type = model.GetType();
558-
var cacheKey = $"{CacheKeyPrefix}-Lambda-Set-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TValue)}";
555+
var cacheKey = $"{CacheKeyPrefix}-Lambda-Set-{type.GetUniqueTypeName()}-{typeof(TModel)}-{fieldName}-{typeof(TValue)}-{supportComplexProperty}";
559556
var invoker = Instance.GetOrCreate(cacheKey, entry =>
560557
{
561558
if (type.Assembly.IsDynamic)
562559
{
563560
entry.SetAbsoluteExpiration(TimeSpan.FromSeconds(10));
564561
}
565-
return LambdaExtensions.SetPropertyValueLambda<TModel, TValue>(model, fieldName).Compile();
562+
return LambdaExtensions.SetPropertyValueLambda<TModel, TValue>(model, fieldName, supportComplexProperty).Compile();
566563
});
567564
invoker(model, value);
568565
}

src/BootstrapBlazor/Utils/Utility.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,18 @@ public static class Utility
108108
/// <typeparam name="TResult"></typeparam>
109109
/// <param name="model"></param>
110110
/// <param name="fieldName"></param>
111+
/// <param name="supportComplexProperty"></param>
111112
/// <returns></returns>
112-
public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fieldName) => CacheManager.GetPropertyValue<TModel, TResult>(model, fieldName);
113+
public static TResult GetPropertyValue<TModel, TResult>(TModel model, string fieldName, bool supportComplexProperty = true) => CacheManager.GetPropertyValue<TModel, TResult>(model, fieldName, supportComplexProperty);
113114

114115
/// <summary>
115116
/// 获取 指定对象的属性值
116117
/// </summary>
117118
/// <param name="model"></param>
118119
/// <param name="fieldName"></param>
120+
/// <param name="supportComplexProperty"></param>
119121
/// <returns></returns>
120-
public static object? GetPropertyValue(object model, string fieldName)
122+
public static object? GetPropertyValue(object model, string fieldName, bool supportComplexProperty = true)
121123
{
122124
return model.GetType().Assembly.IsDynamic ? ReflectionInvoke() : LambdaInvoke();
123125

@@ -132,7 +134,7 @@ public static class Utility
132134
return ret;
133135
}
134136

135-
object? LambdaInvoke() => GetPropertyValue<object, object?>(model, fieldName);
137+
object? LambdaInvoke() => GetPropertyValue<object, object?>(model, fieldName, supportComplexProperty);
136138
}
137139

138140
/// <summary>
@@ -143,8 +145,9 @@ public static class Utility
143145
/// <param name="model"></param>
144146
/// <param name="fieldName"></param>
145147
/// <param name="value"></param>
148+
/// <param name="supportComplexProperty"></param>
146149
/// <returns></returns>
147-
public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldName, TValue value) => CacheManager.SetPropertyValue(model, fieldName, value);
150+
public static void SetPropertyValue<TModel, TValue>(TModel model, string fieldName, TValue value, bool supportComplexProperty = true) => CacheManager.SetPropertyValue(model, fieldName, value, supportComplexProperty);
148151

149152
/// <summary>
150153
/// 获得 排序方法
@@ -866,7 +869,7 @@ static Expression<Func<ComponentBase, object, string, object>> CreateLambda(Type
866869
/// <param name="model"></param>
867870
/// <param name="fieldName"></param>
868871
/// <returns></returns>
869-
public static EventCallback<TType> CreateCallback<TType>(ComponentBase component, object model, string fieldName) => EventCallback.Factory.Create<TType>(component, t => CacheManager.SetPropertyValue(model, fieldName, t));
872+
public static EventCallback<TType> CreateCallback<TType>(ComponentBase component, object model, string fieldName) => EventCallback.Factory.Create<TType>(component, t => CacheManager.SetPropertyValue(model, fieldName, t, true));
870873

871874
/// <summary>
872875
/// 获得指定泛型的 IEditorItem 集合

0 commit comments

Comments
 (0)