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[]));
} public void SetDbContext(IUnitOfWork unitOfWork)
{
throw new NotImplementedException();
}
}
感觉面向对象也是一种病,但这种病我认为是正确的,当你对它的理解达到某种程度时,这种病就会犯了,并且你会相信,世间万物,皆为对象。
EF架构~XMLRepository仓储的实现的更多相关文章
- EF架构~XMLRepository仓储的实现~续(XAttribute方式)
回到目录 之前我写过关于XMLRepository仓储的实现的文章,主要是针对XElement方式的,对于XML的结构,一般来说有两种,一是使用节点方式的,我习惯称为XElement方式,别一种是属性 ...
- EF架构~基于EF数据层的实现
回到目录 之前写过关于实现一个完整的EF架构的文章,文章的阅读量也是满大的,自己很欣慰,但是,那篇文章是我2011年写的,所以,技术有些不成熟,所以今天把我的2014年写的EF底层架构公开一下,这个架 ...
- EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一
回到目录 本讲是通过DbCommand拦截器来实现读写分离的最后一讲,对之前几篇文章做了一个优化,无论是程序可读性还是实用性上都有一个提升,在配置信息这块,去除了字符串方式的拼接,取而代之的是sect ...
- DDD分层架构之仓储
DDD分层架构之仓储(层超类型基础篇) 前一篇介绍了仓储的基本概念,并谈了我对仓储的一些认识,本文将实现仓储的基本功能. 仓储代表聚合在内存中的集合,所以仓储的接口需要模拟得像一个集合.仓储中有很多操 ...
- EF架构~为EF DbContext生成的实体添加注释(T5模板应用)
回到目录 相关文章系列 第八回 EF架构~将数据库注释添加导入到模型实体类中 第二十一回 EF架构~为EF DbContext生成的实体添加注释(T4模板应用) 第二十二回EF架构~为EF DbCo ...
- EF架构~数据分批批量提交
回到目录 对于大数据量提交,包括插入,更新和删除,我始终不建议用EF自带的方法,因为它会增加与数据库的交互次数,一般地,EF的一个上下文在提交时会打开一个数据连接,然后把转换成的SQL语句一条一条的发 ...
- EF架构~扩展一个分页处理大数据的方法
回到目录 最近总遇到大数据的问题,一次性处理几千万数据不实际,所以,我们需要对大数据进行分块处理,或者叫分页处理,我在EF架构里曾经写过类似的,那是在进行BulkInsert时,对大数据批量插入时候用 ...
- EF架构~CodeFirst数据迁移与防数据库删除
回到目录 本文介绍两个概念,防数据库自动删除,这是由于在code first模式下,当数据实体发生变化时,会对原来数据库进行删除,并将新数据表添加进来,但这对于我们的运营环境数据库,是万万不能接受的, ...
- EF架构~CodeFirst生产环境的Migrations
回到目录 Migrations即迁移,它是EF的code first模式出现的产物,它意思是说,将代码的变化反映到数据库上,这种反映有两种环境,一是本地开发环境,别一种是服务器的生产环境,本地开发环境 ...
随机推荐
- java连接Oracle数据库
Oracle数据库先创建一个表和添加一些数据 1.先在Oracle数据库中创建一个student表: create table student ( id ) not null primary key, ...
- easyUI的formatter使用
<table class="easyui-datagrid" style="width:400px;height:250px" data-options= ...
- c#下载共享文件夹下的文件并记录错误日志
public void Run() { //获取目标文件列表 string _ErrorMessage = ""; string _ErrorMessageFile = " ...
- swift 如何给tabBarItem的相关设计
//设置tabBarItem的title,以及点击和不点击状态图片 self.tabBarController.tabBarItem = UITabBarItem(title: "投资理财& ...
- Spring绑定表单数据
Spring提供了一些jsp页面常用的form标签,很大程度上提高了我们开发的速度,不用再一个个的标签去绑定属性,而且后台接收数据也很简单,可以直接接收object对象作为属性.官方form标签介绍的 ...
- we are the champion!!!!
- 特征哈希(Feature Hashing)
[本文链接:http://www.cnblogs.com/breezedeus/p/4114686.html,转载请注明出处] 我的博客主营地迁至github,欢迎朋友们有空去看看:http://br ...
- 首次创建maven项目的准备工作
需要JDK1.5以上.Eclipse.maven maven下载地址:http://maven.apache.org/download.cgi 1.配置环境变量新建系统变量M2_HOME 2.运行cm ...
- modal的使用
$modal是一个可以迅速创建模态窗口的服务,创建部分页,控制器,并关联他们 $modal仅有一个方法open(options) templateUrl:模态窗口的地址 template:用于显示ht ...
- Oracle 两个表之间更新的实现
Oracle 两个表之间更新的实现 来源:互联网 作者:佚名 时间:2014-04-23 21:39 Oracle中,如果跨两个表进行更新,Sql语句写成这样,Oracle 不会通过.查了资料,S ...