.NET 文件系统(四)-- BaseService实现增加与修改方法
作者:互联网
FileDownLoadSystem.Core
1.BaseService增加 AddEntity 方法与Add方法
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FileDownLoadSystem.Core.Enums;
using FileDownLoadSystem.Core.Utility;
using FileDownLoadSystem.Entity;
using Microsoft.EntityFrameworkCore;
namespace FileDownLoadSystem.Core.BaseProvider
{
public class BaseService<TModel, TRepository>
where TModel : BaseModel
where TRepository : IRepository<TModel>
{
protected readonly TRepository _repository;
private WebResponseContent _responseContent;
public BaseService(TRepository repository)
{
this._repository = repository;
_responseContent = new(true);
}
public TModel FindFirst(Expression<Func<TModel, bool>> predicate,
Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null)
{
return _repository.FindFirst(predicate, orderBy);
}
public virtual WebResponseContent AddEntity(TModel entity)
{
return Add<TModel>(entity, null);
}
public WebResponseContent Add<TDetail>(TModel entity, List<TDetail> list = null)
where TDetail : class
{
_repository.Insert(entity);
// 保存明细
if (list != null && list.Count > 0)
{
list.ForEach(item =>
{
//开启对当前对象的跟踪
_repository.DbContext.Entry<TDetail>(item).State = EntityState.Added;
});
_repository.DbContext.SaveChanges();
}
_responseContent.OK(ResponseType.SaveSuccess);
return _responseContent;
}
}
}
FileDownLoadSystem.Entity
新建一个文件夹Core
添加一个类SaveModel
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace FileDownLoadSystem.Entity.Core
{
public class SaveModel
{
public Dictionary<string, object> MainData { get; set; }
public List<Dictionary<string, object>> DetailData { get; set; }
public List<object> DelKeys { get; set; }
public object Extra { get; set; }
}
}
FileDownLoadSystem.Core
新增扩展类
在Extensions文件夹下新建一个扩展类:ObjectExtensions
ObjectExtensions
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.Threading.Tasks;
namespace FileDownLoadSystem.Core.Extensions
{
public static class ObjectExtensions
{
/// <summary>
/// 字典集合转换为list集合
/// </summary>
/// <param name="dicList"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static List<T> DicToList<T>(this List<Dictionary<string, object>> dicList)
{
return dicList.DicToEnumerable<T>().ToList();
}
public static T DicToEntity<T>(this Dictionary<string, object> dicList)
{
return new List<Dictionary<string, object>>() { dicList }.DicToList<T>()[0];
}
public static IEnumerable<T> DicToEnumerable<T>(this List<Dictionary<string, object>> dicList)
{
foreach (var dic in dicList)
{
T obj = Activator.CreateInstance<T>();
PropertyInfo[] properties = obj.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in properties)
{
if (prop != null)
{
if (!dic.TryGetValue(prop.Name, out object value))
{
continue;
}
else
{
prop.SetValue(obj, value.ChangeType(prop.PropertyType));
}
}
}
}
return default;
}
public static object ChangeType(this object convertibleValue, Type type)
{
if (null == convertibleValue)
{
return null;
}
try
{
//判断类型是否是Guid类型
if (type == typeof(Guid) || type == typeof(Guid?))
{
string value = convertibleValue.ToString();
if (value == "")
{
return null;
}
else
{
return Guid.Parse(value);
}
}
//判断当前类型不是泛型类型
if (!type.IsGenericType)
{
return Convert.ChangeType(convertibleValue, type);
}
//判断当前类型是bool类型或者是可空的bool类型
if (type.ToString() == "System.Nullable`1[FileSystemAclExtensions.Boolean]"
|| type.ToString() == "System.Boolean")
{
if (convertibleValue.ToString() == "0")
{
return false;
}
return true;
}
//判断当前是否是泛型的基础类型
Type genericTypeDefinition = type.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
{
//GetUnderlyingType:返回可以为null类型的基础类型参数
return Convert.ChangeType(convertibleValue, Nullable.GetUnderlyingType(type));
}
}
catch (System.Exception ex)
{
throw;
}
return null;
}
}
}
改造BaseService
UpdateEntity主要为了前端随便传字典类型,后端可以根据字段进行匹配,这样前端就不用写多个对象了,增加了前端的灵活性
点击查看代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using FileDownLoadSystem.Core.Enums;
using FileDownLoadSystem.Core.Extensions;
using FileDownLoadSystem.Core.Utility;
using FileDownLoadSystem.Entity;
using FileDownLoadSystem.Entity.Core;
using Microsoft.EntityFrameworkCore;
namespace FileDownLoadSystem.Core.BaseProvider
{
public class BaseService<TModel, TRepository>
where TModel : BaseModel
where TRepository : IRepository<TModel>
{
protected readonly TRepository _repository;
private WebResponseContent _responseContent;
public BaseService(TRepository repository)
{
this._repository = repository;
_responseContent = new(true);
}
public TModel FindFirst(Expression<Func<TModel, bool>> predicate,
Expression<Func<TModel, Dictionary<object, QueryOrderBy>>> orderBy = null)
{
return _repository.FindFirst(predicate, orderBy);
}
public virtual WebResponseContent AddEntity(TModel entity)
{
return Add<TModel>(entity, null);
}
public WebResponseContent Add<TDetail>(TModel entity, List<TDetail> list = null)
where TDetail : class
{
_repository.Insert(entity);
// 保存明细
if (list != null && list.Count > 0)
{
list.ForEach(item =>
{
//开启对当前对象的跟踪
_repository.DbContext.Entry<TDetail>(item).State = EntityState.Added;
});
_repository.DbContext.SaveChanges();
}
_responseContent.OK(ResponseType.SaveSuccess);
return _responseContent;
}
public WebResponseContent UpdateEntity<TDetail>(SaveModel saveModel)
where TDetail : BaseModel
{
var datailList = saveModel.DetailData.DicToList<TDetail>();
return default;
}
}
}
标签:return,repository,BaseService,--,FileDownLoadSystem,System,using,NET,public 来源: https://www.cnblogs.com/rookiewang/p/16663737.html