Beispiel #1
0
 public static void Save(this TransactionScope scope, DataAccessModel dataAccessModel)
 {
     if (!dataAccessModel.IsDisposed)
     {
         dataAccessModel.Flush();
     }
 }
Beispiel #2
0
 internal TransactionContext(DataAccessTransaction dataAccessTransaction, DataAccessModel dataAccessModel)
 {
     this.dataAccessModel              = dataAccessModel;
     this.DataAccessTransaction        = dataAccessTransaction;
     this.DatabaseContextCategoriesKey = "*";
     this.attributes       = new Dictionary <string, object>();
     this.executionVersion = dataAccessModel.AsyncLocalExecutionVersion;
 }
        public virtual void Backup(DataAccessModel dataAccessModel)
        {
            if (dataAccessModel == this)
            {
                throw new InvalidOperationException("Cannot backup to self");
            }

            GetCurrentSqlDatabaseContext().Backup(dataAccessModel.GetCurrentSqlDatabaseContext());
        }
Beispiel #4
0
        public TransactionContext(DataAccessModel dataAccessModel, Transaction transaction)
        {
            this.DataAccessModel = dataAccessModel;
            this.Transaction     = transaction;
            this.DatabaseContextCategoriesKey = ".";
            this.ResourceManagerIdentifier    = Guid.NewGuid();

            this.persistenceTransactionContextsBySqlDatabaseContexts = new Dictionary <SqlDatabaseContext, TransactionEntry>(PrimeNumbers.Prime7);
        }
        internal TransactionContext(DataAccessTransaction dataAccessTransaction, DataAccessModel dataAccessModel)
        {
            this.dataAccessModel              = dataAccessModel;
            this.DataAccessTransaction        = dataAccessTransaction;
            this.DatabaseContextCategoriesKey = "*";
            this.executionVersion             = dataAccessModel.AsyncLocalExecutionVersion;

            this.commandsContextsBySqlDatabaseContexts = new ConcurrentDictionary <SqlDatabaseContext, SqlTransactionalCommandsContext>();
        }
