当前位置:编程学习 > JS >>

ExtJS4.1+MVC3+Spring.NET1.3+EF5 整合五:数据访问层

本篇将讲解数据访问接口及实现层代码的编写。数据访问层一般都是原子操作,由于项目较小,涉及到的数据表也很少。
 
我先把数据访问接口和接口实现上个图,里面只是本篇要说的几个类,其它类以后再实现。
\  \
  
1 数据访问接口
这里我根据实际情况抽象出了父接口,对于其他IDao直接继承该接口即可。主要是考虑项目比较简单,这样设计可以简化代码。
 
但有个问题,虽然Dao依赖于持久层框架(EF或NHibernate),但不应该把这个依赖使IDao接口表现出来,否则Service层也将依赖于持久层框架,显然这种设计不是良好的设计(当然,如果你非要这么做也不是不可以)。所以,IDao不应该与EF框架耦合在一起,这也是我把IDao层所有接口的参数都是string、int等类型的原因,而没有设计成Expression<Func<X,Y>>类型。
 
1.1 IDao 接口
其中的Query方法主要是数据分页的实现:
 
[csharp] 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using MESE.EF5;  
  
namespace MESE.IDao  
{  
    public interface IDao<T>  
        where T : class  
    {  
        IList<T> Query(string sql, int pageIndex, int pageSize, out int recordCount);  
        IList<T> QueryAll();  
        bool Exists(string code);  
        T Read(string code);  
        bool Add(T entity);  
        bool Update(T entity);  
        bool Delete(T entity);  
        bool Delete(string code);  
        int DeleteByKeys(IList<string> keys);  
    }  
}  
 
这样,其它访问层接口直接继承就可以了,直接上代码:
1.2 IAdminDao接口
[csharp]  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using MESE.EF5;  
  
namespace MESE.IDao  
{  
    public interface IAdminDao : IDao<Admin>  
    { }  
}  
1.3 IRoleDao接口:
[csharp]  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using MESE.EF5;  
  
namespace MESE.IDao  
{  
    public interface IRoleDao : IDao<Role>  
    { }  
}  
 
1.4 ICategoryDao接口
根据实际需要,扩展了一个方法:
 
[csharp]  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using MESE.EF5;  
  
namespace MESE.IDao  
{  
    public interface ICategoryDao : IDao<Category>  
    {  
        IList<Category> QueryByParent(string parent);  
    }  
}  
2 数据访问实现类
经过上面接口的定义,现在开始实现这些接口,与IDao设计类似,我们也写个DaoBase类,以把封装常用的原子操作。
 
这里要注意的是,由于使用了EF5持久层框架,结合上篇编写的DbContext管理类,Dao中获取和创建DbContext都要通过DbContextFactory类来获取。
 
2.1 DaoBase类
[csharp] 
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Linq.Expressions;  
using System.Text;  
using System.Data.Entity;  
using Simple.Web.EntityFramework5;  
  
namespace MESE.Dao  
{  
    public class DaoBase<T>  
        where T : class  
    {  
        public DbContext DbContext  
        {  
            get  
            {  
                return DbContextFactory.GetContext();  
            }  
        }  
  
        public virtual IList<T> Query(string sql, int pageIndex, int pageSize, out int recordCount)  
        {  
            var list = this.DbContext.Set<T>().SqlQuery(sql);  
            recordCount = list.Count();  
            return list.Skip(pageIndex * pageSize).Take(pageSize).ToList();  
        }  
        public virtual IList<T> QueryAll()  
        {  
            return this.DbContext.Set<T>().AsNoTracking().ToList();  
        }  
        public virtual IList<T> QueryItems(Expression<Func<T, bool>> predicate)  
        {  
            return this.DbContext.Set<T>().AsNoTracking().Where(predicate).ToList();  
        }  
        protected virtual bool Exists(Expression<Func<T, bool>> predicate)  
        {  
            return this.DbContext.Set<T>().AsNoTracking().Where(predicate).Any();  
        }  
        protected virtual T Read(Expression<Func<T, bool>> predicate)  
        {  
            return this.DbContext.Set<T>().AsNoTracking().Single(predicate);  
        }  
        public virtual bool Add(T entity)  
        {  
            this.DbContext.Set<T>().Add(entity);  
            return this.DbContext.SaveChanges() > 0;  
        }  
        public virtual bool Update(T entity)  
  &nbs
补充:web前端 , JavaScript ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,