예제 #1
0
        /// <summary>
        /// 初始化 <see cref="DefaultContextService"/> 类的新实例。
        /// </summary>
        /// <param name="context"></param>
        /// <param name="databaseFactory">一个用于创建 <see cref="IDatabase"/> 的工厂函数。</param>
        public DefaultContextService(EntityContextInitializeContext context, Func <IProvider, ConnectionString, IDatabase> databaseFactory)
            : base(context)
        {
            var options = context.Options;

            Func <IDatabase> factory = null;

            if (DatabaseScope.Current != null)
            {
                factory = () => DatabaseScope.Current.Database;
            }
            else if (databaseFactory != null)
            {
                factory = () => databaseFactory(context.Provider, context.ConnectionString);
            }
            else if (context.Provider != null && context.ConnectionString != null)
            {
                factory = () => new Database(context.ConnectionString, context.Provider);
            }
            else if (options != null)
            {
                factory = () => DatabaseFactory.CreateDatabase(options.ConfigName);
            }

            if (factory != null)
            {
                Database = EntityDatabaseFactory.CreateDatabase(InstanceName, factory);
                Provider = Database.Provider;
            }
        }
예제 #2
0
        internal static string TryAdd(EntityContextInitializeContext context)
        {
            lock (instances)
            {
                var item = instances.FirstOrDefault(s => s.Value == context);
                if (!string.IsNullOrEmpty(item.Key))
                {
                    return(item.Key);
                }

                var key = RandomGenerator.Create();
                instances.TryAdd(key, context);
                return(key);
            }
        }
예제 #3
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;
            }
        }
예제 #4
0
 /// <summary>
 /// 初始化 <see cref="ContextServiceBase"/> 类的新实例。
 /// </summary>
 /// <param name="context"></param>
 public ContextServiceBase(EntityContextInitializeContext context)
 {
     InitializeContext = context;
     InstanceName      = ContextInstanceManager.TryAdd(context);
 }
예제 #5
0
        IContextService IContextProvider.CreateContextService(EntityContextInitializeContext context)
        {
            var factory = context.DatabaseFactory ?? new Func <IProvider, ConnectionString, IDatabase>((p, s) => new Database(s, p));

            return(new DefaultContextService(context, factory));
        }
예제 #6
0
 IContextService IContextProvider.CreateContextService(EntityContextInitializeContext context)
 {
     return(new DefaultContextService(context, context.DatabaseFactory));
 }