/// <summary>
        /// Returns the entity's query.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <returns>The entity's query.</returns>
        protected override IQueryable <TEntity> AsQueryable <TEntity>()
        {
            var entityType = typeof(TEntity);
            var store      = InMemoryCache.Instance.GetDatabaseStore(DatabaseName);

            if (!store.ContainsKey(entityType))
            {
                return(Enumerable.Empty <TEntity>().AsQueryable());
            }

            var context = store[entityType];
            var query   = context
                          .Select(x => (TEntity)Convert.ChangeType(CloneableHelper.DeepCopy(x.Value), entityType))
                          .AsQueryable();

            return(query);
        }
        /// <summary>
        /// Saves all changes made in this context to the database.
        /// </summary>
        /// <returns>The number of state entries written to the database.</returns>
        public override int SaveChanges()
        {
            var store = InMemoryCache.Instance.GetDatabaseStore(DatabaseName);
            var count = 0;

            try
            {
                while (_items.TryTake(out var entitySet))
                {
                    var entityType = entitySet.Entity.GetType();
                    var key        = GetPrimaryKeyValue(entitySet.Entity);

                    if (!store.ContainsKey(entityType))
                    {
                        store[entityType] = new ConcurrentDictionary <object, object>();
                    }

                    var context = store[entityType];

                    if (entitySet.State == EntityState.Added)
                    {
                        if (context.ContainsKey(key))
                        {
                            throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture,
                                                                              Resources.EntityAlreadyBeingTrackedInStore, entitySet.Entity.GetType()));
                        }

                        var primaryKeyPropertyInfo = Conventions.GetPrimaryKeyPropertyInfos(entityType).First();

                        if (Conventions.IsColumnIdentity(primaryKeyPropertyInfo))
                        {
                            key = GeneratePrimaryKey(entityType);

                            primaryKeyPropertyInfo.SetValue(entitySet.Entity, key);
                        }
                    }
                    else if (!context.ContainsKey(key))
                    {
                        throw new InvalidOperationException(Resources.EntityNotFoundInStore);
                    }

                    if (entitySet.State == EntityState.Removed)
                    {
                        context.TryRemove(key, out _);
                    }
                    else
                    {
                        context[key] = CloneableHelper.DeepCopy(entitySet.Entity);
                    }

                    count++;
                }
            }
            finally
            {
                // Clears the collection
                while (_items.Count > 0)
                {
                    _items.TryTake(out _);
                }
            }

            return(count);
        }
Exemplo n.º 3
0
 private static object Clone(object entity)
 {
     return(CloneableHelper.DeepCopy(entity));
 }