public virtual void Populate()
        {
            var localRequest = new Object();

            lock (currentRequestLock)
            {
                currentRequest = localRequest;
            }
            if (GuiThreadHelper.IsInGuiThread())
            {
                IFilterDescriptor       filterDescriptor = GetFilterDescriptor();
                IList <ISortDescriptor> sortDescriptors  = GetSortDescriptors();
                IPagingRequest          pagingRequest    = GetPagingRequest();
                Object[] contextInformation = GetContextInformation();

                Model.IsBusy = true;
                ThreadPool.Queue((GenericViewModel <T> model) =>
                {
                    CacheContext.ExecuteWithCache <Object>(CacheProvider.GetCurrentCache(), delegate()
                    {
                        ConfigureCacheWithEagerLoads(Cache);
                        PopulateAsync(model, filterDescriptor, sortDescriptors, pagingRequest, contextInformation, Cache, localRequest);
                        return(null);
                    });
                }, Model);
            }
            else
            {
                IFilterDescriptor       filterDescriptor = null;
                IList <ISortDescriptor> sortDescriptors  = null;
                IPagingRequest          pagingRequest    = null;
                Object[] contextInformation = null;
                GuiThreadHelper.InvokeInGuiAndWait(delegate()
                {
                    filterDescriptor = GetFilterDescriptor();
                    sortDescriptors  = GetSortDescriptors();
                    pagingRequest    = GetPagingRequest();

                    contextInformation = GetContextInformation();

                    Model.IsBusy = true;
                });
                CacheContext.ExecuteWithCache <Object>(CacheProvider.GetCurrentCache(), delegate()
                {
                    ConfigureCacheWithEagerLoads(Cache);
                    PopulateAsync(Model, filterDescriptor, sortDescriptors, pagingRequest, contextInformation, Cache, localRequest);
                    return(null);
                });
            }
        }
Esempio n. 2
0
        public ICUDResult ApplyCUDResultOnEntitiesOfCache(ICUDResult cudResult, bool checkBaseState, IIncrementalMergeState incrementalState)
        {
            ICache cache = incrementalState.GetStateCache().CurrentCache;

            if (cache.CurrentCache == cache)
            {
                // given cache is already the current cache
                return(ApplyIntern(cudResult, checkBaseState, (IncrementalMergeState)incrementalState));
            }
            return(CacheContext.ExecuteWithCache(cache, new IResultingBackgroundWorkerParamDelegate <ICUDResult, ICUDResult>(delegate(ICUDResult state)
            {
                return ApplyIntern(state, checkBaseState, (IncrementalMergeState)incrementalState);
            }), cudResult));
        }
