天天看點

spring.net+EF6+MVC 架構的搭建。

   由于一直開發的小項目,是以基本都使用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&lt;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&lt;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);
        }
           

作者:[email protected],QQ群:695080688 

spring.net+EF6+MVC 架構的搭建。