Example #1
0
        protected internal DataAccessObject Inflate(DataAccessObject dataAccessObject)
        {
            if (dataAccessObject == null)
            {
                throw new ArgumentNullException(nameof(dataAccessObject));
            }

            Func <DataAccessObject, DataAccessObject> func;
            var definitionTypeHandle = dataAccessObject.GetAdvanced().DefinitionType.TypeHandle;

            if (!this.inflateFuncsByType.TryGetValue(definitionTypeHandle, out func))
            {
                var definitionType = Type.GetTypeFromHandle(definitionTypeHandle);
                var parameter      = Expression.Parameter(typeof(IDataAccessObjectAdvanced), "dataAccessObject");
                var methodInfo     = MethodInfoFastRef.DataAccessModelGenericInflateHelperMethod.MakeGenericMethod(definitionType);
                var body           = Expression.Call(Expression.Constant(this), methodInfo, Expression.Convert(parameter, definitionType));

                var lambda = Expression.Lambda <Func <DataAccessObject, DataAccessObject> >(body, parameter);

                func = lambda.Compile();

                this.inflateFuncsByType = this.inflateFuncsByType.Clone(definitionTypeHandle, func);
            }

            return(func(dataAccessObject));
        }
        public virtual DataAccessObject EvictObject(DataAccessObject value)
        {
            var typeHandle = Type.GetTypeHandle(value);

            if ((value.GetAdvanced().ObjectState & DataAccessObjectState.Untracked) == DataAccessObjectState.Untracked)
            {
                return(value);
            }

            if (!this.cachesByType.TryGetValue(typeHandle, out var cache))
            {
                if (this.isCommiting)
                {
                    Logger.Debug($"Skipping eviction of object {value.GetType()} because commit in process");

                    return(value);
                }

                cache = CreateCacheForDataAccessObject(value, this);

                this.cachesByType[typeHandle] = cache;
            }

            cache.Evict(value);

            return(value);
        }
Example #3
0
        protected internal Task <DataAccessObject> InflateAsync(DataAccessObject dataAccessObject, CancellationToken cancellationToken)
        {
            if (dataAccessObject == null)
            {
                throw new ArgumentNullException(nameof(dataAccessObject));
            }

            Func <DataAccessObject, CancellationToken, Task <DataAccessObject> > func;
            var definitionTypeHandle = dataAccessObject.GetAdvanced().DefinitionType.TypeHandle;

            if (!this.inflateAsyncFuncsByType.TryGetValue(definitionTypeHandle, out func))
            {
                var definitionType             = Type.GetTypeFromHandle(definitionTypeHandle);
                var parameter                  = Expression.Parameter(typeof(IDataAccessObjectAdvanced), "dataAccessObject");
                var cancellationTokenParameter = Expression.Parameter(typeof(CancellationToken), "cancellationToken");
                var methodInfo                 = MethodInfoFastRef.DataAccessModelGenericInflateAsyncHelperMethod.MakeGenericMethod(definitionType);
                var body = Expression.Call(Expression.Constant(this), methodInfo, Expression.Convert(parameter, definitionType), cancellationTokenParameter);

                var lambda = Expression.Lambda <Func <DataAccessObject, CancellationToken, Task <DataAccessObject> > >(body, parameter, cancellationTokenParameter);

                func = lambda.Compile();

                var newDictionary = new Dictionary <RuntimeTypeHandle, Func <DataAccessObject, CancellationToken, Task <DataAccessObject> > >(this.inflateAsyncFuncsByType)
                {
                    [definitionTypeHandle] = func
                };

                this.inflateAsyncFuncsByType = newDictionary;
            }

            return(func(dataAccessObject, cancellationToken));
        }
