refactor: 增加 Clone 扩展方法

This commit is contained in:
Argo Window10 2019-12-10 16:54:17 +08:00
parent 60b0f2225d
commit f383b62864
4 changed files with 52 additions and 16 deletions

View File

@ -75,32 +75,26 @@ namespace Bootstrap.Admin.Components
/// 新建按钮回调方法
/// </summary>
[Parameter]
public Func<TItem>? OnAdd { get; set; }
/// <summary>
/// 编辑按钮回调方法
/// </summary>
[Parameter]
public Action<TItem>? OnEdit { get; set; }
public Func<TItem> OnAdd { get; set; } = () => throw new InvalidOperationException($"The property {nameof(OnAdd)} can't be set to Null");
/// <summary>
/// 保存按钮回调方法
/// </summary>
[Parameter]
public Func<TItem, bool>? OnSave { get; set; }
public Func<TItem, bool> OnSave { get; set; } = item => false;
/// <summary>
/// 删除按钮回调方法
/// </summary>
[Parameter]
public Func<IEnumerable<TItem>, bool>? OnDelete { get; set; }
public Func<IEnumerable<TItem>, bool> OnDelete { get; set; } = item => false;
/// <summary>
/// 组件初始化方法
/// </summary>
protected override void OnInitialized()
{
if (string.IsNullOrEmpty(Id)) throw new InvalidOperationException($"The property {nameof(Id)} can't set to Null");
if (string.IsNullOrEmpty(Id)) throw new InvalidOperationException($"The property {nameof(Id)} can't be set to Null");
}
/// <summary>

View File

@ -57,7 +57,5 @@ namespace Bootstrap.Admin.Components
if (string.IsNullOrEmpty(Title)) Title = _defaultTitle;
if (string.IsNullOrEmpty(Text)) Text = _defaultText;
}
protected override void OnAfterRender(bool firstRender) => base.OnAfterRender(firstRender);
}
}

View File

@ -1,4 +1,5 @@
using Bootstrap.Admin.Shared;
using Bootstrap.Admin.Extensions;
using Bootstrap.Admin.Shared;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
using System;
@ -265,7 +266,7 @@ namespace Bootstrap.Admin.Components
{
if (SelectedItems.Count == 1)
{
EditModel = SelectedItems[0];
EditModel = SelectedItems[0].Clone();
EditModal?.Toggle();
}
else
@ -335,7 +336,5 @@ namespace Bootstrap.Admin.Components
}
ShowMessage("删除数据", "删除数据" + (result ? "成功" : "失败"), result ? ToastCategory.Success : ToastCategory.Error);
}
protected override void OnAfterRender(bool firstRender) => base.OnAfterRender(firstRender);
}
}

View File

@ -0,0 +1,45 @@
using System;
using System.Linq;
namespace Bootstrap.Admin.Extensions
{
/// <summary>
///
/// </summary>
public static class ObjectExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="TItem"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public static TItem Clone<TItem>(this TItem item)
{
var ret = item;
if (item != null)
{
var type = item.GetType();
if (typeof(ICloneable).IsAssignableFrom(type))
{
var clv = type.GetMethod("Clone")?.Invoke(type, null);
if (clv != null) ret = (TItem)clv;
}
if (type.IsClass)
{
ret = Activator.CreateInstance<TItem>();
var valType = ret?.GetType();
if (valType != null)
{
type.GetProperties().ToList().ForEach(p =>
{
var v = p.GetValue(item);
valType.GetProperty(p.Name)?.SetValue(ret, v);
});
}
}
}
return ret;
}
}
}