回到目录
之前我写过关于XMLRepository仓储的实现的文章,主要是针对XElement方式的,对于XML的结构,一般来说有两种,一是使用节点方式的,我习惯称为XElement方式,别一种是属性方式的,我习惯称为XAttribute,这两种方式,我比较取向于后面的,好处就是更简洁,将C#里的类看作节点,而将C#里的属性看作是节点的每个特性(XAttribute),这种方式感觉更容易被人接受!
如果您还看不懂这两方式指的是什么,就看一下两种方式的XML举例吧
XElement方式
<User>
<id>1</id>
<name>zzl</name>
</User>
XAttribute方式
<User id="1" name="zzl" />
怎么样,第二种方式更简洁吧,但要注意,XAttribute方式书写时,需要为每个特性的值加双引号的,因为linq to xml认为每个XAttribute都是字符型的,在进行操作时,你可以根据实体类型进行转换的
下面看一下我为XAttribute方式进行封装的Repository吧
XML2Repository.cs源代码
/// <summary>
/// XML文件数据仓储
/// XML结构为Attribute
/// </summary>
/// <typeparam name="TEntity"></typeparam>
public class XML2Repository<TEntity> :
IRepository<TEntity>
where TEntity : XMLEntity, new()
{
XDocument _doc;
string _filePath;
static object lockObj = new object();
public XML2Repository(string filePath)
{
_filePath = filePath;
_doc = XDocument.Load(filePath);
}
public void Insert(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement db = new XElement(typeof(TEntity).Name);
foreach (var member in item.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))
{
db.Add(new XAttribute(member.Name, member.GetValue(item, null) ?? string.Empty));
}
_doc.Root.Add(db);
lock (lockObj)
{
_doc.Save(_filePath);
}
}
public void Delete(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement xe = (from db in _doc.Root.Elements(typeof(TEntity).Name)
where db.Attribute("RootID").Value == item.RootID
select db).Single() as XElement;
xe.Remove();
lock (lockObj)
{
_doc.Save(_filePath);
}
}
public void Update(TEntity item)
{
if (item == null)
throw new ArgumentException("The database entity can not be null.");
XElement xe = (from db in _doc.Root.Elements(typeof(TEntity).Name)
where db.Attribute("RootID").Value == item.RootID
select db).Single();
try
{
foreach (var member in item.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))
{
xe.SetAttributeValue(member.Name, member.GetValue(item, null) ?? string.Empty);
}
lock (lockObj)
{
_doc.Save(_filePath);
}
}
catch
{
throw;
}
}
public IQueryable<TEntity> GetModel()
{
IEnumerable<XElement> list = _doc.Root.Elements(typeof(TEntity).Name);
IList<TEntity> returnList = new List<TEntity>();
foreach (var item in list)
{
TEntity entity = new TEntity();
foreach (var member in entity.GetType()
.GetProperties()
.Where(i => i.PropertyType.IsValueType
|| i.PropertyType == typeof(String)))//只找简单类型的属性
{
if (item.Attribute(member.Name) != null)
member.SetValue(entity, Convert.ChangeType(item.Attribute(member.Name).Value, member.PropertyType), null);//动态转换为指定类型
}
returnList.Add(entity);
}
return returnList.AsQueryable();
}
public TEntity Find(params object[] id)
{
return GetModel().FirstOrDefault(i => i.RootID == Convert.ToString(id[0]));
}
public void SetDbContext(IUnitOfWork unitOfWork)
{
throw new NotImplementedException();
}
}
至此,对于XML的仓储实现就已经完都讲完了,而对于其它仓储我也都在这前介绍过,其中包括EFRepsitory,LinqRepository,MemoryRepositoy,RedisRepository再加上今天的XMLRepository和XML2Repository一共六大仓储了,感觉已经可以写一篇
关于如何实现仓储的文章了,哈哈。
作者:仓储大叔,张占岭,
荣誉:微软MVP
QQ:853066980
支付宝扫一扫,为大叔打赏!
