Пример #1
0
        public virtual SqlDatabaseContext GetCurrentSqlDatabaseContext()
        {
            var forWrite = DataAccessTransaction.Current != null;

            var transactionContext = this.GetCurrentContext(forWrite);

            if (transactionContext.sqlDatabaseContext != null)
            {
                return(transactionContext.sqlDatabaseContext);
            }

            SqlDatabaseContextsInfo info;

            if (!this.sqlDatabaseContextsByCategory.TryGetValue(transactionContext.DatabaseContextCategoriesKey, out info))
            {
                var compositeInfo = SqlDatabaseContextsInfo.Create();

                foreach (var category in transactionContext.DatabaseContextCategories)
                {
                    info = this.sqlDatabaseContextsByCategory[category];

                    compositeInfo.DatabaseContexts.AddRange(info.DatabaseContexts);
                }

                info = this.sqlDatabaseContextsByCategory[transactionContext.DatabaseContextCategoriesKey] = compositeInfo;
            }

            var index = (int)(info.Count++ % info.DatabaseContexts.Count);

            transactionContext.sqlDatabaseContext = info.DatabaseContexts[index];

            return(transactionContext.sqlDatabaseContext);
        }
Пример #2
0
        private void SetAssemblyBuildInfo(RuntimeDataAccessModelInfo value)
        {
            this.RuntimeDataAccessModelInfo = value;
            this.DefinitionAssembly         = value.DefinitionAssembly;
            this.TypeDescriptorProvider     = value.TypeDescriptorProvider;
            this.ModelTypeDescriptor        = this.TypeDescriptorProvider.ModelTypeDescriptor;

            foreach (var contextInfo in this.Configuration.SqlDatabaseContextInfos)
            {
                SqlDatabaseContextsInfo info;
                var newSqlDatabaseContext = contextInfo.CreateSqlDatabaseContext(this);

                if (newSqlDatabaseContext.ContextCategories.Length == 0)
                {
                    if (!this.sqlDatabaseContextsByCategory.TryGetValue("*", out info))
                    {
                        info = SqlDatabaseContextsInfo.Create();

                        this.sqlDatabaseContextsByCategory["*"] = info;
                    }

                    info.DatabaseContexts.Add(newSqlDatabaseContext);
                }
                else
                {
                    foreach (var category in newSqlDatabaseContext.ContextCategories)
                    {
                        if (!this.sqlDatabaseContextsByCategory.TryGetValue(category, out info))
                        {
                            info = SqlDatabaseContextsInfo.Create();

                            this.sqlDatabaseContextsByCategory[category] = info;
                        }

                        info.DatabaseContexts.Add(newSqlDatabaseContext);
                    }
                }
            }

            if (!this.sqlDatabaseContextsByCategory.ContainsKey("*"))
            {
                throw new InvalidDataAccessObjectModelDefinition("Configuration must define at least one root DatabaseContext category");
            }
        }
Пример #3
0
        public virtual SqlDatabaseContext GetCurrentSqlDatabaseContext()
        {
            var forWrite = DataAccessTransaction.Current != null;

            var transactionContext = this.GetCurrentContext(forWrite);

            if (transactionContext?.sqlDatabaseContext != null)
            {
                return(transactionContext.sqlDatabaseContext);
            }

            SqlDatabaseContextsInfo info;

            var categories = transactionContext?.DatabaseContextCategoriesKey ?? "*";

            if (!this.sqlDatabaseContextsByCategory.TryGetValue(categories, out info))
            {
                var compositeInfo = SqlDatabaseContextsInfo.Create();

                if (transactionContext != null)
                {
                    foreach (var category in transactionContext.DatabaseContextCategoriesKey.Split(",").Select(c => c.Trim()))
                    {
                        info = this.sqlDatabaseContextsByCategory[category];

                        compositeInfo.DatabaseContexts.AddRange(info.DatabaseContexts);
                    }
                }

                info = this.sqlDatabaseContextsByCategory[categories] = compositeInfo;
            }

            var index  = (int)(info.Count++ % info.DatabaseContexts.Count);
            var retval = info.DatabaseContexts[index];

            if (transactionContext != null)
            {
                transactionContext.sqlDatabaseContext = retval;
            }

            return(retval);
        }