Ejemplo n.º 1
0
        public static Task InsertOrUpdate <T>(
            string _fileName,
            T itemToUpdate,
            MatchForUpdate <T> matcher,
            Func <T, T> cloneWithUpdateVersion)
        {
            using (var stream = FileUtils.GetReadWriteStream(_fileName))
            {
                var list = stream.ReadList <T>();

                var found = false;
                for (int idx = 0; idx < list.Count && !found; idx++)
                {
                    var  item         = list[idx];
                    bool shouldUpdate = false;
                    (found, shouldUpdate) = matcher(item, itemToUpdate);

                    if (shouldUpdate)
                    {
                        list[idx] = cloneWithUpdateVersion(itemToUpdate);
                    }
                }

                if (!found)
                {
                    list.Add(cloneWithUpdateVersion(itemToUpdate));
                }

                stream.WriteList(list);
            }

            return(Task.FromResult(true));
        }
Ejemplo n.º 2
0
        public static Task DeleteItem <T>(
            string fileName,
            T itemToDelete,
            MatchForUpdate <T> matcher)
        {
            using (var stream = FileUtils.GetReadWriteStream(fileName))
            {
                var list = stream.ReadList <T>();

                var found = false;
                for (int idx = 0; idx < list.Count && !found; idx++)
                {
                    var  item         = list[idx];
                    bool shouldUpdate = false;
                    (found, shouldUpdate) = matcher(item, itemToDelete);

                    if (shouldUpdate)
                    {
                        list.RemoveAt(idx);
                    }
                }

                stream.WriteList(list);
            }

            return(Task.FromResult(true));
        }
Ejemplo n.º 3
0
        public static Task <bool> Exists <T>(
            string fileName,
            T itemToFind,
            MatchForUpdate <T> matcher)
        {
            var found = false;

            using (var stream = FileUtils.GetReadStream(fileName))
            {
                var list = stream.ReadList <T>();

                for (int idx = 0; idx < list.Count && !found; idx++)
                {
                    var  item         = list[idx];
                    bool shouldUpdate = false;
                    (found, shouldUpdate) = matcher(item, itemToFind);
                }
            }

            return(Task.FromResult(found));
        }