Example #1
0
        protected virtual void ProcessRemove(IList oldItems)
        {
            foreach (var oi in oldItems.Cast <T>())
            {
                _contains.Remove(oi);
            }

            if (!EnableIntegration || !EnableLinking)
            {
                return;
            }

            if (ParentContainer != null)
            {
                var oiCopy = (from a in oldItems.Cast <Object>() select a).ToList();

                foreach (var oi in oiCopy)
                {
                    if (oi != null)
                    {
                        // Attempt to establish a FK relationship, carry parent key down
                        CEF.CurrentKeyService()?.UnlinkChildFromParentContainer(BoundScope, ParentTypeName, ParentFieldName, ParentContainer, oi);
                    }
                }
            }
        }
        public void AddByIdentity <T>(T o, object[] key = null, int?expirySeconds = null) where T : class, new()
        {
            // We can only cache tracked objects!
            var iw = o?.AsInfraWrapped();

            if (iw == null)
            {
                return;
            }

            StringBuilder sb = new StringBuilder(128);

            sb.Append(o.GetBaseType().Name);

            if (key == null)
            {
                key = (from a in CEF.CurrentKeyService()?.GetKeyValues(o) select a.value).ToArray();
            }

            if (!key.Any())
            {
                throw new CEFInvalidOperationException("No primary key for this object makes it impossible to cache.");
            }

            foreach (var k in key)
            {
                sb.Append(k);
            }

            var c = _index.GetFirstByName(nameof(MFSEntry.ByIdentityComposite), sb.ToString());

            if (!expirySeconds.HasValue)
            {
                expirySeconds = CEF.CurrentServiceScope.ResolvedCacheDurationForType(typeof(T));
            }

            var newExpDate = DateTime.Now.AddSeconds(expirySeconds.GetValueOrDefault(DefaultCacheIntervalSeconds));

            if (c == null)
            {
                c = new MFSEntry();
                c.ObjectTypeName      = o.GetBaseType().Name;
                c.ByIdentityComposite = sb.ToString();
                c.FileName            = BuildNewFileName(o.GetBaseType());
                _index.Add(c);
            }

            long current;

            lock (c.ObjSync)
            {
                current      = ++c.Sequence;
                c.ExpiryDate = newExpDate;
                c.Properties = iw.GetAllValues(true, true);
                c.Persisted  = false;
                c.Active     = true;
            }
        }
Example #3
0
        private void WireDependencies()
        {
            foreach (var item in _toWire.ToList())
            {
                CEF.CurrentKeyService()?.WireDependents(item.AsUnwrapped(), item, BoundScope, this, null);
            }

            _toWire.Clear();
        }
Example #4
0
        private void WireDependencies()
        {
            foreach (var item in _toWire.ToList())
            {
                var iw = item.AsUnwrapped() ?? throw new CEFInvalidStateException(InvalidStateType.ObjectTrackingIssue);
                CEF.CurrentKeyService()?.WireDependents(iw, item, BoundScope, this, null);
            }

            _toWire.Clear();
        }
Example #5
0
        private string GetKey(object o)
        {
            var kv = CEF.CurrentKeyService().GetKeyValues(o);

            if (kv?.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                foreach (var k in kv)
                {
                    sb.Append(k.value?.ToString());
                    sb.Append("~");
                }

                return(sb.ToString());
            }

            return(null);
        }
Example #6
0
        protected virtual void ProcessAdd(IList newItems, int newStartingIndex)
        {
            var niCopy = (from a in newItems.Cast <Object>() select a).ToList();

            foreach (T ni in niCopy)
            {
                _contains[ni] = true;
            }

            if (!EnableIntegration)
            {
                return;
            }

            if (BoundScope != null)
            {
                // First, we inspect to see if we have a wrapped object or not - if not, we try to do so and REPLACE the current item
                if (!BoundScope.Settings.EntitySetUsesUnwrapped)
                {
                    var idx2 = 0;
                    foreach (var ni in niCopy.ToArray())
                    {
                        if (ni != null)
                        {
                            if (BoundScope.InternalCreateAddBase(ni, true, null, null, null, new Dictionary <object, object>(Globals.DefaultDictionaryCapacity), true, false) is ICEFWrapper w)
                            {
                                var cast = w as T;

                                if (ni != cast)
                                {
                                    try
                                    {
                                        this.SuspendNotifications(true);

                                        using (new WriterLock(_lock))
                                        {
                                            this.Replace(ni as T, cast);

                                            _contains.Add(cast, true);
                                            _contains.Remove(newItems[idx2] as T);
                                        }
                                    }
                                    finally
                                    {
                                        this.SuspendNotifications(false);
                                    }
                                }

                                niCopy[idx2] = cast;
                            }
                        }

                        idx2++;
                    }
                }
            }

            if (ParentContainer != null && EnableLinking)
            {
                foreach (var ni in niCopy)
                {
                    if (ni != null)
                    {
                        // Attempt to establish a FK relationship, carry parent key down
                        CEF.CurrentKeyService()?.LinkChildInParentContainer(BoundScope, ParentTypeName, ParentFieldName, ParentContainer, ni);
                    }
                }
            }
        }