EF架构~XMLRepository仓储的实现
对于数据仓储大家应该都很熟悉了,它一般由几个仓储规范和实现它的具体类组成,而仓储的接口与架构本身无关,对于仓储的实现,你可以选择linq2Sql,EF,Nosql,及XML
等等,之前我介绍过linq2Sql,ef和nosql(redis)的仓储实现,今天主要说一下xml仓储的实现。
下面的相关核心代码
XML实体基类
/// <summary> /// XML实体基类 /// </summary> public abstract class XMLEntity { private string id = Guid.NewGuid().ToString(); /// <summary> /// XML实体主键 /// </summary> public string RootID { get { return id; } set { id = value; } } }
XML实体的仓储操作
/// <summary> /// XML文件数据仓储 /// XML结构为Element /// </summary> /// <typeparam name="TEntity"></typeparam> public class XMLRepository<TEntity> : IRepository<TEntity> where TEntity : XMLEntity, new() { XDocument _doc; string _filePath; static object lockObj = new object(); public XMLRepository(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 XElement(member.Name, new XAttribute("value", member.GetValue(item, null)))); } _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.Element("RootID").Attribute("value").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.Element("RootID").Attribute("value").Value == item.RootID select db).Single(); try { foreach (var member in item.GetType() .GetProperties() .Where(i => i.PropertyType.IsValueType || i.PropertyType == typeof(String)))//只找简单类型的属性 { xe.Add(new XElement(member.Name, new XAttribute("value", member.GetValue(item, null)))); } 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.Element(member.Name).Attribute("value").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(); } }
感觉面向对象也是一种病,但这种病我认为是正确的,当你对它的理解达到某种程度时,这种病就会犯了,并且你会相信,世间万物,皆为对象。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
2012-04-15 MVC验证(只验证指定字段)