Skip to content

Commit 9e37e80

Browse files
committed
feat: 增加 IsAutoInitializeModelProperty 参数
1 parent ab8e288 commit 9e37e80

2 files changed

Lines changed: 57 additions & 15 deletions

File tree

src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,18 +294,28 @@ private async Task InternalOnAddAsync()
294294
[Parameter]
295295
public Func<TItem>? CreateItemCallback { get; set; }
296296

297+
/// <summary>
298+
/// Get or sets Whether to automatically initialize model properties default value is false.
299+
/// </summary>
300+
[Parameter]
301+
public bool IsAutoInitializeModelProperty { get; set; }
302+
297303
private TItem CreateTItem() => CreateItemCallback?.Invoke() ?? CreateInstance();
298304

305+
private readonly string ErrorMessage = $"{typeof(TItem)} create instrance failed. Please provide {nameof(CreateItemCallback)} create the {typeof(TItem)} instance. {typeof(TItem)} 自动创建实例失败,请通过 {nameof(CreateItemCallback)} 回调方法手动创建实例";
306+
299307
private TItem CreateInstance()
300308
{
309+
TItem? item;
301310
try
302311
{
303-
return ObjectExtensions.CreateInstanceWithCascade<TItem>();
312+
item = ObjectExtensions.CreateInstanceWithCascade<TItem>(IsAutoInitializeModelProperty);
304313
}
305314
catch (Exception ex)
306315
{
307-
throw new InvalidOperationException($"{typeof(TItem)} missing new() method. Please provide {nameof(CreateItemCallback)} create the {typeof(TItem)} instance. {typeof(TItem)} 未提供无参构造函数 new() 请通过 {nameof(CreateItemCallback)} 回调方法创建实例", ex);
316+
throw new InvalidOperationException(ErrorMessage, ex);
308317
}
318+
return item ?? throw new InvalidOperationException(ErrorMessage);
309319
}
310320

311321
/// <summary>

src/BootstrapBlazor/Extensions/ObjectExtensions.cs

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,37 +242,69 @@ internal static void Clone<TModel>(this TModel source, TModel item)
242242
/// Creates an instance of a type and ensures all class-type properties are initialized.
243243
/// </summary>
244244
/// <typeparam name="TItem">The type to create an instance of.</typeparam>
245+
/// <param name="isAutoInitializeModelProperty">Whether to automatically initialize model properties default value is false.</param>
245246
/// <returns>An instance of the specified type with initialized properties.</returns>
246-
public static TItem CreateInstanceWithCascade<TItem>()
247+
public static TItem? CreateInstanceWithCascade<TItem>(bool isAutoInitializeModelProperty = false)
247248
{
248-
var instance = Activator.CreateInstance<TItem>();
249-
instance!.EnsureInitialized();
249+
TItem? instance;
250+
try
251+
{
252+
instance = Activator.CreateInstance<TItem>();
253+
if (isAutoInitializeModelProperty)
254+
{
255+
instance.EnsureInitialized(isAutoInitializeModelProperty);
256+
}
257+
}
258+
catch
259+
{
260+
throw;
261+
}
262+
return instance;
263+
}
264+
265+
private static object? CreateInstance(Type type, bool isAutoInitializeModelProperty = false)
266+
{
267+
object? instance;
268+
try
269+
{
270+
instance = Activator.CreateInstance(type);
271+
if (isAutoInitializeModelProperty)
272+
{
273+
instance.EnsureInitialized();
274+
}
275+
}
276+
catch
277+
{
278+
throw;
279+
}
250280
return instance;
251281
}
252282

253283
/// <summary>
254284
/// Ensures that all class-type properties of the instance are initialized.
255285
/// </summary>
286+
/// <param name="isAutoInitializeModelProperty">Whether to automatically initialize model properties default value is false.</param>
256287
/// <param name="instance">The instance to initialize properties for.</param>
257-
private static void EnsureInitialized(this object instance)
288+
private static void EnsureInitialized(this object? instance, bool isAutoInitializeModelProperty = false)
258289
{
290+
if (instance is null)
291+
{
292+
return;
293+
}
294+
259295
// Reflection performance needs to be optimized here
260296
foreach (var propertyInfo in instance.GetType().GetProperties().Where(p => p.PropertyType.IsClass && p.PropertyType != typeof(string)))
261297
{
262298
var type = propertyInfo.PropertyType;
263299
var value = propertyInfo.GetValue(instance, null);
264300
if (value is null)
265301
{
266-
var pv = CreateInstance(type);
267-
propertyInfo.SetValue(instance, pv);
302+
var pv = CreateInstance(type, isAutoInitializeModelProperty);
303+
if (pv is not null)
304+
{
305+
propertyInfo.SetValue(instance, pv);
306+
}
268307
}
269308
}
270309
}
271-
272-
private static object? CreateInstance(Type type)
273-
{
274-
var instance = Activator.CreateInstance(type);
275-
instance!.EnsureInitialized();
276-
return instance;
277-
}
278310
}

0 commit comments

Comments
 (0)