Example #4
0
        protected internal DataAccessObject Inflate(DataAccessObject dataAccessObject)
        {
            if (dataAccessObject == null)
            {
                throw new ArgumentNullException("dataAccessObject");
            }

            Func <DataAccessObject, DataAccessObject> func;
            var definitionType = dataAccessObject.GetAdvanced().DefinitionType;

            if (!inflateFuncsByType.TryGetValue(definitionType, out func))
            {
                var parameter  = Expression.Parameter(typeof(IDataAccessObjectAdvanced), "dataAccessObject");
                var methodInfo = MethodInfoFastRef.BaseDataAccessModelGenericInflateMethod.MakeGenericMethod(definitionType);
                var body       = Expression.Call(Expression.Constant(this), methodInfo, Expression.Convert(parameter, definitionType));

                var lambda = Expression.Lambda <Func <DataAccessObject, DataAccessObject> >(body, parameter);

                func = lambda.Compile();

                var newDictionary = new Dictionary <Type, Func <DataAccessObject, DataAccessObject> >(inflateFuncsByType);

                newDictionary[definitionType] = func;

                inflateFuncsByType = newDictionary;
            }

            return(func(dataAccessObject));
        }
 Task IDataAccessModelInternal.OnHookCreateAsync(DataAccessObject dataAccessObject, CancellationToken cancellationToken)
 {
     return(CallHooksAsync(hook =>
     {
         var task = hook.CreateAsync(dataAccessObject, cancellationToken);
         task.ConfigureAwait(false);
         return task;
     }));
 }
        public virtual void ImportObject(DataAccessObject value)
        {
            if (value == null)
            {
                return;
            }

            this.ImportObject(new HashSet <DataAccessObject>(), value);
        }
Example #7
0
        Task IDataAccessModelInternal.OnHookReadAsync(DataAccessObject dataAccessObject, CancellationToken cancellationToken)
        {
            var localHooks = this.hooks;
            var tasks      = new List <Task>();

            if (localHooks != null)
            {
                tasks.AddRange(localHooks.Select(hook => hook.ReadAsync(dataAccessObject, cancellationToken)));
            }

            return(Task.WhenAll(tasks));
        }
Example #8
0
        void IDataAccessModelInternal.OnHookCreate(DataAccessObject obj)
        {
            var localHooks = this.hooks;

            if (localHooks != null)
            {
                foreach (var hook in localHooks)
                {
                    hook.Create(obj);
                }
            }
        }
Example #9
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);
        }
        protected void ImportObject(HashSet <DataAccessObject> alreadyVisited, DataAccessObject value)
        {
            CacheObject(value, true);

            alreadyVisited.Add(value);

            foreach (var propertyInfoAndValue in value.GetAdvanced().GetAllProperties())
            {
                if (propertyInfoAndValue.Value is DataAccessObject propertyValue && !alreadyVisited.Contains(propertyValue))
                {
                    alreadyVisited.Add(propertyValue);

                    ImportObject(alreadyVisited, propertyValue);
                }
            }
        }
Example #11
0
        public virtual DataAccessObject CacheObject(DataAccessObject value, bool forImport)
        {
            var typeHandle = Type.GetTypeHandle(value);

            if ((value.GetAdvanced().ObjectState & DataAccessObjectState.Untracked) == DataAccessObjectState.Untracked)
            {
                return(value);
            }

            if (!this.cachesByType.TryGetValue(typeHandle, out var cache))
            {
                cache = CreateCacheForDao(value, this);

                this.cachesByType[typeHandle] = cache;
            }

            return(cache.Cache(value, forImport));
        }
Example #12
0
        Task IDataAccessModelInternal.OnHookCreateAsync(DataAccessObject dataAccessObject, CancellationToken cancellationToken)
        {
            var localHooks = this.hooks;
            var tasks      = new List <Task>();

            if (localHooks != null)
            {
                foreach (var hook in localHooks)
                {
                    var task = hook.CreateAsync(dataAccessObject, cancellationToken);

                    task.ConfigureAwait(false);
                    tasks.Add(task);
                }
            }

            return(Task.WhenAll(tasks));
        }
