Exemple #1
0
 public ObjectsByIdCache(DataAccessObjectDataContext dataAccessObjectDataContext)
 {
     this.dataAccessObjectDataContext = dataAccessObjectDataContext;
     this.objectsByIdCache            = new Dictionary <Type, Dictionary <T, DataAccessObject> >(PrimeNumbers.Prime67);
     this.objectsNotReadyForCommit    = new Dictionary <Type, HashSet <DataAccessObject> >(PrimeNumbers.Prime67);
     this.newObjects = new Dictionary <Type, Dictionary <DataAccessObject, DataAccessObject> >(PrimeNumbers.Prime67);
 }
Exemple #2
0
            public ObjectsByCondition(DataAccessObjectDataContext dataAccessObjectDataContext)
            {
                this.dataAccessObjectDataContext = dataAccessObjectDataContext;

                this.objectsDeletedByCondition   = new Dictionary <ConditionalKey, DataAccessObject>(ConditionalKeyComparer.Default);
                this.objectsForUpdateByCondition = new Dictionary <ConditionalKey, DataAccessObject>(ConditionalKeyComparer.Default);
            }
Exemple #3
0
 public ObjectsByIdCache(Type type, DataAccessObjectDataContext dataAccessObjectDataContext, Func <DataAccessObject, K> getIdFunc, IEqualityComparer <K> keyComparer)
 {
     this.Type      = type;
     this.getIdFunc = getIdFunc;
     this.dataAccessObjectDataContext = dataAccessObjectDataContext;
     this.objectsByIdCache            = new Dictionary <K, DataAccessObject>(keyComparer ?? EqualityComparer <K> .Default);
     this.newObjects = new Dictionary <DataAccessObject, DataAccessObject>(DataAccessObjectServerSidePropertiesAccountingComparer.Default);
 }
        public DataAccessObjectDataContext GetCurrentDataContext()
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            return(this.dataAccessObjectDataContext ?? (this.dataAccessObjectDataContext = new DataAccessObjectDataContext(this.dataAccessModel, this.dataAccessModel.GetCurrentSqlDatabaseContext())));
        }
 public ObjectsByIdCache(Type type, DataAccessObjectDataContext dataAccessObjectDataContext, Func <DataAccessObject, K> getIdFunc, IEqualityComparer <K> keyComparer)
 {
     this.Type      = type;
     this.getIdFunc = getIdFunc;
     this.dataAccessObjectDataContext = dataAccessObjectDataContext;
     this.objectsByIdCache            = new Dictionary <K, DataAccessObject>(keyComparer ?? EqualityComparer <K> .Default);
     this.objectsDeleted           = new Dictionary <K, DataAccessObject>();
     this.objectsByPredicateCache  = new Dictionary <LambdaExpression, DataAccessObject>();
     this.objectsNotReadyForCommit = new HashSet <DataAccessObject>(ObjectReferenceIdentityEqualityComparer <IDataAccessObjectAdvanced> .Default);
     this.newObjects = new Dictionary <DataAccessObject, DataAccessObject>(DataAccessObjectServerSidePropertiesAccountingComparer.Default);
 }
        protected void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            List <Exception> exceptions = null;

            foreach (var commandsContext in this.commandsContextsBySqlDatabaseContexts.Values)
            {
                try
                {
                    commandsContext.Dispose();
                }
                catch (Exception e)
                {
                    if (exceptions == null)
                    {
                        exceptions = new List <Exception>();
                    }

                    exceptions.Add(e);
                }
            }

            try
            {
                this.DataAccessTransaction?.RemoveTransactionContext(this);
                this.dataAccessModel.AsyncLocalTransactionContext = null;
                this.dataAccessObjectDataContext = null;
            }
            finally
            {
                this.disposed = true;

                if (exceptions?.Count > 0)
                {
                    throw new AggregateException(exceptions);
                }
            }

            if (this.DataAccessTransaction != null)
            {
                if (!DataAccessTransaction.HasSystemTransaction)
                {
                    DataAccessTransaction.Current?.Dispose();
                }
            }
        }
        private void OnVersionContextFinished(object sender, EventArgs eventArgs)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            if (this.DataAccessTransaction == null)
            {
                this.dataAccessObjectDataContext = null;
                this.commandsContext?.Dispose();
                this.dataAccessModel.AsyncLocalAmbientTransactionContext = null;
                this.Dispose();
            }
        }
        protected void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            GC.SuppressFinalize(this);

            try
            {
                this.commandsContext?.Dispose();
                this.DataAccessTransaction?.RemoveTransactionContext(this);
                this.dataAccessObjectDataContext = null;
            }
            finally
            {
                this.disposed = true;
            }
        }
        internal void VersionContextFinished(TransactionContextExecutionVersionContext executionVersionContext)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            if (this.DataAccessTransaction == null)
            {
                this.dataAccessObjectDataContext = null;

                foreach (var cc in this.commandsContextsBySqlDatabaseContexts.Values)
                {
                    cc.Dispose();
                }

                this.commandsContextsBySqlDatabaseContexts.Clear();
                this.dataAccessModel.AsyncLocalTransactionContext = null;
                this.Dispose();
            }
        }
        public virtual DatabaseTransactionContextAcquisition AcquirePersistenceTransactionContext(SqlDatabaseContext sqlDatabaseContext)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException(nameof(TransactionContext));
            }

            SqlTransactionalCommandsContext commandsContext;

            if (!this.commandsContextsBySqlDatabaseContexts.TryGetValue(sqlDatabaseContext, out commandsContext))
            {
                commandsContext = sqlDatabaseContext.CreateSqlTransactionalCommandsContext(this.DataAccessTransaction);

                this.commandsContextsBySqlDatabaseContexts[sqlDatabaseContext] = commandsContext;
            }

            var startIndex = this.GetExecutionVersion();

            var retval = new DatabaseTransactionContextAcquisition(this, sqlDatabaseContext, commandsContext);

            if (this.DataAccessTransaction == null)
            {
                retval.Disposed += (s, e) =>
                {
                    if (this.GetExecutionVersion() <= startIndex)
                    {
                        this.dataAccessObjectDataContext = null;

                        foreach (var cc in this.commandsContextsBySqlDatabaseContexts.Values)
                        {
                            cc.Dispose();
                        }

                        this.commandsContextsBySqlDatabaseContexts.Clear();
                    }
                };
            }

            return(retval);
        }
