由于一直开发的小项目,所以基本都使用EF+MVC来搭建网站,但是最近心血来潮学习了一下spring.net,但是又觉得里面针对实体的控制又太麻烦(不知道是不是还没有深入了解哈),所以加上了EF来做数据持久层(我习惯是先搭建数据库,然后通过数据表来生成实体类,不知道高手们是如何做的哈,这里通过ef就很方便的生成了所有的实体类,而且以后更新删除都很方便),下面开始:
第一步:创建如下6个项目,IDao是接口,BLL里要调用的,SqlDao操作数据(EF,数据层);
第二步,在BLL里添加Spring.Core,通过控制它添加比较方便,右键引用点击“管理NuGet程序包”,在Test.Web里添加如图上的所有引用。
第三步,在SqlDao里添加EF,并创建DaoBase(里面主要是操作数据库的方法,都是用EF实现),创建UserDao(其实这个可以不用创建);
第四步,先把所有接口里的方法写好,然后DaoBase类集成IDao类的接口来实现所有方法,并且是通过泛型实现,如下:
DaoBase
public class DaoBase<T> : IDao<T> where T : class, new()
{
DbContext da =new caiwuEntities();
public List<T> AddEntity(List<T> models)
{
da.Set<T>().AddRange(models);
da.SaveChanges();
return models;
}
public T AddEntity(T model)
{
da.Set<T>().Add(model);
da.SaveChanges();
return model;
}
public bool DeleteEntity(T model)
{
da.Set<T>().Remove(model);
return da.SaveChanges() > 0;
}
public bool EditEntity(T model)
{
da.Entry(model).State= EntityState.Modified;
return da.SaveChanges() > 0;
}
public IQueryable<T> GetDataList<TS>(Expression<Func<T, bool>> qwhere, bool isDesc,Expression<Func<T,TS>> order=null)
{
if (order != null)
return isDesc ? da.Set<T>().Where(qwhere).OrderByDescending(order) : da.Set<T>().Where(qwhere).OrderBy(order);
else
{
return da.Set<T>().Where(qwhere);
}
}
public IQueryable<T> GetDataPage<TS>(int index, int pages, Expression<Func<T, bool>> where, bool isDesc, Expression<Func<T, TS>> order = null)
{
return GetDataList(where, isDesc, order).Skip((index-1)*pages).Take(pages);
}
}
其中的方法我就不一一来讲解了,这里主要是说框架。
第五步:配置web.config,这里配置后,只要网站启动首先要加载完Test.SqlDao这个动态库以及动态创建Test.SqlDao.UserDao<Test.SqlDao.New_AirData>这个类:
<configSections>
<!--配置spring-->
<!--下面几行是必须的,注册spring模块-->
<sectionGroup name="spring">
<!--这里的context属性和winform程序不一样,因为是mvc5web程序所以改为MvcContextHandler-->
<section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc5" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<!--这里开始注入内容-->
<spring>
<context>
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<description>控制反转配置</description>
<object id="DaoBase" type="Test.SqlDao.UserDao<Test.SqlDao.New_AirData>, Test.SqlDao" />
</objects>
</spring>
第六步:在BLL业务层里写spring.net注入的代码,这里我只写了获取数据的方法实现:
BLLServer
public class BllServer<T> : IBLL.BllServer<T> where T : class, new()
{
public BllServer()
{
IApplicationContext ctx = ContextRegistry.GetContext();//spring.net开始注入
var dk = ctx.GetObject("DaoBase");//获取配置里制定的类
dao = ctx.GetObject("DaoBase") as IDao.IDao<T>;//通过接口转换成实现类
}
public IDao.IDao<T> dao { get; set; }
public List<T> AddEntity(List<T> models)
{
throw new NotImplementedException();
}
public T AddEntity(T model)
{
throw new NotImplementedException();
}
public bool DeleteEntity(T model)
{
throw new NotImplementedException();
}
public bool EditEntity(T model)
{
throw new NotImplementedException();
}
public IQueryable<T> Getdata<TS>(Expression<Func<T, bool>> where, bool isDesc, Expression<Func<T, TS>> order=null)
{
return dao.GetDataList(where, isDesc, order);
}
public IQueryable<T> GetDataPage<TS>(int index, int pages, Expression<Func<T, bool>> where, bool isDesc, Expression<Func<T, TS>> order = null)
{
throw new NotImplementedException();
}
}
第七步:UserBLL调用,这里很方便,如果数据库添加一张表,那么只要更新ef实体,那么在添加一行如下对应的代码就可以了(还有配置web.config里):
public class NewAirData : BllServer<SqlDao.New_AirData>{ }
第八步:在控制器里调用:
public ActionResult Index()
{
var dt= new BLL.NewAirData().Getdata(p=>true,false,p=>p.New_AirDataID).FirstOrDefault();
return View(dt);
}
作者:403648571@qq.com,QQ群:695080688