Example #13
0
        public void Evict(DataAccessObject value)
        {
            if (this.dataAccessObjectDataContext.isCommiting)
            {
                Logger.Debug("Skipping eviction of object {value.GetType()} because commit in process");

                return;
            }

            if (value.GetAdvanced().IsNew)
            {
                if (value.GetAdvanced().PrimaryKeyIsCommitReady)
                {
                    this.newObjects.Remove(value);
                }
                else
                {
                    this.objectsNotReadyForCommit?.Remove(value);
                }

                return;
            }

            var advanced  = value.GetAdvanced();
            var predicate = advanced?.DeflatedPredicate;

            if (predicate != null)
            {
                this.objectsByPredicateCache?.Remove(predicate);

                return;
            }

            if (value.GetAdvanced().IsMissingAnyDirectOrIndirectServerSideGeneratedPrimaryKeys)
            {
                return;
            }

            var id = this.getIdFunc(value);

            this.objectsDeleted?.Remove(id);
            this.objectsByIdCache.Remove(id);
        }
        public virtual DataAccessObject EvictObject(DataAccessObject value)
        {
            var typeHandle = Type.GetTypeHandle(value);

            if ((value.GetAdvanced().ObjectState & DataAccessObjectState.Untracked) == DataAccessObjectState.Untracked)
            {
                return(value);
            }

            if (!this.cachesByType.TryGetValue(typeHandle, out var cache))
            {
                cache = CreateCacheForDataAccessObject(value, this);

                this.cachesByType[typeHandle] = cache;
            }

            cache.Evict(value);

            return(value);
        }
Example #15
0
        public void Evict(DataAccessObject value)
        {
            if (this.dataAccessObjectDataContext.isCommiting)
            {
                return;
            }

            if (value.GetAdvanced().IsNew)
            {
                if (value.GetAdvanced().PrimaryKeyIsCommitReady)
                {
                    this.newObjects.Remove(value);
                }
                else
                {
                    this.objectsNotReadyForCommit?.Remove(value);
                }

                return;
            }

            var internalDao = value.ToObjectInternal();
            var predicate   = internalDao?.DeflatedPredicate;

            if (predicate != null)
            {
                this.objectsByPredicateCache?.Remove(predicate);

                return;
            }

            if (value.GetAdvanced().IsMissingAnyDirectOrIndirectServerSideGeneratedPrimaryKeys)
            {
                return;
            }

            var id = this.getIdFunc(value);

            this.objectsDeleted?.Remove(id);
            this.objectsByIdCache.Remove(id);
        }
Example #16
0
        public void Deleted(DataAccessObject value)
        {
            if (((IDataAccessObjectAdvanced)value).IsNew)
            {
                this.newObjects.Remove(value);
                this.objectsNotReadyForCommit?.Remove(value);
            }
            else
            {
                var id = this.getIdFunc(value);

                this.objectsByIdCache.Remove(id);
                (this.objectsDeleted = this.objectsDeleted ?? new Dictionary <K, DataAccessObject>())[id] = value;

                var advanced = value.GetAdvanced();

                if (advanced?.DeflatedPredicate != null)
                {
                    this.objectsByPredicateCache?.Remove(advanced.DeflatedPredicate);
                }
            }
        }
            public void Deleted(DataAccessObject value)
            {
                if (((IDataAccessObjectAdvanced)value).IsNew)
                {
                    this.newObjects.Remove(value);
                    this.objectsNotReadyForCommit.Remove(value);
                }
                else
                {
                    var id = this.getIdFunc(value);

                    this.objectsByIdCache.Remove(id);
                    this.objectsDeleted[id] = value;

                    var internalDao = (IDataAccessObjectInternal)value;

                    if (internalDao?.DeflatedPredicate != null)
                    {
                        this.objectsByPredicateCache.Remove(internalDao.DeflatedPredicate);
                    }
                }
            }
Example #18
0
        protected void ImportObject(HashSet <DataAccessObject> alreadyVisited, DataAccessObject value)
        {
            if (this.DisableCache)
            {
                return;
            }

            this.CacheObject(value, true);

            alreadyVisited.Add(value);

            foreach (var propertyInfoAndValue in value.GetAdvanced().GetAllProperties())
            {
                var propertyValue = propertyInfoAndValue.Value as DataAccessObject;

                if (propertyValue != null && !alreadyVisited.Contains(propertyValue))
                {
                    alreadyVisited.Add(propertyValue);

                    this.ImportObject(alreadyVisited, propertyValue);
                }
            }
        }
 private static CompositePrimaryKey GetDataAccessObjectCompositeId(DataAccessObject dataAccessObject)
 {
     return(new CompositePrimaryKey(dataAccessObject.GetAdvanced().GetPrimaryKeys()));
 }