Beispiel #6
0
        public void Flush(DataAccessModel dataAccessModel)
        {
            this.transaction.CheckAborted();

            if (!dataAccessModel.IsDisposed)
            {
                dataAccessModel.Flush();
            }
        }
        public static TransactionContext GetCurrentContext(DataAccessModel dataAccessModel, bool forWrite)
        {
            TransactionContext context;
            var dataAccessTransaction = DataAccessTransaction.Current;

            if (dataAccessTransaction == null && Transaction.Current != null)
            {
                dataAccessTransaction = DataAccessTransaction.Current = new DataAccessTransaction(DataAccessIsolationLevel.Unspecified);
            }

            if (dataAccessTransaction == null)
            {
                if (forWrite)
                {
                    throw new InvalidOperationException("Write operation must be performed inside a scope");
                }

                context = dataAccessModel.AsyncLocalTransactionContext;

                if (context == null || context.disposed)
                {
                    context = new TransactionContext(null, dataAccessModel);

                    dataAccessModel.AsyncLocalTransactionContext = context;
                }

                return(context);
            }

            if (dataAccessTransaction.SystemTransaction?.TransactionInformation.Status == TransactionStatus.Aborted)
            {
                throw new TransactionAbortedException();
            }

            var contexts = dataAccessTransaction.transactionContextsByDataAccessModel;

            var skipTest = false;

            if (contexts == null)
            {
                skipTest = true;
                contexts = dataAccessTransaction.transactionContextsByDataAccessModel = new Dictionary <DataAccessModel, TransactionContext>();
            }

            if (skipTest || !contexts.TryGetValue(dataAccessModel, out context))
            {
                context = new TransactionContext(dataAccessTransaction, dataAccessModel);
                contexts[dataAccessModel] = context;
                dataAccessModel.AsyncLocalTransactionContext = context;

                dataAccessTransaction.AddTransactionContext(context);
            }

            return(context);
        }
        protected DataAccessObjectsQueryable(DataAccessModel dataAccessModel, Expression expression)
        {
            if (this.DataAccessModel != null)
            {
                throw new ObjectAlreadyInitializedException(this);
            }

            this.DataAccessModel = dataAccessModel;
            this.typeDescriptor  = dataAccessModel.TypeDescriptorProvider.GetTypeDescriptor(typeof(T));
            this.Initialize(this.DataAccessModel.NewQueryProvider(), expression);
        }
        public RelatedDataAccessObjects(DataAccessModel dataAccessModel, IDataAccessObjectAdvanced parentDataAccessObject, string parentPropertyName)
            : base(dataAccessModel)
        {
            this.RelatedDataAccessObject = parentDataAccessObject;

            var parentType = this.DataAccessModel.TypeDescriptorProvider.GetTypeDescriptor(this.DataAccessModel.GetDefinitionTypeFromConcreteType(parentDataAccessObject.GetType()));

            this.relationshipInfo = parentType.GetRelationshipInfos().Single(c => c.ReferencingProperty.PropertyName == parentPropertyName);

            this.Condition = CreateJoinCondition(this.relationshipInfo.TargetProperty);
            this.InitializeDataAccessObject = GetInitializeRelatedMethod(parentType, this.relationshipInfo.TargetProperty);
        }
        protected SqlTransactionalCommandsContext(SqlDatabaseContext sqlDatabaseContext, IDbConnection dbConnection, Transaction transaction)
        {
            this.DbConnection       = dbConnection;
            this.Transaction        = transaction;
            this.SqlDatabaseContext = sqlDatabaseContext;
            this.DataAccessModel    = sqlDatabaseContext.DataAccessModel;

            if (transaction != null)
            {
                this.dbTransaction = dbConnection.BeginTransaction(ConvertIsolationLevel(transaction.IsolationLevel));
            }
        }
Beispiel #11
0
        protected DataAccessObjectsQueryable(DataAccessModel dataAccessModel, Expression expression)
            : base(dataAccessModel.NewQueryProvider(), expression)
        {
            this.DataAccessModel = dataAccessModel;

            this.typeDescriptor = dataAccessModel.TypeDescriptorProvider.GetTypeDescriptor(typeof(T));

            if (this.typeDescriptor.PrimaryKeyCount > 0)
            {
                this.idType             = this.typeDescriptor.PrimaryKeyProperties[0].PropertyType;
                this.primaryKeyProperty = this.typeDescriptor.PrimaryKeyProperties[0].PropertyInfo;
            }
        }
        /// <summary>
        /// Gets the <see cref="DataAccessModelTransactionManager "/> for the current <see cref="Shaolinq.DataAccessModel"/> for the current thread.
        /// </summary>
        /// <remarks>
        /// The framework does not support accessing objects created within different transactions
        /// from other transactions. For each Transaction there may be more than one attached
        /// <see cref="TransactionContext"/> (one for each thread that participates in the transaction).
        /// </remarks>
        public static DataAccessModelTransactionManager GetAmbientTransactionManager(DataAccessModel dataAccessModel)
        {
            DataAccessModelTransactionManager retval;
            var transactionManagers = ambientTransactionManagers.Value;

            if (!transactionManagers.TryGetValue(dataAccessModel, out retval))
            {
                retval = new DataAccessModelTransactionManager(dataAccessModel);

                transactionManagers[dataAccessModel] = retval;
            }

            return(retval);
        }
Beispiel #13
0
        public static DataAccessModelConfiguration GetDefaultConfiguration(Type type)
        {
            var typeName      = type.Name;
            var configuration = DataAccessModel.GetConfiguration(typeName);

            if (configuration != null)
            {
                return(configuration);
            }

            if (typeName.EndsWith("DataAccessModel"))
            {
                configuration = DataAccessModel.GetConfiguration(typeName.Left(typeName.Length - "DataAccessModel".Length));

                return(configuration);
            }

            return(null);
        }
