/// <summary> /// 初始化一个<see cref="FunctionHandlerBase{TFunction, TFunctionHandler}"/>类型的新实例 /// </summary> protected FunctionHandlerBase(IServiceProvider applicationServiceProvider) { _scope = applicationServiceProvider.CreateScope(); ScopedServiceProvider = _scope.ServiceProvider; AllAssemblyFinder = ScopedServiceProvider.GetService <IAllAssemblyFinder>(); _logger = ScopedServiceProvider.GetService <ILogger <TFunctionHandler> >(); }
/// <summary> /// 将从程序集获取的功能信息同步到数据库中 /// </summary> /// <param name="functions">程序集获取的功能信息</param> protected virtual void SyncToDatabase(TFunction[] functions) { Check.NotNull(functions, nameof(functions)); if (functions.Length == 0) { return; } IRepository <TFunction, Guid> repository = ScopedServiceProvider.GetService <IRepository <TFunction, Guid> >(); if (repository == null) { throw new OsharpException("IRepository<,>的服务未找到,请初始化 EntityFrameworkCoreModule 模块"); } TFunction[] dbItems = repository.TrackQuery().ToArray(); //删除的功能 TFunction[] removeItems = dbItems.Except(functions, EqualityHelper <TFunction> .CreateComparer(m => m.Area + m.Controller + m.Action)).ToArray(); int removeCount = removeItems.Length; //todo:由于外键关联不能物理删除的实体,需要实现逻辑删除 repository.Delete(removeItems); //新增的功能 TFunction[] addItems = functions.Except(dbItems, EqualityHelper <TFunction> .CreateComparer(m => m.Area + m.Controller + m.Action)).ToArray(); int addCount = addItems.Length; repository.Insert(addItems); //更新的功能信息 int updateCount = 0; foreach (TFunction item in dbItems.Except(removeItems)) { bool isUpdate = false; TFunction function = functions.Single(m => string.Equals(m.Area, item.Area, StringComparison.OrdinalIgnoreCase) && string.Equals(m.Controller, item.Controller, StringComparison.OrdinalIgnoreCase) && string.Equals(m.Action, item.Action, StringComparison.OrdinalIgnoreCase)); if (function == null) { continue; } if (!string.Equals(item.Name, function.Name, StringComparison.OrdinalIgnoreCase)) { item.Name = function.Name; isUpdate = true; } if (item.IsAjax != function.IsAjax) { item.IsAjax = function.IsAjax; isUpdate = true; } if (!item.IsAccessTypeChanged && item.AccessType != function.AccessType) { item.AccessType = function.AccessType; isUpdate = true; } if (isUpdate) { repository.Update(item); updateCount++; } } repository.UnitOfWork.Commit(); if (removeCount + addCount + updateCount > 0) { string msg = "刷新功能信息"; if (addCount > 0) { msg += ",添加功能信息 " + addCount + " 个"; } if (updateCount > 0) { msg += ",更新功能信息 " + updateCount + " 个"; } if (removeCount > 0) { msg += ",移除功能信息 " + removeCount + " 个"; } _logger.LogInformation(msg); } }
/// <summary> /// 从数据库获取最新功能信息 /// </summary> /// <returns></returns> protected virtual TFunction[] GetFromDatabase() { IRepository <TFunction, Guid> repository = ScopedServiceProvider.GetService <IRepository <TFunction, Guid> >(); return(repository.Query().ToArray()); }
private IWarrantyService GetWarrantyService() { return(ScopedServiceProvider.GetRequiredService <IWarrantyService>()); }