Example #20
0
        public DataAccessObject Cache(DataAccessObject value, bool forImport)
        {
            if (value.GetAdvanced().IsNew)
            {
                if (value.GetAdvanced().PrimaryKeyIsCommitReady)
                {
                    if (this.newObjects.TryGetValue(value, out var result))
                    {
                        if (result != value)
                        {
                            throw new ObjectAlreadyExistsException(value, null, null);
                        }
                    }

                    if (this.dataAccessObjectDataContext.isCommiting)
                    {
                        Logger.Debug("Skipping caching of new object {value.GetType()} because commit in process");

                        return(value);
                    }

                    this.newObjects[value] = value;

                    this.objectsNotReadyForCommit?.Remove(value);

                    if (value.GetAdvanced().NumberOfPrimaryKeysGeneratedOnServerSide > 0)
                    {
                        return(value);
                    }
                }
                else
                {
                    if (!(this.objectsNotReadyForCommit?.Contains(value) ?? false))
                    {
                        if (this.dataAccessObjectDataContext.isCommiting)
                        {
                            Logger.Debug("Skipping caching of not ready for commit object {value.GetType()} because commit in process");

                            return(value);
                        }

                        (this.objectsNotReadyForCommit = this.objectsNotReadyForCommit ?? new HashSet <DataAccessObject>(ObjectReferenceIdentityEqualityComparer <IDataAccessObjectAdvanced> .Default))
                        .Add(value);
                    }

                    return(value);
                }
            }

            var advanced  = value.GetAdvanced();
            var predicate = advanced?.DeflatedPredicate;

            if (predicate != null)
            {
                if (this.objectsByPredicateCache != null)
                {
                    if (this.objectsByPredicateCache.TryGetValue(predicate, out var existing))
                    {
                        existing.ToObjectInternal().SwapData(value, true);

                        return(existing);
                    }
                }

                if (this.dataAccessObjectDataContext.isCommiting)
                {
                    Logger.Debug("Skipping caching of predicated deflated object {value.GetType()} because commit in process");

                    return(value);
                }

                (this.objectsByPredicateCache = this.objectsByPredicateCache ?? new Dictionary <LambdaExpression, DataAccessObject>())[predicate] = value;

                return(value);
            }

            if (value.GetAdvanced().IsMissingAnyDirectOrIndirectServerSideGeneratedPrimaryKeys)
            {
                return(value);
            }

            var id = this.getIdFunc(value);

            if (!forImport)
            {
                if (this.objectsByIdCache.TryGetValue(id, out var outValue))
                {
                    var deleted = outValue.IsDeleted();

                    outValue.ToObjectInternal().SwapData(value, true);

                    if (deleted)
                    {
                        outValue.ToObjectInternal().SetIsDeleted(true);
                    }

                    return(outValue);
                }
            }

            if (this.objectsDeleted != null)
            {
                if (this.objectsDeleted.TryGetValue(id, out var existingDeleted))
                {
                    if (!forImport)
                    {
                        existingDeleted.ToObjectInternal().SwapData(value, true);
                        existingDeleted.ToObjectInternal().SetIsDeleted(true);

                        return(existingDeleted);
                    }
                    else
                    {
                        if (value.IsDeleted())
                        {
                            this.objectsDeleted[id] = value;
                        }
                        else
                        {
                            this.objectsDeleted.Remove(id);
                            this.objectsByIdCache[id] = value;
                        }

                        return(value);
                    }
                }
            }

            if (this.dataAccessObjectDataContext.isCommiting)
            {
                Logger.Debug("Skipping caching of object {value.GetType()} because commit in process");

                return(value);
            }

            this.objectsByIdCache[id] = value;

            return(value);
        }
 public virtual void Read(DataAccessObject dataAccessObject)
 {
 }
Example #22
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);
        }
 public virtual void Create(DataAccessObject dataAccessObject)
 {
 }