Esempio n. 3
0
        protected override void InterceptIntern(IInvocation invocation)
        {
            MethodInfo method = invocation.Method;

            if (method.DeclaringType.Equals(typeof(Object)))
            {
                InvokeTarget(invocation);
                return;
            }
            Object result = CacheContext.ExecuteWithCache(CacheProvider, delegate()
            {
                InvokeTarget(invocation);
                return(invocation.ReturnValue);
            });
        }
        public virtual void DataChanged(IDataChange dataChange, DateTime dispatchTime, long sequenceId)
        {
            dataChange = dataChange.Derive(InterestedEntityTypes);
            if (dataChange.IsEmpty)
            {
                return;
            }
            ISet <Object> directObjectsToDelete = null;

            ISet <Type> requestedTypes = new HashSet <Type>();
            IDictionary <Type, IEntityMetaData> typeToMetaDataDict = new Dictionary <Type, IEntityMetaData>();

            GuiThreadHelper.InvokeInGuiAndWait(delegate()
            {
                IList <T> entities = Model.Objects;

                for (int i = entities.Count; i-- > 0;)
                {
                    Object entity = entities[i];

                    requestedTypes.Add(entity.GetType());
                }
            });

            IList <IDataChangeEntry> dataChangeEntries = dataChange.Inserts;

            for (int a = dataChangeEntries.Count; a-- > 0;)
            {
                requestedTypes.Add(dataChangeEntries[a].EntityType);
            }
            dataChangeEntries = dataChange.Updates;
            for (int a = dataChangeEntries.Count; a-- > 0;)
            {
                requestedTypes.Add(dataChangeEntries[a].EntityType);
            }
            dataChangeEntries = dataChange.Deletes;
            for (int a = dataChangeEntries.Count; a-- > 0;)
            {
                requestedTypes.Add(dataChangeEntries[a].EntityType);
            }

            IList <IEntityMetaData> metaDatas = EntityMetaDataProvider.GetMetaData(ListUtil.ToList(requestedTypes));

            foreach (IEntityMetaData metaData in metaDatas)
            {
                typeToMetaDataDict[metaData.EntityType] = metaData;
            }

            bool consistsOnlyOfDirectDeletes = false;

            if (dataChange.Deletes.Count > 0)
            {
                consistsOnlyOfDirectDeletes = true;
                foreach (IDataChangeEntry deleteEntry in dataChange.Deletes)
                {
                    if (deleteEntry is DirectDataChangeEntry)
                    {
                        if (directObjectsToDelete == null)
                        {
                            directObjectsToDelete = new IdentityHashSet <Object>();
                        }
                        directObjectsToDelete.Add(((DirectDataChangeEntry)deleteEntry).Entry);
                    }
                    else
                    {
                        consistsOnlyOfDirectDeletes = false;
                    }
                }
            }

            IList <T> interestingEntities = null;

            Object[]                contextInformation = GetContextInformation();
            IFilterDescriptor       filterDescriptor   = GetFilterDescriptor();
            IList <ISortDescriptor> sortDescriptors    = GetSortDescriptors();
            IPagingRequest          pagingRequest      = GetPagingRequest();

            IPagingResponse         pagingResponse  = null;
            List <IDataChangeEntry> modifiedEntries = new List <IDataChangeEntry>();

            modifiedEntries.AddRange(dataChange.All);

            if (!consistsOnlyOfDirectDeletes)
            {
                interestingEntities = CacheContext.ExecuteWithCache(CacheProvider.GetCurrentCache(), delegate()
                {
                    ConfigureCacheWithEagerLoads(Cache);
                    if (Refresher is IPagingRefresher <T> )
                    {
                        interestingEntities = new List <T>();
                        pagingResponse      = ((IPagingRefresher <T>)Refresher).Refresh(modifiedEntries, filterDescriptor, sortDescriptors, pagingRequest, contextInformation);
                        foreach (Object obj in pagingResponse.Result)
                        {
                            interestingEntities.Add((T)obj);
                        }
                        return(interestingEntities);
                    }
                    else
                    {
                        if (filterDescriptor != null || sortDescriptors != null)
                        {
                            contextInformation    = new Object[2];
                            contextInformation[0] = filterDescriptor;
                            contextInformation[1] = sortDescriptors;
                        }

                        return(((IRefresher <T>)Refresher).Refresh(modifiedEntries, contextInformation));
                    }
                });
            }
            GuiThreadHelper.InvokeInGuiAndWait(delegate()
            {
                IList <T> entities = Model.Objects;

                ISet <T> entitiesToAdd                           = null;
                ISet <T> entitiesToRemove                        = null;
                IDictionary <T, T> entitiesToReplace             = null;
                IDictionary <IObjRef, T> oldObjRefToOldEntityMap = null;
                bool mergeModel = false;

                if (interestingEntities != null && interestingEntities.Count > 0)
                {
                    entitiesToAdd           = new IdentityHashSet <T>(interestingEntities);
                    entitiesToRemove        = new IdentityHashSet <T>(entities);
                    entitiesToReplace       = new IdentityDictionary <T, T>();
                    oldObjRefToOldEntityMap = new Dictionary <IObjRef, T>();
                    mergeModel = true;
                }
                for (int i = entities.Count; i-- > 0;)
                {
                    T oldEntity = entities[i];
                    if (directObjectsToDelete != null && directObjectsToDelete.Contains(oldEntity))
                    {
                        if (entitiesToRemove != null)
                        {
                            entitiesToRemove.Remove(oldEntity);
                        }
                        Model.RemoveAt(i);
                        continue;
                    }
                    Type oldEntityType       = ProxyHelper.GetRealType(oldEntity.GetType());
                    PrimitiveMember idMember = typeToMetaDataDict[oldEntityType].IdMember;
                    Object oldEntityId       = idMember.GetValue(oldEntity, false);
                    if (oldEntityId == null)
                    {
                        if (entitiesToRemove != null)
                        {
                            entitiesToRemove.Remove(oldEntity);
                        }
                        // Unpersisted object. This object should not be removed
                        // only because of a background DCE
                        continue;
                    }
                    bool entryRemoved = false;
                    foreach (IDataChangeEntry deleteEntry in dataChange.Deletes)
                    {
                        if (deleteEntry is DirectDataChangeEntry)
                        {
                            continue;
                        }
                        Object id = deleteEntry.Id;
                        if (!EqualsItems(oldEntityType, oldEntityId, deleteEntry.EntityType, id))
                        {
                            continue;
                        }
                        if (entitiesToRemove != null)
                        {
                            entitiesToRemove.Remove(oldEntity);
                        }
                        Model.RemoveAt(i);
                        entryRemoved = true;
                        break;
                    }
                    if (entryRemoved)
                    {
                        continue;
                    }
                    if (mergeModel)
                    {
                        IObjRef oldObjRef   = new ObjRef(oldEntityType, ObjRef.PRIMARY_KEY_INDEX, oldEntityId, null);
                        T existingOldEntity = DictionaryExtension.ValueOrDefault(oldObjRefToOldEntityMap, oldObjRef);
                        if (existingOldEntity == null)
                        {
                            oldObjRefToOldEntityMap.Add(oldObjRef, oldEntity);
                        }
                        else if (!Object.ReferenceEquals(existingOldEntity, oldEntity))
                        {
                            // Force duplicate key exception
                            oldObjRefToOldEntityMap.Add(oldObjRef, oldEntity);
                        }
                    }
                }
                if (oldObjRefToOldEntityMap != null && oldObjRefToOldEntityMap.Count > 0)
                {
                    IDictionary <IObjRef, T> newObjRefToNewEntityMap = new Dictionary <IObjRef, T>();
                    for (int a = interestingEntities.Count; a-- > 0;)
                    {
                        T newEntity              = interestingEntities[a];
                        Type newEntityType       = ProxyHelper.GetRealType(newEntity.GetType());
                        PrimitiveMember idMember = typeToMetaDataDict[newEntityType].IdMember;
                        Object newEntityId       = idMember.GetValue(newEntity, false);

                        IObjRef newObjRef = new ObjRef(newEntityType, ObjRef.PRIMARY_KEY_INDEX, newEntityId, null);
                        newObjRefToNewEntityMap.Add(newObjRef, newEntity);
                    }
                    DictionaryExtension.Loop(oldObjRefToOldEntityMap, delegate(IObjRef objRef, T oldEntity)
                    {
                        T newEntity = DictionaryExtension.ValueOrDefault(newObjRefToNewEntityMap, objRef);
                        if (newEntity == null)
                        {
                            // Nothing to do if current oldEntity has no corresponding newEntity
                            return;
                        }
                        entitiesToAdd.Remove(newEntity);
                        if (!Object.ReferenceEquals(oldEntity, newEntity) &&
                            (dataChange.IsLocalSource || !(oldEntity is IDataObject) || !((IDataObject)oldEntity).ToBeUpdated))
                        {
                            entitiesToReplace[oldEntity] = newEntity;
                        }
                        entitiesToRemove.Remove(oldEntity);
                    });
                }

                if (mergeModel)
                {
                    for (int a = entities.Count; a-- > 0;)
                    {
                        T item = entities[a];
                        if (entitiesToRemove.Contains(item))
                        {
                            Model.RemoveAt(a);
                            continue;
                        }
                        T replacingItem = DictionaryExtension.ValueOrDefault(entitiesToReplace, item);
                        if (replacingItem != null)
                        {
                            Model.Replace(a, replacingItem);
                            continue;
                        }
                    }
                    IEnumerator <T> enumerator = entitiesToAdd.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        T entityToAdd = enumerator.Current;
                        Model.Add(entityToAdd);
                    }

                    if (hasPagedViewModel)
                    {
                        UpdatePagingInformation(pagingResponse);
                    }
                    UpdateAfterDCE();
                }
            });
        }
