ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

.NET 文件系统(四)-- BaseService实现增加与修改方法

2022-09-11 21:02:54  阅读:168  来源: 互联网

标签:return repository BaseService -- FileDownLoadSystem System using NET public


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;
		}
	}
}
增加扩展类的意义是为了让字典类转成IEnumerable,List集合,和实体类,为了方便后续实现

改造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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有