Пример #1
0
        public bool SetCollection <T>(ref T CollectionField_, T value, [CallerMemberName] string propertyName = null) where T : INotifyCollectionChanged
        {
            if (DBContext != null)
            {
                if (CollectionField_ != null)
                {
                    object         OldCollection = (object)CollectionField_;
                    CollectionInfo item          = Collections.FirstOrDefault(x => x.Collection == OldCollection);
                    if (item != null)
                    {
                        CollectionField_.CollectionChanged -= CollectionChanged;
                        Collections.Remove(item);
                    }
                }
                CollectionField_ = value;
                if (CollectionField_ != null)
                {
                    InitiliazeCollection(CollectionField_, propertyName);
                }
            }
            else
            {
                if (EqualityComparer <T> .Default.Equals(CollectionField_, value))
                {
                    return(false);
                }
                CollectionField_ = value;
            }

            OnPropertyChanged(propertyName);
            return(true);
        }
Пример #2
0
        private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (DBContext != null)
            {
                CollectionInfo collectionInfo = Collections.FirstOrDefault(x => x.Collection == sender);
                if (collectionInfo == null)
                {
                    throw new Exception("Collection not initialized");
                }

                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    foreach (Base obj in e.NewItems)
                    {
                        if (obj.DBContext == null)
                        {
                            DBContext.AddNewItemToDBContext(obj, collectionInfo.DependentTableInfo.Type);
                        }
                        PropertyInfo prop   = collectionInfo.KeyIdProperytInfo.Property;
                        UUID         old_id = (UUID)prop.GetValue(obj);
                        if (old_id != this.id)
                        {
                            obj.Modified();
                            prop.SetValue(obj, this.id);
                        }
                    }
                    break;


                case NotifyCollectionChangedAction.Remove:
                    foreach (Base obj in e.OldItems)
                    {
                        if (obj.DBContext == null)
                        {
                            throw new Exception("A collection has an item which hasn't the DBContext");
                        }
                        if (collectionInfo.KeyIdProperytInfo.IsNullable)
                        {
                            collectionInfo.KeyIdProperytInfo.Property.SetValue(obj, null);
                        }

                        if (collectionInfo.InversePropertyInfo.CascadeDeleteAttribute)
                        {
                            DBContext.Delete(obj, collectionInfo.InversePropertyInfo.GenericType);
                        }
                    }
                    break;

                case NotifyCollectionChangedAction.Move:
                    break;

                case NotifyCollectionChangedAction.Reset:
                    break;

                default:
                    throw new Exception("CollectionChanged: Operation " + e.Action + " not permitted");
                }
            }
        }
Пример #3
0
        void UpdateCollections <T>(string propertyName, T owner, CollectionOperations operation) where T : Base
        {
            TableInfo  thisTi = DBContext.DBschema.GetTable(this.GetType());
            ColumnInfo thisCi = thisTi.GetColumnInfo(propertyName);

            if (thisCi.ForeignKeyAttribute)
            {
                //TableInfo owner_ti = DBContext.DBschema.GetTable<T>();
                //ColumnInfo owner_ci = owner_ti.Columns.Select(x=>x.Value).FirstOrDefault(x => x.InversePropertyName == propertyName);
                //if (owner_ci == null) return;

                CollectionInfo colinf = owner.Collections.FirstOrDefault(x => x.KeyPropertyInfo == thisCi);
                if (colinf != null && colinf.isLoadedFromDB)
                {
                    IList ilist = (IList)colinf.Collection;
                    switch (operation)
                    {
                    case CollectionOperations.Add:
                        if (!ilist.Contains(this))
                        {
                            ilist.Add(this);
                        }
                        break;

                    case CollectionOperations.Remove:
                        if (ilist.Contains(this))
                        {
                            ilist.Remove(this);
                        }
                        break;
                    }
                }
            }
        }
