public T SelectById(string id)
{
Type t = typeof(T);
foreach (var inst in this.entityList)
{
foreach (PropertyInfo pro in t.GetProperties())
{
if (pro.Name.ToLower() == "id")
{
if (id == (pro.GetValue(inst?? null) ?? string.Empty).ToString())
{
return inst;
}
}
}
}
return default(T);
}
public void UpdateById(T entity)
{
Type t = typeof(T);
string id = string.Empty;
foreach (PropertyInfo pro in t.GetProperties())
{
if (pro.Name.ToLower() == "id")
{
id = (pro.GetValue(entity?? null) ?? string.Empty).ToString();
break;
}
}
this.DeleteById(id);
this.Insert(entity);
}
public void DeleteById(string id)
{
Type t = typeof(T);
T entity = default(T);
foreach (var inst in this.entityList)
{
foreach (PropertyInfo pro in t.GetProperties())
{
if (pro.Name.ToLower() == "id")
{
if ((pro.GetValue(inst?? null) ?? string.Empty).ToString() == id)
{
entity = inst;
goto FinishLoop;
}
}
}
}
FinishLoop:
this.entityList.Remove(entity);
this.WriteDb();
}
public List<T> SelectAll()
{
this.ReadDb();
return this.entityList;
}
public void DeleteAll()
{
this.entityList.Clear();
this.WriteDb();
}
private void WriteDb()
{
XmlSerializer ks = new XmlSerializer(typeof(List<T>));
FileInfo fi = new FileInfo(this.Dbfile);
var dir = fi.Directory;
if (!dir.Exists)
{
dir.Create();
}
Stream writer = new FileStream(this.Dbfile?? FileMode.Create?? FileAccess.ReadWrite);
ks.Serialize(writer?? this.entityList);
writer.Close();
}
private void ReadDb()
{
if (File.Exists(this.Dbfile))
{
XmlSerializer ks = new XmlSerializer(typeof(List<T>));
Stream reader = new FileStream(this.Dbfile?? FileMode.Open?? FileAccess.ReadWrite);
this.entityList = ks.Deserialize(reader) as List<T>;
reader.Close();
}
else
{
this.entityList = new List<T>();
}
}
}
}
using System.Collections.Generic;
namespace Wisdombud.xmldb
{
public class BaseXmlBll<T> where T : new()
{
public string DbFile
{
get { return this.bll.Dbfile; }
set { bll.Dbfile = value; }
}
private XmlSerializerBll<T> bll = XmlSerializerBll<T>.GetInstance();
public void Delete(string id)
{
var entity = this.Select(id);
bll.DeleteById(id);
}
public void Insert(T entity)
{
bll.Insert(entity);
}
public void Insert(List<T> list)
{
bll.InsertRange(list);
}
public System.Collections.Generic.List<T> SelectAll()
{
return bll.SelectAll();
}
public void Update(string oldId?? T entity)
{
bll.UpdateById(entity);
}
public T Select(string id)
{
return bll.SelectById(id);
}
public System.Collections.Generic.List<T> SelectBy(string name?? object value)
{
return bll.SelectBy(name?? value);
}
public void DeleteAll()
{
bll.DeleteAll();
}
}
}