Beispiel #14
0
        internal static TransactionExecutionContext Acquire(DataAccessModel dataAccessModel, bool forWrite)
        {
            var context = GetOrCreateCurrent(dataAccessModel, forWrite, true);

            if (context == null)
            {
                throw new InvalidOperationException("No Current TransactionContext");
            }

            if (context.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            var retval = new TransactionExecutionContext(context);

            retval.Finished += context.OnVersionContextFinished;

            return(retval);
        }
Beispiel #15
0
        /// <summary>
        /// Gets the <see cref="DataAccessModelTransactionManager "/> for the current <see cref="Shaolinq.DataAccessModel"/> for the current thread.
        /// </summary>
        /// <remarks>
        /// The framework does not support accessing objects created within different transactions
        /// from other transactions. For each Transaction there may be more than one attached
        /// <see cref="TransactionContext"/> (one for each thread that participates in the transaction).
        /// </remarks>
        public static DataAccessModelTransactionManager GetAmbientTransactionManager(DataAccessModel dataAccessModel)
        {
            DataAccessModelTransactionManager retval;
            var transactionManagers = AmbientTransactionManagers;

            if (transactionManagers == null)
            {
                transactionManagers = new Dictionary <DataAccessModel, DataAccessModelTransactionManager>();

                DataAccessModelTransactionManager.AmbientTransactionManagers = transactionManagers;
            }

            if (!transactionManagers.TryGetValue(dataAccessModel, out retval))
            {
                retval = new DataAccessModelTransactionManager(dataAccessModel);

                transactionManagers[dataAccessModel] = retval;
            }

            return(retval);
        }
Beispiel #16
0
        public DataAccessModelTransactionManager(DataAccessModel dataAccessModel)
        {
            EventHandler handler  = null;
            var          weakThis = new WeakReference(this);

            this.DataAccessModel             = dataAccessModel;
            transactionContextsByTransaction = new Dictionary <Transaction, TransactionContext>();

            handler = delegate(object sender, EventArgs eventArgs)
            {
                var strongThis = (DataAccessModelTransactionManager)weakThis.Target;

                if (strongThis != null)
                {
                    strongThis.Dispose();
                }
                else
                {
                    ((DataAccessModel)sender).Disposed -= handler;
                }
            };

            this.DataAccessModel.Disposed += handler;
        }
Beispiel #17
0
 public static void Flush(this TransactionScope scope, DataAccessModel dataAccessModel)
 {
     scope.Save(dataAccessModel);
 }
Beispiel #18
0
 public static int GetCurrentTransactionContextVersion(DataAccessModel dataAccessModel)
 {
     return(dataAccessModel.AsyncLocalExecutionVersion);
 }
Beispiel #19
0
 public DataAccessObjectDataContext(DataAccessModel dataAccessModel, SqlDatabaseContext sqlDatabaseContext, bool disableCache)
 {
     this.DisableCache       = disableCache;
     this.DataAccessModel    = dataAccessModel;
     this.SqlDatabaseContext = sqlDatabaseContext;
 }
Beispiel #20
0
        private static TransactionContext GetOrCreateCurrent(DataAccessModel dataAccessModel, bool forWrite, bool createTransactionIfNotExist)
        {
            TransactionContext context;
            var dataAccessTransaction = DataAccessTransaction.Current;

            if (dataAccessTransaction == null && Transaction.Current != null)
            {
                dataAccessTransaction = DataAccessTransaction.Current = new DataAccessTransaction(DataAccessIsolationLevel.Unspecified);
            }

            if (dataAccessTransaction == null)
            {
                if (forWrite)
                {
                    throw new InvalidOperationException("Write operation must be performed inside a scope");
                }

                context = dataAccessModel.AsyncLocalAmbientTransactionContext;

                if (context == null || context.disposed)
                {
                    if (!createTransactionIfNotExist)
                    {
                        if (context != null)
                        {
                            dataAccessModel.AsyncLocalAmbientTransactionContext = null;
                        }

                        return(null);
                    }

                    context = new TransactionContext(null, dataAccessModel);

                    dataAccessModel.AsyncLocalAmbientTransactionContext = context;
                }

                if (context.currentlyCommiting)
                {
                    throw new InvalidOperationException("The context is currently committing");
                }

                return(context);
            }

            if (dataAccessTransaction.systemTransactionCompleted && dataAccessTransaction.systemTransactionStatus == TransactionStatus.Aborted)
            {
                throw new TransactionAbortedException();
            }

            if (dataAccessTransaction.SystemTransaction?.TransactionInformation.Status == TransactionStatus.Aborted)
            {
                throw new TransactionAbortedException();
            }

            var contexts = dataAccessTransaction.transactionContextsByDataAccessModel;

            var skipTest = false;

            if (contexts == null)
            {
                skipTest = true;
                contexts = dataAccessTransaction.transactionContextsByDataAccessModel = new Dictionary <DataAccessModel, TransactionContext>();
            }

            if (skipTest || !contexts.TryGetValue(dataAccessModel, out context))
            {
                context = new TransactionContext(dataAccessTransaction, dataAccessModel);
                contexts[dataAccessModel] = context;

                dataAccessTransaction.AddTransactionContext(context);
            }

            if (context.currentlyCommiting)
            {
                throw new InvalidOperationException("The context is currently committing");
            }

            return(context);
        }
Beispiel #21
0
 public static TransactionContext GetCurrent(DataAccessModel dataAccessModel, bool forWrite)
 {
     return(GetOrCreateCurrent(dataAccessModel, forWrite, false));
 }
        public RelatedDataAccessObjects(IDataAccessObjectAdvanced relatedDataAccessObject, DataAccessModel dataAccessModel, EntityRelationshipType relationshipType, string propertyName)
            : base(dataAccessModel, null)
        {
            this.PropertyName            = propertyName;
            this.RelatedDataAccessObject = relatedDataAccessObject;
            this.RelationshipType        = relationshipType;
            this.ExtraCondition          = this.GetExtraCondition();
            this.PersistenceQueryProvider.RelatedDataAccessObjectContext = this;

            this.BuildInitializeRelatedMethod();
        }
Beispiel #23
0
 public DataAccessObjects(DataAccessModel dataAccessModel, Expression expression)
     : base(dataAccessModel, expression)
 {
 }
Beispiel #24
0
 protected DataAccessObjectsQueryable(DataAccessModel dataAccessModel)
     : this(dataAccessModel, null)
 {
 }
 public DataAccessObjectDataContext(DataAccessModel dataAccessModel, SqlDatabaseContext sqlDatabaseContext)
 {
     this.DataAccessModel    = dataAccessModel;
     this.SqlDatabaseContext = sqlDatabaseContext;
 }
Beispiel #26
0
 public static void Flush(this TransactionScope scope, DataAccessModel dataAccessModel)
 {
     dataAccessModel.Flush();
 }
Beispiel #27
0
 public static void SetReadOnly(this TransactionScope scope, DataAccessModel dataAccessModel)
 {
     dataAccessModel.SetCurentTransactionReadOnly();
 }
Beispiel #28
0
 /// <summary>
 /// Retrieves the current <see cref="SqlTransactionalCommandsContext"/> for direct access to the database.
 /// </summary>
 /// <param name="scope">The current scope</param>
 /// <param name="model">The dataaccess model</param>
 /// <returns>The <see cref="SqlTransactionalCommandsContext"/></returns>
 public static SqlTransactionalCommandsContext GetCurrentSqlTransactionalCommandsContext(this TransactionScope scope, DataAccessModel model)
 {
     return(model.GetCurrentCommandsContext());
 }