Exemplo n.º 1
0
        public IEnumerable <T> GetAll()
        {
            List <T> entities = new List <T>();

            Directory.CreateDirectory($"{this.type.Name}");

            // Get all xml files from our type directory
            FileInfo[] files = this.directory.GetFiles("*.xml");

            if (files == null)
            {
                return(entities);
            }

            foreach (var file in files)
            {
                using (FileStream fs = new FileStream(file.FullName, FileMode.Open))
                {
                    // deserialize object
                    T objFromXml = (T)this.serializer.Deserialize(fs);

                    if (this.objectHasPasswordProperty)
                    {
                        XmlMaker.EncodePasswordAndSetToObject(ref objFromXml);
                    }

                    entities.Add(objFromXml);
                }
            }

            return(entities);
        }
Exemplo n.º 2
0
        public T Get(int id)
        {
            T objectFromXml;
            // get file by id
            FileInfo file = this.directory.GetFiles($"{type.Name}{id}.xml").FirstOrDefault();

            if (file == null)
            {
                return(null);
            }
            ;

            using (FileStream fs = new FileStream($"{this.type.Name}/{file.Name}", FileMode.Open))
            {
                // deserialize object
                objectFromXml = (T)this.serializer.Deserialize(fs);

                if (this.objectHasPasswordProperty)
                {
                    // encode password
                    XmlMaker.EncodePasswordAndSetToObject(ref objectFromXml);
                }
            }

            return(objectFromXml);
        }
Exemplo n.º 3
0
        public void Update(T objectToUpdate)
        {
            Thread.Sleep(200);

            using (FileStream fs = new FileStream($"{this.type.Name}/{this.type.Name}{typeof(T).GetProperty("Id").GetValue(objectToUpdate)}.xml", FileMode.Create))
            {
                if (this.objectHasPasswordProperty)
                {
                    // decode password
                    XmlMaker.DecodePasswordAndSetToObject(ref objectToUpdate);
                }

                // serialize
                this.serializer.Serialize(fs, objectToUpdate);

                if (this.objectHasPasswordProperty)
                {
                    // encode password
                    XmlMaker.EncodePasswordAndSetToObject(ref objectToUpdate);
                }
            }
        }