Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Linq.Expressions;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using AR.Entities;
- using AR.Gateways.XML;
- namespace AR.Gateways
- {
- public class UniversalGateway_XML<T> : IGateway<T> where T : BaseEntity
- {
- private XmlDocument document;
- private string mainName;
- private string mainListString;
- private XmlNode mainList;
- private string databaseFile;
- public UniversalGateway_XML(string p_databaseDirectory) {
- this.mainName = typeof(T).Name;
- this.mainListString = this.mainName + "List";
- this.databaseFile = p_databaseDirectory.TrimEnd('\\') + @"\" + mainName + "s.xml";
- if (!File.Exists(this.databaseFile))
- {
- using (FileStream fs = File.Create(this.databaseFile))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- sw.WriteLine("<?xml version=\"1.0\"?><" + this.mainListString + "></" + this.mainListString + ">");
- }
- }
- }
- this.document = new XmlDocument();
- this.document.Load(this.databaseFile);
- this.mainList = this.document.GetElementsByTagName(this.mainListString)[0];
- }
- private void saveXML()
- {
- this.document.Save(this.databaseFile);
- }
- private int getNextID()
- {
- int res = 1;
- try
- {
- XmlNodeList entitys = this.mainList.ChildNodes;
- foreach (XmlNode entit in entitys)
- {
- int lastID = -1;
- try
- {
- PropertyInfo[] ppi = typeof(BaseEntity).GetProperties();
- lastID = int.Parse(entit[ppi[0].Name].InnerText);
- }
- catch (Exception e)
- {
- res = -1;
- break;
- }
- if (lastID > -1)
- {
- if (lastID >= res)
- {
- res = lastID + 1;
- }
- }
- }
- }
- catch (Exception e)
- {
- res = -1;
- }
- return res;
- }
- public IEnumerable<T> List
- {
- get
- {
- throw new NotImplementedException();
- }
- }
- public bool Add(T entity)
- {
- bool res = false;
- try
- {
- XmlNode entityNode = this.document.CreateNode(XmlNodeType.Element, this.mainName, "");
- int newID = this.getNextID();
- if (newID > -1)
- {
- entity.Id = newID;
- foreach (PropertyInfo propertyInfo in typeof(User).GetProperties())
- {
- XmlNode node_property = this.document.CreateNode(XmlNodeType.Element, propertyInfo.Name, "");
- node_property.InnerText = propertyInfo.GetValue(entity, null).ToString();
- entityNode.AppendChild(node_property);
- }
- this.mainList.AppendChild(entityNode);
- this.saveXML();
- res = true;
- }
- }
- catch (Exception e)
- {
- res = false;
- }
- return res;
- }
- public bool Delete(T entity)
- {
- throw new NotImplementedException();
- }
- public T FindById(int Id)
- {
- throw new NotImplementedException();
- }
- public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
- {
- throw new NotImplementedException();
- }
- public bool Update(T entity)
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement