示例#1
0
        public virtual void Deleted(IDataAccessObjectAdvanced value)
        {
            if (value.IsDeleted)
            {
                return;
            }

            var keyType = value.KeyType;

            if (keyType == null && value.NumberOfPrimaryKeys > 1)
            {
                keyType = value.CompositeKeyTypes[0];
            }

            switch (Type.GetTypeCode(keyType))
            {
            case TypeCode.Int32:
                if (cacheByInt == null)
                {
                    cacheByInt = new ObjectsByIdCache <int>(this);
                }
                cacheByInt.Deleted((DataAccessObject <int>)value);
                break;

            case TypeCode.Int64:
                if (cacheByLong == null)
                {
                    cacheByLong = new ObjectsByIdCache <long>(this);
                }
                cacheByLong.Deleted((DataAccessObject <long>)value);
                break;

            case TypeCode.String:
                if (keyType == typeof(string))
                {
                    if (cacheByString == null)
                    {
                        cacheByString = new ObjectsByIdCache <string>(this);
                    }
                    cacheByString.Deleted((DataAccessObject <string>)value);
                }
                break;

            default:
                if (keyType == typeof(Guid))
                {
                    if (cacheByGuid == null)
                    {
                        cacheByGuid = new ObjectsByIdCache <Guid>(this);
                    }
                    cacheByGuid.Deleted((DataAccessObject <Guid>)value);
                }
                break;
            }
        }
示例#2
0
        public virtual DataAccessObject CacheObject(DataAccessObject value, bool forImport)
        {
            if (this.DisableCache)
            {
                return(value);
            }

            var keyType = value.GetAdvanced().KeyType;

            if (keyType == null && value.GetAdvanced().NumberOfPrimaryKeys > 1)
            {
                keyType = value.GetAdvanced().CompositeKeyTypes[0];
            }

            switch (Type.GetTypeCode(keyType))
            {
            case TypeCode.Int32:
                if (cacheByInt == null)
                {
                    cacheByInt = new ObjectsByIdCache <int>(this);
                }
                return(cacheByInt.Cache((DataAccessObject <int>)value, forImport));

            case TypeCode.Int64:
                if (cacheByLong == null)
                {
                    cacheByLong = new ObjectsByIdCache <long>(this);
                }
                return(cacheByLong.Cache((DataAccessObject <long>)value, forImport));

            default:
                if (keyType == typeof(Guid))
                {
                    if (cacheByGuid == null)
                    {
                        cacheByGuid = new ObjectsByIdCache <Guid>(this);
                    }
                    return(cacheByGuid.Cache((DataAccessObject <Guid>)value, forImport));
                }
                else if (keyType == typeof(string))
                {
                    if (cacheByString == null)
                    {
                        cacheByString = new ObjectsByIdCache <string>(this);
                    }
                    return(cacheByString.Cache((DataAccessObject <string>)value, forImport));
                }
                break;
            }

            return(value);
        }
示例#3
0
        private static void CommitUpdated <T>(SqlDatabaseContext sqlDatabaseContext, ObjectsByIdCache <T> cache, HashSet <DatabaseTransactionContextAcquisition> acquisitions, TransactionContext transactionContext)
        {
            var acquisition = transactionContext.AcquirePersistenceTransactionContext(sqlDatabaseContext);

            acquisitions.Add(acquisition);

            foreach (var j in cache.objectsByIdCache)
            {
                acquisition.SqlDatabaseCommandsContext.Update(j.Key, j.Value.Values);
            }

            if (cache.objectsByIdCacheComposite != null)
            {
                foreach (var j in cache.objectsByIdCacheComposite)
                {
                    acquisition.SqlDatabaseCommandsContext.Update(j.Key, j.Value.Values);
                }
            }
        }
示例#4
0
        public virtual DataAccessObject CacheObject(DataAccessObject value, bool forImport)
        {
            if (this.DisableCache)
            {
                return(value);
            }

            var keyType = value.GetAdvanced().KeyType;

            if (keyType == null && value.GetAdvanced().NumberOfPrimaryKeys > 1)
            {
                keyType = value.GetAdvanced().CompositeKeyTypes[0];
            }

            switch (Type.GetTypeCode(keyType))
            {
            case TypeCode.Int32:
                if (this.cacheByInt == null)
                {
                    this.cacheByInt = new ObjectsByIdCache <int>(this);
                }

                return(this.cacheByInt.Cache((DataAccessObject <int>)value, forImport));

            case TypeCode.Int64:
                if (this.cacheByLong == null)
                {
                    this.cacheByLong = new ObjectsByIdCache <long>(this);
                }

                return(this.cacheByLong.Cache((DataAccessObject <long>)value, forImport));

            case TypeCode.String:
                if (this.cacheByString == null)
                {
                    this.cacheByString = new ObjectsByIdCache <string>(this);
                }

                return(this.cacheByString.Cache((DataAccessObject <string>)value, forImport));

            default:
                if (keyType == typeof(Guid))
                {
                    if (this.cacheByGuid == null)
                    {
                        this.cacheByGuid = new ObjectsByIdCache <Guid>(this);
                    }

                    return(this.cacheByGuid.Cache((DataAccessObject <Guid>)value, forImport));
                }
                else if (keyType.IsDataAccessObjectType())
                {
                    IObjectsByIdCache cache;

                    if (this.cacheByDao == null)
                    {
                        this.cacheByDao = new Dictionary <Type, IObjectsByIdCache>();
                    }

                    if (!this.cacheByDao.TryGetValue(keyType, out cache))
                    {
                        cache = (IObjectsByIdCache)Activator.CreateInstance(typeof(ObjectsByIdCache <>).MakeGenericType(keyType), this);

                        this.cacheByDao[keyType] = cache;
                    }

                    return(cache.Cache(value, forImport));
                }
                break;
            }

            return(value);
        }
示例#5
0
        private static void CommitNewPhase1 <T>(SqlDatabaseContext sqlDatabaseContext, HashSet <DatabaseTransactionContextAcquisition> acquisitions, ObjectsByIdCache <T> cache, TransactionContext transactionContext, Dictionary <TypeAndTransactionalCommandsContext, InsertResults> insertResultsByType, Dictionary <TypeAndTransactionalCommandsContext, IReadOnlyList <DataAccessObject> > fixups)
        {
            var acquisition = transactionContext.AcquirePersistenceTransactionContext(sqlDatabaseContext);

            acquisitions.Add(acquisition);

            var persistenceTransactionContext = acquisition.SqlDatabaseCommandsContext;

            foreach (var j in cache.newObjects)
            {
                var key = new TypeAndTransactionalCommandsContext(j.Key, persistenceTransactionContext);

                var currentInsertResults = persistenceTransactionContext.Insert(j.Key, j.Value.Values);

                if (currentInsertResults.ToRetry.Count > 0)
                {
                    insertResultsByType[key] = currentInsertResults;
                }

                if (currentInsertResults.ToFixUp.Count > 0)
                {
                    fixups[key] = currentInsertResults.ToFixUp;
                }
            }
        }