using System; using System.Linq; using Microsoft.AspNetCore.Mvc.ModelBinding; using Hncore.Infrastructure.Data; namespace Hncore.Infrastructure.Common { public class CheckHelper { public static void NotNull(object obj, string message = "") { if (ReferenceEquals(obj, null)) { if (string.IsNullOrEmpty(message)) { message = nameof(obj) + "空引用"; } throw new BusinessException(message); } } public static void NotEmpty(string obj, string message = "") { NotNull(obj,message); if (obj.Trim() == "") { if (string.IsNullOrEmpty(message)) { message = nameof(obj) + "值不能为空"; } throw new BusinessException(message); } } } public static class Ext { public static void Check(this ModelStateDictionary dic) { if (!dic.IsValid) { var errs = dic.Values.SelectMany(x => x.Errors); string msg = ""; errs.Select(t => t.Exception.Message).ToList().ForEach((s => { msg += s + "\r\n"; })); throw new Exception(msg); } } } }