Пример #1
0
 private void TrySetContextType(EntityContextOptions options)
 {
     if (options is IInstanceIdentifier identifier && identifier.ContextType == null)
     {
         identifier.ContextType = GetType();
     }
 }
Пример #2
0
        /// <summary>
        /// 尝试从配置文件中配置 <see cref="EntityContextOptions"/> 实例。
        /// </summary>
        /// <param name="options"></param>
        protected virtual void TryPaserInstanceSetting(EntityContextOptions options)
        {
            if (options.Provider == null && options.ConnectionString == null)
            {
                var section = ConfigurationUnity.GetSection <InstanceConfigurationSection>();
                if (section != null)
                {
                    IInstanceConfigurationSetting setting;

                    if (!string.IsNullOrEmpty(options.ConfigName))
                    {
                        setting = section.Settings[options.ConfigName];
                    }
                    else
                    {
                        setting = section.Default;
                    }

                    if (setting != null)
                    {
                        options.Provider         = ProviderHelper.GetDefinedProviderInstance(setting);
                        options.ConnectionString = setting.ConnectionString;
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// 初始化。
 /// </summary>
 private void Initialize(EntityContextOptions options)
 {
     context = new InternalContext(options)
     {
         OnRespositoryCreated      = OnRespositoryCreated,
         OnRespositoryCreateFailed = OnRespositoryCreateFailed
     };
 }
        /// <summary>
        /// 初始化 <see cref="EntityContextOptionsBuilder"/> 类的新实例。
        /// </summary>
        /// <param name="options"></param>
        public EntityContextOptionsBuilder(EntityContextOptions options)
        {
            Options = options;

            if (Options is IInstanceIdentifier identifier && identifier.ContextType != null)
            {
                _contextType = identifier.ContextType;
            }
        }
Пример #5
0
        /// <summary>
        /// 初始化 <see cref="EntityContext"/> 类的新实例。
        /// </summary>
        /// <param name="options">选项参数。</param>
        public EntityContext(EntityContextOptions options)
        {
            this.options = options;

            OnConfiguring(new EntityContextOptionsBuilder(options));
            Initialize(options);

            new EntityRepositoryDiscoveryService(this).InitializeSets();
        }
Пример #6
0
 /// <summary>
 /// 初始化。
 /// </summary>
 private void Initialize(EntityContextOptions options)
 {
     context = new InternalContext(options.ConfigName)
     {
         AutoCreateTables          = options.AutoCreateTables,
         OnRespositoryCreated      = OnRespositoryCreated,
         OnRespositoryCreateFailed = OnRespositoryCreateFailed
     };
 }
Пример #7
0
        /// <summary>
        /// 初始化 <see cref="EntityContext"/> 类的新实例。
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="options">选项参数。</param>
        protected EntityContext(IServiceProvider serviceProvider, EntityContextOptions options)
        {
            _options         = options;
            _serviceProvider = serviceProvider;

            Initialize(options);

            new EntityRepositoryDiscoveryService(this, options).InitializeSets();
        }
Пример #8
0
 /// <summary>
 /// 初始化 <see cref="EntityTreeRepository{TEntity}"/> 类的新实例。
 /// </summary>
 /// <param name="repository"></param>
 /// <param name="service"></param>
 public EntityTreeRepository(IRepository <TEntity> repository, IContextService service)
 {
     this.repository = repository;
     entityType      = typeof(TEntity);
     metadata        = EntityMetadataUnity.GetEntityMetadata(entityType);
     metaTree        = metadata.EntityTree;
     database        = service.Database;
     syntax          = database.Provider.GetService <ISyntaxProvider>();
     options         = service.InitializeContext.Options;
 }
Пример #9
0
        /// <summary>
        /// 初始化 <see cref="EntityRepository{TEntity}"/> 类的新实例。
        /// </summary>
        /// <param name="contextService"></param>
        /// <param name="options"></param>
        public EntityRepository(IContextService contextService, IRepositoryProvider <TEntity> repositoryProxy, EntityContextOptions options)
        {
            _contextService  = contextService;
            _repositoryProxy = repositoryProxy;
            _options         = options;
            _provider        = options.Provider;
            EntityType       = typeof(TEntity);

            _subMgr = PersistentSubscribeManager.GetRequiredManager(contextService.ContextType);
        }
Пример #10
0
        /// <summary>
        /// 销毁资源。
        /// </summary>
        /// <param name="disposing">如果为 true,则同时释放托管资源和非托管资源;如果为 false,则仅释放非托管资源。</param>
        protected override bool Dispose(bool disposing)
        {
            if (_poolName == null)
            {
                Tracer.Debug($"The {GetType().Name} is Disposing.");

                _contextService?.TryDispose(disposing);
                _contextService = null;

                _options = null;

                return(true);
            }

            return(false);
        }
Пример #11
0
        /// <summary>
        /// 初始化。
        /// </summary>
        private void Initialize(EntityContextOptions options)
        {
            Guard.ArgumentNull(options, nameof(options));

            EntityContextInitializeContext initContext = null;

            if (options.ContextFactory != null)
            {
                initContext = options.ContextFactory();
            }
            else
            {
                var section = ConfigurationUnity.GetSection <InstanceConfigurationSection>();
                if (section != null)
                {
                    IInstanceConfigurationSetting setting;

                    if (!string.IsNullOrEmpty(options.ConfigName))
                    {
                        setting = section.Settings[options.ConfigName];
                    }
                    else
                    {
                        setting = section.Default;
                    }

                    initContext = new EntityContextInitializeContext(options, ProviderHelper.GetDefinedProviderInstance(setting), setting.ConnectionString);
                }
            }

            if (initContext == null || initContext.Provider == null)
            {
                throw new InvalidOperationException(SR.GetString(SRKind.MustAssignEntityContextInitializeContext));
            }

            initContext.Options = options;

            var provider = initContext.Provider.GetService <IContextProvider>();

            service = provider.CreateContextService(initContext);

            if (service != null)
            {
                service.OnRespositoryChanged = OnRespositoryChanged;
            }
        }
Пример #12
0
        /// <summary>
        /// 初始化。
        /// </summary>
        private void Initialize(EntityContextOptions options)
        {
            Guard.ArgumentNull(options, nameof(options));

            TrySetContextType(options);
            TrySetServiceProvider(options);
            TryPaserInstanceSetting(options);
            TryHandleConnectionTenancy(options);

            var builder = new EntityContextOptionsBuilder(options);

            OnConfiguring(builder);

            if (options.Provider == null)
            {
                throw new NotSupportedException(SR.GetString(SRKind.NotSupportDbProvider));
            }

            var contextProvider = options.GetProviderService <IContextProvider>();

            _contextService = contextProvider.CreateContextService(new ContextServiceContext(_serviceProvider, options));
        }
Пример #13
0
        /// <summary>
        /// 尝试初始化 <see cref="IServiceProvider"/> 实例。
        /// </summary>
        /// <param name="options"></param>
        protected virtual void TrySetServiceProvider(EntityContextOptions options)
        {
            if (options is IInstanceIdentifier identification)
            {
                if (identification.ServiceProvider == null && _serviceProvider != null)
                {
                    identification.ServiceProvider = _serviceProvider;
                }
                else if (ServiceProvider == null && identification.ServiceProvider != null)
                {
                    _serviceProvider = identification.ServiceProvider;
                }
                else if (identification.ServiceProvider == null && _serviceProvider == null)
                {
                    _serviceProvider = identification.ServiceProvider = ContainerUnity.GetContainer();
                }
            }

            if (_serviceProvider == null)
            {
                _serviceProvider = ContainerUnity.GetContainer();
            }
        }
 /// <summary>
 /// 初始化 <see cref="EntityContextOptionsBuilder"/> 类的新实例。
 /// </summary>
 /// <param name="contextType"></param>
 /// <param name="options"></param>
 public EntityContextOptionsBuilder(Type contextType, EntityContextOptions options)
     : this(options)
 {
     _contextType = contextType;
 }
Пример #15
0
 /// <summary>
 /// 初始化 <see cref="EntityRepository{TEntity}"/> 类的新实例。
 /// </summary>
 /// <param name="context"></param>
 /// <param name="options"></param>
 public EntityRepository(IRepositoryProvider <TEntity> repositoryProxy, EntityContextOptions options)
 {
     this.repositoryProxy = repositoryProxy;
     this.options         = options;
     EntityType           = typeof(TEntity);
 }
Пример #16
0
 internal EntityContextInitializeContext(EntityContextOptions options, IProvider provider, ConnectionString connectionStr)
     : this(provider, connectionStr)
 {
     Options = options;
 }
Пример #17
0
 /// <summary>
 /// 初始化 <see cref="EntityContext"/> 类的新实例。
 /// </summary>
 protected EntityContext(EntityContextOptions options)
     : this(null, options)
 {
 }
 public EntityContextOptionsBuilder(EntityContextOptions options)
 {
     Options = options;
 }
Пример #19
0
 IRepository IRepositoryProvider.CreateRepository(EntityContextOptions options)
 {
     return(new EntityRepository <TEntity>(this, options));
 }
Пример #20
0
 IRepository IRepositoryProvider.CreateRepository(EntityContextOptions options)
 {
     return(_repository ?? (_repository = new EntityRepository <TEntity>(_contextService, this, options)));
 }
Пример #21
0
        /// <summary>
        /// 初始化 <see cref="EntityContext"/> 类的新实例。
        /// </summary>
        /// <param name="options">选项参数。</param>
        public EntityContext(EntityContextOptions options)
        {
            Initialize(options);

            new EntityRepositoryDiscoveryService(this).InitializeSets();
        }
Пример #22
0
 // <summary>
 // Creates a set discovery service for the given derived context.
 // </summary>
 public EntityRepositoryDiscoveryService(EntityContext context, EntityContextOptions options)
 {
     _context        = context;
     _options        = options;
     _contextService = context.GetService <IContextService>();
 }