コード例 #1
0
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            if (ParentProperty?.PropertyType != PropertyType.Lookup)
            {
                throw new NotSupportedException("You cannot use SetItem on a property thats not a lookup.");
            }

            LazyLoad();
            LazySet();

            //if (!moment.HasValue)
            //	moment = RunningTransaction.TransactionDate;

            if (item is not null && EagerLoadLogic is not null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.Where(e => e.Overlaps(moment, null)).ToList();

            if (NeedsToAssign(currentItem, item, moment))
            {
                if (ForeignProperty is not null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item is null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue is not null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, moment);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, moment);
                    }
                }

                if (item is null)
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            foreach (CollectionItem <TEntity> current in currentItem)
                            {
                                if (current?.Item is not null)
                                {
                                    Remove(current.Item, moment);
                                }
                            }
                        }

                        if (Count() > 0)
                        {
                            Clear(moment, false);                             // Clear should not be called here as this is for lookup.
                        }
                    }
                }
                else
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            foreach (CollectionItem <TEntity> current in currentItem)
                            {
                                if (current?.Item is not null)
                                {
                                    Remove(current.Item, moment);
                                }
                            }
                        }

                        if (Count() > 0)
                        {
                            Clear(moment, false);                             // Clear should not be called here as this is for lookup.
                        }
                    }

                    if (Count() == 0)
                    {
                        Add(item, moment, false);
                    }
                }
            }
コード例 #2
0
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            LazyLoad();
            LazySet();

            if (!moment.HasValue)
            {
                moment = RunningTransaction.TransactionDate;
            }

            if (item != null && EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.Where(e => e.Overlaps(moment, null)).ToList();

            if (!currentItem.FirstOrDefault()?.Item?.Equals(item) ?? !ReferenceEquals(item, null))
            {
                if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item is null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue != null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, null);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, moment);
                    }
                }

                if (item == null)
                {
                    if (ParentProperty?.PropertyType == PropertyType.Lookup)
                    {
                        Remove(currentItem[0].Item, moment);
                    }

                    if (currentItem.Count > 0)
                    {
                        Clear(moment, false);
                    }
                }
                else
                {
                    if (currentItem.Count == 1 && currentItem[0].Item.Equals(item))
                    {
                        return;
                    }

                    if (ParentProperty?.PropertyType == PropertyType.Lookup && currentItem.Count == 1)
                    {
                        Remove(currentItem[0].Item, moment);
                    }

                    if (currentItem.Count > 0)
                    {
                        Clear(moment, false);
                    }

                    if (CountAt(moment) == 0)
                    {
                        Add(item, moment, false);
                    }
                }
            }
        }
コード例 #3
0
        protected override void SetItem(TEntity?item, DateTime?moment)
        {
            LazyLoad();
            LazySet();

            if (item != null && EagerLoadLogic != null)
            {
                EagerLoadLogic.Invoke(item);
            }

            List <CollectionItem <TEntity> > currentItem = InnerData.ToList();

            if (!currentItem.FirstOrDefault()?.Item?.Equals(item) ?? !ReferenceEquals(item, null))
            {
                if (ForeignProperty != null && ForeignProperty.PropertyType == PropertyType.Lookup)
                {
                    OGM?oldForeignValue = (item == null) ? null : (OGM)ForeignProperty.GetValue(item, moment);
                    if (oldForeignValue != null)
                    {
                        ParentProperty?.ClearLookup(oldForeignValue, null);
                    }

                    foreach (TEntity entity in currentItem.Select(iitem => iitem.Item).Distinct())
                    {
                        ForeignProperty.ClearLookup(entity, null);
                    }
                }

                if (item == null)
                {
                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            Remove(currentItem[0].Item);
                        }

                        if (Count > 0)
                        {
                            Clear(false); // Clear should not be called here as this is for lookup.
                        }
                    }
                }
                else
                {
                    if (currentItem.Count == 1 && currentItem[0].Item.Equals(item))
                    {
                        return;
                    }

                    if (currentItem.Count > 0)
                    {
                        if (ParentProperty?.PropertyType == PropertyType.Lookup)
                        {
                            Remove(currentItem[0].Item);
                        }

                        if (Count > 0)
                        {
                            Clear(false); // Clear should not be called here as this is for lookup.
                        }
                    }

                    if (Count == 0)
                    {
                        Add(item, false);
                    }
                }
            }
        }