Пример #1
0
        /// <summary>
        /// Removes a selection of items that inherit from ICollectable object from their database collection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        public void Remove <T>(ICollection <T> items) where T : ICollectableObject
        {
            if (items.IsNull())
            {
                throw new ArgumentNullException("The parameter items cannot be null.");
            }

            DatabaseItemInfo <T> dbInfo = GetDatabaseItemInfo <T>();

            var db           = GetType().GetMethod("GetDatabase", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(dbInfo.DatabaseType).Invoke(this, null);
            var dbCollection = db.GetType().GetProperty(dbInfo.CollectionName).GetValue(db) as ICollection <T>;

            List <T> newCollection = dbCollection.IsNull() ? new List <T>() : dbCollection.ToList();

            items.ForEach(item => newCollection.RemoveAll(i => ((ICollectableObject)i).GUID == item.GUID));

            Save <T>(newCollection);
        }
Пример #2
0
        /// <summary>
        /// Gets a selection of items that inherit from ICollectable object from their database collection.
        /// If propertyName and propertyValue are null, then returns all items of the selected type T in the database.
        /// If propertyName and propertyValue are NOT null, then all items matching the query (propertyNames that have propertyValues as their value).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName"></param>
        /// <param name="propertyValue"></param>
        /// <returns></returns>
        private ICollection <T> Get <T>(string propertyName = null, object propertyValue = null, bool dummyParameter = false) where T : ICollectableObject
        {
            if (string.IsNullOrEmpty(propertyName) && !propertyValue.IsNull())
            {
                throw new Exception("If propertyName is null, then propertyValue must also be null.");
            }

            bool getAllItems = string.IsNullOrEmpty(propertyName) && propertyValue.IsNull();

            DatabaseItemInfo <T> dbInfo = GetDatabaseItemInfo <T>();

            var db           = GetType().GetMethod("GetDatabase", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(dbInfo.DatabaseType).Invoke(this, null);
            var dbCollection = db.GetType().GetProperty(dbInfo.CollectionName).GetValue(db) as ICollection <T>;

            ICollection <T> items = null;

            if (getAllItems)
            {
                items = dbCollection;
            }
            else
            {
                List <T> col = new List <T>();
                foreach (var item in dbCollection)
                {
                    var prop = item.GetType().GetProperty(propertyName);
                    if (!prop.IsNull())
                    {
                        var propValue = prop.GetValue(item);
                        if (!propValue.IsNull())
                        {
                            if (prop.GetValue(item).ToString() == (propertyValue ?? "").ToString())
                            {
                                col.Add(item);
                            }
                        }
                    }
                }
                items = col.Any() ? col : null;
            }

            //A collection or null should be returned.
            return(items);
        }
Пример #3
0
        /// <summary>
        /// Saves a selection of items in its respective database.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        public void Save <T>(ICollection <T> items) where T : ICollectableObject
        {
            if (items.IsNull())
            {
                throw new ArgumentNullException("The parameter items cannot be null.");
            }

            DatabaseItemInfo <T> dbInfo = GetDatabaseItemInfo <T>();

            var db = GetType().GetMethod("GetDatabase", BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(dbInfo.DatabaseType).Invoke(this, null);

            Type   typeOfCollection = db.GetType().GetProperty(dbInfo.CollectionName).PropertyType;
            object newItems         = EnumerateCollection <T>(items);

            if (typeOfCollection.BaseType == typeof(Array))
            {
                newItems = (newItems as ICollection <T>).ToArray();
            }
            else if (typeOfCollection.BaseType == typeof(Collection <T>))
            {
                newItems = (newItems as ICollection <T>).ToObservableCollection();
            }

            db.GetType().GetProperty(dbInfo.CollectionName).SetValue(db, newItems);

            string path = GetDatabasePath(dbInfo.DatabaseType);

            lock (lockObject)
            {
                var xml = db.Serialize();

                try
                {
                    try
                    {
                        if (!mutex.IsNull())
                        {
                            mutex.WaitOne();
                        }

                        xml.Save(path);

                        //Will fire the database changed event when the FileSystemWatcher is unavailable.
                        //This should happen only when using Xamarin.Forms as there is no native support for that class.
                        if (fileSystemWatcher.IsNull())
                        {
                            OnDatabaseChanged?.Invoke(this, new OnDatabaseChangedEventArgs(dbInfo.DatabaseType.Name, DateTime.Now));
                        }
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        if (!mutex.IsNull())
                        {
                            mutex.ReleaseMutex();
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                }
            }
        }