Example #24
0
            public DataAccessObject <T> Cache(DataAccessObject <T> value, bool forImport)
            {
                if (this.dataAccessObjectDataContext.isCommiting)
                {
                    return(value);
                }

                var dataAccessObject = (DataAccessObject)value;

                var type = value.GetType();

                if (dataAccessObject.GetAdvanced().IsNew)
                {
                    HashSet <DataAccessObject> notReadyForCommitSubcache;

                    if (dataAccessObject.GetAdvanced().PrimaryKeyIsCommitReady)
                    {
                        Dictionary <DataAccessObject, DataAccessObject> subcache;

                        if (!this.newObjects.TryGetValue(type, out subcache))
                        {
                            subcache = new Dictionary <DataAccessObject, DataAccessObject>(DataAccessObjectServerSidePropertiesAccountingComparer.Default);

                            this.newObjects[type] = subcache;
                        }

                        DataAccessObject result;

                        if (subcache.TryGetValue(value, out result))
                        {
                            if (result != value)
                            {
                                throw new ObjectAlreadyExistsException(value, null, null);
                            }
                        }

                        subcache[value] = value;

                        if (this.objectsNotReadyForCommit.TryGetValue(type, out notReadyForCommitSubcache))
                        {
                            notReadyForCommitSubcache.Remove(value);
                        }

                        if (dataAccessObject.GetAdvanced().NumberOfPrimaryKeysGeneratedOnServerSide > 0)
                        {
                            return(value);
                        }
                    }
                    else
                    {
                        if (!this.objectsNotReadyForCommit.TryGetValue(type, out notReadyForCommitSubcache))
                        {
                            notReadyForCommitSubcache = new HashSet <DataAccessObject>(ObjectReferenceIdentityEqualityComparer <IDataAccessObjectAdvanced> .Default);

                            this.objectsNotReadyForCommit[type] = notReadyForCommitSubcache;
                        }

                        if (!notReadyForCommitSubcache.Contains(value))
                        {
                            notReadyForCommitSubcache.Add(value);
                        }

                        return(value);
                    }
                }

                if (dataAccessObject.GetAdvanced().IsMissingAnyDirectOrIndirectServerSideGeneratedPrimaryKeys)
                {
                    return(value);
                }

                if (dataAccessObject.GetAdvanced().NumberOfPrimaryKeys > 1)
                {
                    Dictionary <CompositePrimaryKey, DataAccessObject> subcache;

                    var key = new CompositePrimaryKey(value.GetAdvanced().GetPrimaryKeys());

                    if (this.objectsByIdCacheComposite == null)
                    {
                        this.objectsByIdCacheComposite = new Dictionary <Type, Dictionary <CompositePrimaryKey, DataAccessObject> >(PrimeNumbers.Prime127);
                    }

                    if (!this.objectsByIdCacheComposite.TryGetValue(type, out subcache))
                    {
                        subcache = new Dictionary <CompositePrimaryKey, DataAccessObject>(PrimeNumbers.Prime127, CompositePrimaryKeyComparer.Default);

                        this.objectsByIdCacheComposite[type] = subcache;
                    }

                    if (!forImport)
                    {
                        DataAccessObject outValue;

                        if (subcache.TryGetValue(key, out outValue))
                        {
                            var deleted = outValue.IsDeleted();

                            outValue.ToObjectInternal().SwapData(value, true);

                            if (deleted)
                            {
                                outValue.ToObjectInternal().SetIsDeleted(true);
                            }

                            return((DataAccessObject <T>)outValue);
                        }
                    }

                    if (this.objectsDeletedComposite != null)
                    {
                        Dictionary <CompositePrimaryKey, DataAccessObject> subList;

                        if (this.objectsDeletedComposite.TryGetValue(type, out subList))
                        {
                            DataAccessObject existingDeleted;

                            if (subList.TryGetValue(key, out existingDeleted))
                            {
                                if (!forImport)
                                {
                                    existingDeleted.ToObjectInternal().SwapData(value, true);
                                    existingDeleted.ToObjectInternal().SetIsDeleted(true);

                                    return((DataAccessObject <T>)existingDeleted);
                                }
                                else
                                {
                                    if (value.IsDeleted())
                                    {
                                        subList[key] = value;
                                    }
                                    else
                                    {
                                        subList.Remove(key);
                                        subcache[key] = value;
                                    }

                                    return(value);
                                }
                            }
                        }
                    }

                    subcache[key] = value;

                    return(value);
                }
                else
                {
                    var id = value.Id;
                    Dictionary <T, DataAccessObject> subcache;

                    if (!this.objectsByIdCache.TryGetValue(type, out subcache))
                    {
                        subcache = new Dictionary <T, DataAccessObject>(PrimeNumbers.Prime127);

                        this.objectsByIdCache[type] = subcache;
                    }

                    if (!forImport)
                    {
                        DataAccessObject outValue;

                        if (subcache.TryGetValue(id, out outValue))
                        {
                            var deleted = outValue.IsDeleted();

                            outValue.ToObjectInternal().SwapData(value, true);

                            if (deleted)
                            {
                                outValue.ToObjectInternal().SetIsDeleted(true);
                            }

                            return((DataAccessObject <T>)outValue);
                        }
                    }

                    if (this.objectsDeleted != null)
                    {
                        Dictionary <T, DataAccessObject> subList;

                        if (this.objectsDeleted.TryGetValue(type, out subList))
                        {
                            DataAccessObject existingDeleted;

                            if (subList.TryGetValue(id, out existingDeleted))
                            {
                                if (!forImport)
                                {
                                    existingDeleted.ToObjectInternal().SwapData(value, true);
                                    existingDeleted.ToObjectInternal().SetIsDeleted(true);

                                    return((DataAccessObject <T>)existingDeleted);
                                }
                                else
                                {
                                    if (value.IsDeleted())
                                    {
                                        subList[id] = value;
                                    }
                                    else
                                    {
                                        subList.Remove(id);
                                        subcache[id] = value;
                                    }

                                    return(value);
                                }
                            }
                        }
                    }

                    subcache[value.Id] = value;

                    return(value);
                }
            }
            public DataAccessObject Cache(DataAccessObject value, bool forImport)
            {
                if (this.dataAccessObjectDataContext.isCommiting)
                {
                    return(value);
                }

                if (value.GetAdvanced().IsNew)
                {
                    if (value.GetAdvanced().PrimaryKeyIsCommitReady)
                    {
                        DataAccessObject result;

                        if (this.newObjects.TryGetValue(value, out result))
                        {
                            if (result != value)
                            {
                                throw new ObjectAlreadyExistsException(value, null, null);
                            }
                        }

                        this.newObjects[value] = value;

                        this.objectsNotReadyForCommit.Remove(value);

                        if (value.GetAdvanced().NumberOfPrimaryKeysGeneratedOnServerSide > 0)
                        {
                            return(value);
                        }
                    }
                    else
                    {
                        if (!this.objectsNotReadyForCommit.Contains(value))
                        {
                            this.objectsNotReadyForCommit.Add(value);
                        }

                        return(value);
                    }
                }

                var internalDao = value.ToObjectInternal();
                var predicate   = internalDao?.DeflatedPredicate;

                if (predicate != null)
                {
                    if (forImport)
                    {
                        throw new InvalidOperationException("Cannot import predicated deflated object");
                    }

                    DataAccessObject existing;

                    if (this.objectsByPredicateCache.TryGetValue(predicate, out existing))
                    {
                        existing.ToObjectInternal().SwapData(value, true);

                        return(existing);
                    }

                    this.objectsByPredicateCache[predicate] = value;

                    return(value);
                }

                if (value.GetAdvanced().IsMissingAnyDirectOrIndirectServerSideGeneratedPrimaryKeys)
                {
                    return(value);
                }

                var id = this.getIdFunc(value);

                if (!forImport)
                {
                    DataAccessObject outValue;

                    if (this.objectsByIdCache.TryGetValue(id, out outValue))
                    {
                        var deleted = outValue.IsDeleted();

                        outValue.ToObjectInternal().SwapData(value, true);

                        if (deleted)
                        {
                            outValue.ToObjectInternal().SetIsDeleted(true);
                        }

                        return(outValue);
                    }
                }

                if (this.objectsDeleted != null)
                {
                    DataAccessObject existingDeleted;

                    if (this.objectsDeleted.TryGetValue(id, out existingDeleted))
                    {
                        if (!forImport)
                        {
                            existingDeleted.ToObjectInternal().SwapData(value, true);
                            existingDeleted.ToObjectInternal().SetIsDeleted(true);

                            return(existingDeleted);
                        }
                        else
                        {
                            if (value.IsDeleted())
                            {
                                this.objectsDeleted[id] = value;
                            }
                            else
                            {
                                this.objectsDeleted.Remove(id);
                                this.objectsByIdCache[id] = value;
                            }

                            return(value);
                        }
                    }
                }

                this.objectsByIdCache[id] = value;

                return(value);
            }