Esempio n. 5
0
        public virtual void execute()
        {
            long start = Environment.TickCount;

            // Do something for nearly 60 seconds (job gets invoked every 60 seconds
            while (Environment.TickCount - start < 58000)
            {
                IList <TestEntity>  allTestEntities  = null;
                IList <TestEntity2> allTest2Entities = null;

                ICache newCache = CacheProvider.GetCurrentCache();

                CacheContext.ExecuteWithCache <Object>(newCache, delegate()
                {
                    allTestEntities  = HelloWorldService.GetAllTestEntities();
                    allTest2Entities = HelloWorldService.GetAllTest2Entities();
                    return(null);
                });

                if (allTestEntities.Count > 0)
                {
                    IRevertChangesSavepoint savepoint = RevertChangesHelper.CreateSavepoint(allTestEntities);
                    allTestEntities[0].MyString = "Hallo";
                    allTestEntities[0].Version  = -67;
                    allTestEntities[0].Relation = null;

                    savepoint.RevertChanges();
                }


                // Evaluate random type of change ( INSERT / UPDATE / DELETE / NOTHING)
                double randomChange = random.NextDouble();

                bool entity2Change = random.NextDouble() > 0.66;

                // Evaluate entity to change by its index in the result list of existing entities (necessary for UPDATE /
                // DELETE)
                int changeIndex = (int)(random.NextDouble() * allTestEntities.Count);

                // Evaluate entity2 to select its index in the result list of existing entities (necessary for INSERT of
                // entity1)
                int selectEntity2Index = (int)(random.NextDouble() * allTest2Entities.Count);

                // Evaluate new value to change on chosen entity (necessary for INSERT / UPDATE)
                int randomNewValue = (int)(random.NextDouble() * Int32.MaxValue / 2);

                // Map from randomChange to the enum-based operation to execute
                ChangeOperation changeOperation;
                if (randomChange < 0.10) // 10% probability for INSERTs
                {
                    changeOperation = ChangeOperation.INSERT;
                }
                else if (randomChange < 0.20) // 10% probability for DELETEs
                {
                    changeOperation = ChangeOperation.DELETE;
                }
                else if (randomChange < 0.40) // 20% probability for doing NOTHING
                {
                    changeOperation = ChangeOperation.NOTHING;
                }
                else
                // 60% probablity for doing an ordinary UPDATE on an entity
                {
                    changeOperation = ChangeOperation.UPDATE;
                }
                if (entity2Change && allTestEntities.Count > 0)
                {
                    // If there are less than 10 entities, force to insertion of one
                    if (allTest2Entities.Count < 10)// || ChangeOperation.INSERT.Equals(changeOperation))
                    {
                        TestEntity2 newEntity = new TestEntity2();
                        newEntity.MyValue2 = randomNewValue;
                        HelloWorldService.SaveTestEntities2(newEntity);
                        allTest2Entities.Add(newEntity);
                    }
                    //// If there are more than 20 entities, force to deletion of one
                    else if (allTestEntities.Count > 20 || ChangeOperation.DELETE.Equals(changeOperation))
                    {
                        TestEntity2 deleteEntity = allTest2Entities[selectEntity2Index];
                        allTest2Entities.RemoveAt(selectEntity2Index);
                        HelloWorldService.DeleteTestEntities2(deleteEntity);
                    }
                    else if (ChangeOperation.UPDATE.Equals(changeOperation))
                    {
                        TestEntity2 updateEntity = allTest2Entities[selectEntity2Index];
                        updateEntity.MyValue2 = randomNewValue;
                        HelloWorldService.SaveTestEntities2(updateEntity);
                    }
                    else
                    {
                        TestEntity2 noOpEntity = allTest2Entities[selectEntity2Index];
                        // Change nothing, but try to save entity (results in NO-OP)
                        HelloWorldService.SaveTestEntities2(noOpEntity);
                    }
                }
                else
                {
                    // If there are less than 10 entities, force to insertion of one
                    if (allTestEntities.Count < 10)// || ChangeOperation.INSERT.Equals(changeOperation))
                    {
                        TestEntity newEntity = new TestEntity();
                        newEntity.MyValue = randomNewValue;
                        HelloWorldService.SaveTestEntities(newEntity);
                        allTestEntities.Add(newEntity);
                    }
                    // If there are more than 20 entities, force to deletion of one
                    else if (allTestEntities.Count > 20 || ChangeOperation.DELETE.Equals(changeOperation))
                    {
                        TestEntity deleteEntity = allTestEntities[changeIndex];
                        allTestEntities.RemoveAt(changeIndex);
                        HelloWorldService.DeleteTestEntities(deleteEntity);
                    }
                    else if (ChangeOperation.UPDATE.Equals(changeOperation))
                    {
                        TestEntity  updateEntity = allTestEntities[changeIndex];
                        TestEntity2 testEntity2  = allTest2Entities.Count > 0 ? allTest2Entities[selectEntity2Index] : null;
                        updateEntity.MyValue              = randomNewValue;
                        updateEntity.Relation             = testEntity2;
                        updateEntity.EmbeddedObject.Name  = "Name_" + randomNewValue;
                        updateEntity.EmbeddedObject.Value = randomNewValue;
                        HelloWorldService.SaveTestEntities(updateEntity);
                    }
                    else
                    {
                        TestEntity noOpEntity = allTestEntities[changeIndex];
                        // Change nothing, but try to save entity (results in NO-OP)
                        HelloWorldService.SaveTestEntities(noOpEntity);
                    }
                }
                Thread.Sleep(500);
            }
        }