Exemple #11
0
 internal DataAccessModelHookSubmitContext(DataAccessObjectDataContext dataContext, bool isFlush)
 {
     this.dataContext = dataContext;
     this.IsFlush     = isFlush;
 }
        private static IObjectsByIdCache CreateCacheForDao(IDataAccessObjectAdvanced dao, DataAccessObjectDataContext context)
        {
            Func <DataAccessObjectDataContext, IObjectsByIdCache> func;
            var typeHandle = Type.GetTypeHandle(dao);

            if (!cacheConstructor.TryGetValue(typeHandle, out func))
            {
                var type = dao.GetType();

                var keyType     = dao.NumberOfPrimaryKeys > 1 ? typeof(CompositePrimaryKey) : dao.KeyType;
                var cacheType   = typeof(ObjectsByIdCache <>).MakeGenericType(keyType);
                var constructor = cacheType.GetConstructors().Single();

                Delegate   getIdFunc;
                Expression keyComparer;
                var        getIdFuncType = typeof(Func <,>).MakeGenericType(typeof(DataAccessObject), keyType);

                if (keyType != typeof(CompositePrimaryKey))
                {
                    keyComparer = Expression.Constant(null, typeof(IEqualityComparer <>).MakeGenericType(dao.KeyType ?? dao.CompositeKeyTypes[0]));

                    var param  = Expression.Parameter(typeof(DataAccessObject));
                    var lambda = Expression.Lambda(dao.TypeDescriptor.GetSinglePrimaryKeyExpression(Expression.Convert(param, type)), param);

                    getIdFunc = lambda.Compile();
                }
                else
                {
                    keyComparer = Expression.Constant(CompositePrimaryKeyComparer.Default);
                    getIdFunc   = Delegate.CreateDelegate(getIdFuncType, TypeUtils.GetMethod(() => GetDataAccessObjectCompositeId(default(DataAccessObject))));
                }

                var contextParam = Expression.Parameter(typeof(DataAccessObjectDataContext));

                func = Expression.Lambda <Func <DataAccessObjectDataContext, IObjectsByIdCache> >(Expression.New(constructor, Expression.Constant(dao.GetType()), contextParam, Expression.Constant(getIdFunc, getIdFunc.GetType()), keyComparer), contextParam).Compile();

                cacheConstructor = new Dictionary <RuntimeTypeHandle, Func <DataAccessObjectDataContext, IObjectsByIdCache> >(cacheConstructor)
                {
                    [typeHandle] = func
                };
            }

            return(func(context));
        }
Exemple #13
0
 internal DataAccessModelHookSubmitContext(TransactionContext transactionContext, DataAccessObjectDataContext dataContext, bool isFlush)
     : base(transactionContext)
 {
     this.dataContext = dataContext;
     this.IsFlush     = isFlush;
 }