/// <summary>
        /// 获取数据上下文的变更日志信息
        /// </summary>
        public static IEnumerable <DataLog> GetEntityDataLogs(this DbContext dbContext, IServiceProvider provider)
        {
            if (provider == null)
            {
                return(Enumerable.Empty <DataLog>());
            }
            IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();

            if (entityInfoHandler == null)
            {
                return(Enumerable.Empty <DataLog>());
            }

            ObjectContext      objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
            ObjectStateManager manager       = objectContext.ObjectStateManager;

            IEnumerable <DataLog> logs = from entry in manager.GetObjectStateEntries(EntityState.Added).Where(entry => entry.Entity != null)
                                         let entityInfo = entityInfoHandler.GetEntityInfo(entry.Entity.GetType())
                                                          where entityInfo != null && entityInfo.DataLogEnabled
                                                          select GetAddedLog(entry, entityInfo);

            logs = logs.Concat(from entry in manager.GetObjectStateEntries(EntityState.Modified).Where(entry => entry.Entity != null)
                               let entityInfo = entityInfoHandler.GetEntityInfo(entry.Entity.GetType())
                                                where entityInfo != null && entityInfo.DataLogEnabled
                                                select GetModifiedLog(entry, entityInfo));

            logs = logs.Concat(from entry in manager.GetObjectStateEntries(EntityState.Deleted).Where(entry => entry.Entity != null)
                               let entityInfo = entityInfoHandler.GetEntityInfo(entry.Entity.GetType())
                                                where entityInfo != null && entityInfo.DataLogEnabled
                                                select GetDeletedLog(entry, entityInfo));

            return(logs);
        }
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            IModuleHandler moduleHandler = provider.GetService <IModuleHandler>();

            moduleHandler.Initialize();

            //初始化各种缓存
            IFunctionHandler functionHandler = provider.GetService <IFunctionHandler>();

            functionHandler.RefreshCache();

            IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();

            entityInfoHandler.RefreshCache();

            IFunctionAuthCache functionAuthCache = provider.GetService <IFunctionAuthCache>();

            functionAuthCache.BuildRoleCaches();

            IDataAuthCache dataAuthCache = provider.GetService <IDataAuthCache>();

            dataAuthCache.BuildCaches();

            IsEnabled = true;
        }
Exemple #3
0
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            IEntityInfoHandler handler = provider.GetService <IEntityInfoHandler>();

            handler.Initialize();
            IsEnabled = true;
        }
Exemple #4
0
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UseModule(IServiceProvider provider)
        {
            IEntityInfoHandler handler = provider.GetRequiredService <IEntityInfoHandler>();

            handler.Initialize();
            IsEnabled = true;
        }
        /// <summary>
        /// 获取上下文实体审计数据
        /// </summary>
        public static IList <AuditEntity> GetAuditEntities(this DbContext context)
        {
            List <AuditEntity> result            = new List <AuditEntity>();
            IEntityInfoHandler entityInfoHandler = ServiceLocator.Instance.GetService <IEntityInfoHandler>();

            if (entityInfoHandler == null)
            {
                return(result);
            }
            EntityState[]      states  = { EntityState.Added, EntityState.Modified, EntityState.Deleted };
            List <EntityEntry> entries = context.ChangeTracker.Entries().Where(m => m.Entity != null && states.Contains(m.State)).ToList();

            if (entries.Count == 0)
            {
                return(result);
            }
            foreach (EntityEntry entry in entries)
            {
                IEntityInfo entityInfo = entityInfoHandler.GetEntityInfo(entry.Entity.GetType());
                if (entityInfo == null || !entityInfo.AuditEnabled)
                {
                    continue;
                }
                result.Add(GetAuditEntity(entry, entityInfo));
            }
            return(result);
        }
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="app">应用程序构建器</param>
        public override void UsePack(IApplicationBuilder app)
        {
            IEntityInfoHandler handler = app.ApplicationServices.GetService <IEntityInfoHandler>();

            handler.Initialize();
            IsEnabled = true;
        }
