示例#1
0
        public bool Create <T, R>(IEnumerable <T> items, Func <T, R> orderBy = null)
        {
            try
            {
                IOBoundary      iob  = IOBoundary.Create <T>();
                IEnumerable <T> prev = iob.Read().Select(line => JsonConvert.DeserializeObject <T>(line));
                items = prev.Concat(items);

                if (orderBy != null)
                {
                    items.OrderBy(orderBy);
                }

                IEnumerable <string> lines = items.Select(i => JsonConvert.SerializeObject(i));

                iob.Write(lines);

                return(true);
            }
            catch (Exception e)
            {
                if (Program.Employee == 2)
                {
                    Console.WriteLine(e.Message);
                }
                return(false);
            }
        }
示例#2
0
        public IEnumerable <T> Delete <T>(Func <T, bool> filter = null)
        {
            try
            {
                IOBoundary      iob = IOBoundary.Create <T>();
                IEnumerable <T> prev, toSave, toDelete;

                prev = iob.Read().Select(line => JsonConvert.DeserializeObject <T>(line));

                if (filter != null)
                {
                    toSave   = prev.Where(p => !filter(p));
                    toDelete = prev.Where(p => filter(p));
                }
                else
                {
                    toDelete = prev;
                    toSave   = Enumerable.Empty <T>();
                }

                IEnumerable <string> lines = toSave.Select(t => JsonConvert.SerializeObject(t));

                iob.Write(lines);

                return(toDelete);
            }
            catch (Exception e)
            {
                if (Program.Employee == 2)
                {
                    Console.WriteLine(e.Message);
                }
                return(Enumerable.Empty <T>());
            }
        }
示例#3
0
        public IEnumerable <T> Read <T>(Func <T, bool> filter = null)
        {
            try
            {
                IOBoundary      iob  = IOBoundary.Create <T>();
                IEnumerable <T> prev = iob.Read().Select(line => JsonConvert.DeserializeObject <T>(line));

                if (filter != null)
                {
                    prev = prev.Where(filter);
                }

                return(prev);
            }
            catch (Exception e)
            {
                if (Program.Employee == 2)
                {
                    Console.WriteLine(e.Message);
                }
                return(Enumerable.Empty <T>());
            }
        }
示例#4
0
        public bool Update <T>(Func <T, T> update, Func <T, bool> filter = null)
        {
            try
            {
                IOBoundary      iob = IOBoundary.Create <T>();
                IEnumerable <T> prev, toUpdate;

                prev = iob.Read().Select(line => JsonConvert.DeserializeObject <T>(line));

                if (filter != null)
                {
                    toUpdate = prev.Where(p => filter(p));
                    prev     = prev.Where(p => !filter(p));
                }
                else
                {
                    toUpdate = prev;
                    prev     = Enumerable.Empty <T>();
                }

                IEnumerable <T> updated = toUpdate.Select(update).ToArray();

                IEnumerable <string> lines = prev.Concat(updated).Select(x => JsonConvert.SerializeObject(x));

                iob.Write(lines);

                return(true);
            }
            catch (Exception e)
            {
                if (Program.Employee == 2)
                {
                    Console.WriteLine(e.Message);
                }
                return(false);
            }
        }