Example #26
0
            public void Deleted(DataAccessObject <T> value)
            {
                var type = value.GetType();

                if (((IDataAccessObjectAdvanced)value).IsNew)
                {
                    HashSet <DataAccessObject> notReadyForCommitSubcache;
                    Dictionary <DataAccessObject, DataAccessObject> subcache;

                    if (this.newObjects.TryGetValue(type, out subcache))
                    {
                        subcache.Remove(value);
                    }

                    if (this.objectsNotReadyForCommit.TryGetValue(type, out notReadyForCommitSubcache))
                    {
                        notReadyForCommitSubcache.Remove(value);
                    }
                }
                else
                {
                    if (((IDataAccessObjectAdvanced)value).NumberOfPrimaryKeys > 1)
                    {
                        Dictionary <CompositePrimaryKey, DataAccessObject> subcache;
                        var key = new CompositePrimaryKey(value.GetAdvanced().GetPrimaryKeys());

                        if (this.objectsByIdCacheComposite == null)
                        {
                            return;
                        }

                        if (!this.objectsByIdCacheComposite.TryGetValue(type, out subcache))
                        {
                            return;
                        }

                        subcache.Remove(key);

                        Dictionary <CompositePrimaryKey, DataAccessObject> subList;

                        if (this.objectsDeletedComposite == null)
                        {
                            this.objectsDeletedComposite = new Dictionary <Type, Dictionary <CompositePrimaryKey, DataAccessObject> >(PrimeNumbers.Prime67);
                        }

                        if (!this.objectsDeletedComposite.TryGetValue(type, out subList))
                        {
                            subList = new Dictionary <CompositePrimaryKey, DataAccessObject>(PrimeNumbers.Prime67, CompositePrimaryKeyComparer.Default);

                            this.objectsDeletedComposite[type] = subList;
                        }

                        subList[key] = value;
                    }
                    else
                    {
                        Dictionary <T, DataAccessObject> subcache;

                        if (!this.objectsByIdCache.TryGetValue(type, out subcache))
                        {
                            return;
                        }

                        subcache.Remove(value.Id);

                        Dictionary <T, DataAccessObject> subList;

                        if (this.objectsDeleted == null)
                        {
                            this.objectsDeleted = new Dictionary <Type, Dictionary <T, DataAccessObject> >(PrimeNumbers.Prime67);
                        }

                        if (!this.objectsDeleted.TryGetValue(type, out subList))
                        {
                            subList = new Dictionary <T, DataAccessObject>(PrimeNumbers.Prime127);

                            this.objectsDeleted[type] = subList;
                        }

                        subList[value.Id] = value;
                    }
                }
            }
Example #27
0
 Task IDataAccessModelInternal.OnHookCreateAsync(DataAccessObject dataAccessObject)
 {
     return(((IDataAccessModelInternal)this).OnHookCreateAsync(dataAccessObject, CancellationToken.None));
 }
 void IDataAccessModelInternal.OnHookCreate(DataAccessObject obj)
 {
     CallHooks(hook => hook.Create(obj));
 }
 internal static IDataAccessObjectInternal ToObjectInternal(this DataAccessObject value)
 {
     // ReSharper disable SuspiciousTypeConversion.Global
     return((IDataAccessObjectInternal)value);
     // ReSharper restore SuspiciousTypeConversion.Global
 }
Example #30
0
 DataAccessObject IObjectsByIdCache.Cache(DataAccessObject value, bool forImport)
 {
     return(this.Cache((DataAccessObject <T>)value, forImport));
 }