Exemple #7
0
        /// <summary>
        /// 开始执行框架初始化
        /// </summary>
        /// <param name="iocBuilder">依赖注入构建器</param>
        public void Initialize(IIocBuilder iocBuilder)
        {
            iocBuilder.CheckNotNull("iocBuilder");

            OSharpConfig config = OSharpConfig.Instance;

            //依赖注入初始化
            IServiceProvider provider = iocBuilder.Build();

            //对象映射功能初始化
            IMappersBuilder mappersBuilder = provider.GetService <IMappersBuilder>();
            IMapper         mapper         = provider.GetService <IMapper>();

            if (!_mapperInitialized && mapper != null)
            {
                if (mappersBuilder != null)
                {
                    IEnumerable <IMapTuple> mapTuples = provider.GetServices <IMapTuple>();
                    mappersBuilder.Build(mapTuples);
                }
                MapperExtensions.SetMaper(mapper);
                _mapperInitialized = true;
            }

            //日志功能初始化
            IBasicLoggingInitializer loggingInitializer = provider.GetService <IBasicLoggingInitializer>();

            if (!_basicLoggingInitialized && loggingInitializer != null)
            {
                loggingInitializer.Initialize(config.LoggingConfig);
                _basicLoggingInitialized = true;
            }

            //数据库初始化
            IDatabaseInitializer databaseInitializer = provider.GetService <IDatabaseInitializer>();

            if (!_databaseInitialized && databaseInitializer != null)
            {
                databaseInitializer.Initialize(config.DataConfig);
                _databaseInitialized = true;
            }

            //实体信息初始化
            IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();

            if (!_entityInfoInitialized && entityInfoHandler != null)
            {
                entityInfoHandler.Initialize();
                _entityInfoInitialized = true;
            }
            //功能信息初始化
            IFunctionHandler functionHandler = provider.GetService <IFunctionHandler>();

            if (functionHandler != null)
            {
                functionHandler.Initialize();
            }
        }
Exemple #8
0
        /// <summary>
        /// 更新实体数据信息信息
        /// </summary>
        /// <param name="inputDtos">包含更新信息的实体数据信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public OperationResult EditEntityInfos(params EntityInfoInputDto[] inputDtos)
        {
            OperationResult result = EntityInfoRepository.Update(inputDtos);

            if (result.ResultType == OperationResultType.Success)
            {
                IEntityInfoHandler handler = ServiceProvider.GetService <IEntityInfoHandler>();
                handler.RefreshCache();
            }
            return(result);
        }
Exemple #9
0
        /// <summary>
        /// 更新实体数据信息信息
        /// </summary>
        /// <param name="dtos">包含更新信息的实体数据信息DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task <OperationResult> UpdateEntityInfos(params EntityInfoInputDto[] dtos)
        {
            OperationResult result = await EntityInfoRepository.UpdateAsync(dtos);

            if (result.Successed)
            {
                IEntityInfoHandler handler = ServiceProvider.GetService <IEntityInfoHandler>();
                handler.RefreshCache();
            }
            return(result);
        }
        /// <summary>
        /// 开始执行框架初始化
        /// </summary>
        /// <param name="services">服务映射集合</param>
        /// <param name="iocBuilder">依赖注入构建器</param>
        public void Initialize(IServiceCollection services, IIocBuilder iocBuilder)
        {
            services.CheckNotNull("services");
            iocBuilder.CheckNotNull("iocBuilder");

            OSharpConfig config = OSharpConfig.Instance;

            //使用副本进行初始化,防止不同平台间的相互污染
            IServiceCollection newServices = services.Clone();
            //依赖注入初始化
            IServiceProvider provider = iocBuilder.Build(newServices);

            //日志功能初始化
            IBasicLoggingInitializer loggingInitializer = provider.GetService <IBasicLoggingInitializer>();

            if (!_basicLoggingInitialized && loggingInitializer != null)
            {
                loggingInitializer.Initialize(config.LoggingConfig);
                _basicLoggingInitialized = true;
            }

            //数据库初始化
            IDatabaseInitializer databaseInitializer = provider.GetService <IDatabaseInitializer>();

            if (!_databaseInitialized)
            {
                if (databaseInitializer == null)
                {
                    throw new InvalidOperationException(Resources.FrameworkInitializerBase_DatabaseInitializeIsNull);
                }
                databaseInitializer.Initialize(config.DataConfig);
                _databaseInitialized = true;
            }

            //实体信息初始化
            if (!_entityInfoInitialized)
            {
                IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();
                if (entityInfoHandler == null)
                {
                    throw new InvalidOperationException(Resources.FrameworkInitializerBase_EntityInfoHandlerIsNull);
                }
                entityInfoHandler.Initialize();
                _entityInfoInitialized = true;
            }
            //功能信息初始化
            IFunctionHandler functionHandler = provider.GetService <IFunctionHandler>();

            if (functionHandler == null)
            {
                throw new InvalidOperationException(Resources.FrameworkInitializerBase_FunctionHandlerIsNull);
            }
            functionHandler.Initialize();
        }
        /// <summary>
        /// 应用模块服务
        /// </summary>
        /// <param name="provider">服务提供者</param>
        public override void UsePack(IServiceProvider provider)
        {
            IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();

            entityInfoHandler.RefreshCache();

            IDataAuthCache dataAuthCache = provider.GetService <IDataAuthCache>();

            dataAuthCache.BuildCaches();

            IsEnabled = true;
        }
Exemple #12
0
        /// <summary>
        /// OSharp框架初始化
        /// </summary>
        public static IApplicationBuilder UseOSharp(this IApplicationBuilder app)
        {
            IServiceProvider serviceProvider = app.ApplicationServices;

            //应用程序级别的服务定位器
            ServiceLocator.Instance.TrySetApplicationServiceProvider(serviceProvider);

            IEntityInfoHandler entityInfoHandler = serviceProvider.GetService <IEntityInfoHandler>();

            entityInfoHandler.Initialize();

            IFunctionHandler[] functionHandlers = serviceProvider.GetServices <IFunctionHandler>().ToArray();
            foreach (IFunctionHandler functionHandler in functionHandlers)
            {
                functionHandler.Initialize();
            }

            return(app);
        }