Пример #4
0
        public T GetCollection <T>(ref T CollectionField_, [CallerMemberName] string propertyName = null) where T : INotifyCollectionChanged, IList, new()
        {
            if (DBContext != null)
            {
                object         objCollection = (object)CollectionField_;
                CollectionInfo item          = Collections.FirstOrDefault(x => x.Collection == objCollection);
                if (item == null)
                {
                    item = InitiliazeCollection(CollectionField_, propertyName);
                }
                if (item == null)
                {
                    throw new Exception(propertyName + " collection isn't initialized for lazy loading.");
                }
                if (!item.isLoadedFromDB)
                {
                    SQLite.Net.TableMapping tablemapping = new SQLite.Net.TableMapping(item.DependentTableInfo.Type, item.DependentTableInfo.Type.GetRuntimeProperties());
                    string sql = "select * from " + item.DependentTableInfo.SqlName + " where " + item.KeyIdProperytInfo.SqlName + " = ?";

                    List <object> items = DBContext.DbConnect.Query(tablemapping, sql, this.id);
                    foreach (Base coll_item in items)
                    {
                        Base objfromcache = DBContext.FindObjectInCache(item.DependentTableInfo.Type, coll_item.id);
                        if (objfromcache == null)
                        {
                            DBContext.AttachToDBContextInternal(coll_item, item.DependentTableInfo.Type, Entity.EntityState.Unchanged);
                            CollectionField_.Add(Convert.ChangeType(coll_item, item.DependentTableInfo.Type));
                        }
                        else
                        {
                            CollectionField_.Add(Convert.ChangeType(objfromcache, item.DependentTableInfo.Type));
                        }
                    }

                    item.isLoadedFromDB = true;
                }
            }
            return(CollectionField_);
        }
Пример #5
0
        public CollectionInfo InitiliazeCollection(INotifyCollectionChanged collection, string propertyName)
        {
            if (DBContext == null)
            {
                throw new Exception("DbContext for " + this + " is null, collection can't be initialized");
            }
            if (collection == null)
            {
                throw new Exception("Collection is null");
            }

            CollectionInfo ci;

            ci = Collections.FirstOrDefault(x => x.Collection == collection);
            if (ci != null)
            {
                return(ci);
            }

            ci                = new CollectionInfo();
            ci.Collection     = collection;
            ci.isLoadedFromDB = false;

            // A table and a property of current object
            Type this_type = this.GetType();

            ci.MasterTableInfo = DBContext.DBschema.GetTable(this_type);
            if (ci.MasterTableInfo == null)
            {
                throw new Exception("Can't initialize collection. Master table info not found");
            }
            ci.InversePropertyInfo = ci.MasterTableInfo.GetColumnInfo(propertyName);
            if (ci.InversePropertyInfo == null)
            {
                throw new Exception("Can't initialize collection. Inverse property info not found");
            }

            // The dependent table
            Type typeofcollection           = collection.GetType();
            Type generic_type_of_collection = typeofcollection.GenericTypeArguments[0];

            ci.DependentTableInfo = DBContext.DBschema.GetTable(generic_type_of_collection);
            if (ci.DependentTableInfo == null)
            {
                throw new Exception("Can't initialize collection. Dependent table info not found");
            }

            // The key property
            TypeInfo                 this_type_info   = this_type.GetTypeInfo();
            PropertyInfo             this_prop        = this_type_info.GetDeclaredProperty(propertyName);
            InversePropertyAttribute inverse_property = this_prop.GetCustomAttribute <InversePropertyAttribute>();

            ci.KeyPropertyInfo = ci.DependentTableInfo.GetColumnInfo(inverse_property.PropertyName);
            if (ci.KeyPropertyInfo == null)
            {
                throw new Exception("Can't initialize collection. Key property info not found");
            }

            // The key id property
            TypeInfo            generic_type_of_collection_type_info = generic_type_of_collection.GetTypeInfo();
            PropertyInfo        generic_type_property = generic_type_of_collection_type_info.GetDeclaredProperty(ci.KeyPropertyInfo.ClrName);
            ForeignKeyAttribute foreignKeyAttribute   = generic_type_property.GetCustomAttribute <ForeignKeyAttribute>();

            ci.KeyIdProperytInfo = ci.DependentTableInfo.GetColumnInfo(foreignKeyAttribute.ForeignKeyName);
            if (ci.KeyIdProperytInfo == null)
            {
                throw new Exception("Can't initialize collection. Key id property info not found");
            }

            collection.CollectionChanged += CollectionChanged;
            Collections.Add(ci);

            return(ci);
        }