Exemple #13
0
        /// <summary>
        /// 获取上下文实体审计数据
        /// </summary>
        public static IList <AuditEntityEntry> GetAuditEntities(this DbContext context)
        {
            List <AuditEntityEntry> result = new List <AuditEntityEntry>();
            //当前操作的功能是否允许数据审计
            ScopedDictionary scopedDict = ServiceLocator.Instance.GetService <ScopedDictionary>();
            IFunction        function   = scopedDict?.Function;

            if (function == null || !function.AuditEntityEnabled)
            {
                return(result);
            }
            IEntityInfoHandler entityInfoHandler = ServiceLocator.Instance.GetService <IEntityInfoHandler>();

            if (entityInfoHandler == null)
            {
                return(result);
            }
            EntityState[]      states  = { EntityState.Added, EntityState.Modified, EntityState.Deleted };
            List <EntityEntry> entries = context.ChangeTracker.Entries().Where(m => m.Entity != null && states.Contains(m.State)).ToList();

            if (entries.Count == 0)
            {
                return(result);
            }
            foreach (EntityEntry entry in entries)
            {
                //当前操作的实体是否允许数据审计
                IEntityInfo entityInfo = entityInfoHandler.GetEntityInfo(entry.Entity.GetType());
                if (entityInfo == null || !entityInfo.AuditEnabled)
                {
                    continue;
                }
                result.AddIfNotNull(GetAuditEntity(entry, entityInfo));
            }
            return(result);
        }
 /// <summary>
 /// 初始化一个<see cref="AuditEntityProvider"/>类型的新实例
 /// </summary>
 public AuditEntityProvider(ScopedDictionary scopedDict, IEntityInfoHandler entityInfoHandler)
 {
     _scopedDict        = scopedDict;
     _entityInfoHandler = entityInfoHandler;
 }
Exemple #15
0
 /// <summary>
 /// 初始化一个<see cref="AuditEntityProvider"/>类型的新实例
 /// </summary>
 public AuditEntityProvider(IServiceProvider provider)
 {
     _logger            = provider.GetLogger(this);
     _scopedDict        = provider.GetService <ScopedDictionary>();
     _entityInfoHandler = provider.GetService <IEntityInfoHandler>();
 }
Exemple #16
0
        /// <summary>
        /// 开始执行框架初始化
        /// </summary>
        /// <param name="iocBuilder">依赖注入构建器</param>
        public void Initialize(IIocBuilder iocBuilder)
        {
            iocBuilder.CheckNotNull("iocBuilder");

            OSharpConfig config = OSharpConfig.Instance;

            //依赖注入初始化
            IServiceProvider provider = iocBuilder.Build();

            //对象映射功能初始化
            IMappersBuilder mappersBuilder = provider.GetService <IMappersBuilder>();
            IMapper         mapper         = provider.GetService <IMapper>();

            if (!_mapperInitialized)
            {
                if (mappersBuilder != null)
                {
                    IEnumerable <IMapTuple> mapTuples = provider.GetServices <IMapTuple>();
                    mappersBuilder.Build(mapTuples);
                }
                if (mapper != null)
                {
                    MapperExtensions.SetMaper(mapper);
                }
                _mapperInitialized = true;
            }

            //日志功能初始化
            IBasicLoggingInitializer loggingInitializer = provider.GetService <IBasicLoggingInitializer>();

            if (!_basicLoggingInitialized && loggingInitializer != null)
            {
                loggingInitializer.Initialize(config.LoggingConfig);
                _basicLoggingInitialized = true;
            }

            //数据库初始化
            IDatabaseInitializer databaseInitializer = provider.GetService <IDatabaseInitializer>();

            if (!_databaseInitialized)
            {
                if (databaseInitializer == null)
                {
                    throw new InvalidOperationException(Resources.FrameworkInitializer_DatabaseInitializeIsNull);
                }
                databaseInitializer.Initialize(config.DataConfig);
                _databaseInitialized = true;
            }

            //实体信息初始化
            if (!_entityInfoInitialized)
            {
                IEntityInfoHandler entityInfoHandler = provider.GetService <IEntityInfoHandler>();
                if (entityInfoHandler == null)
                {
                    throw new InvalidOperationException(Resources.FrameworkInitializer_EntityInfoHandlerIsNull);
                }
                entityInfoHandler.Initialize();
                _entityInfoInitialized = true;
            }
            //功能信息初始化
            IFunctionHandler functionHandler = provider.GetService <IFunctionHandler>();

            if (functionHandler == null)
            {
                throw new InvalidOperationException(Resources.FrameworkInitializer_FunctionHandlerIsNull);
            }
            functionHandler.Initialize();
        }