zoukankan      html  css  js  c++  java
  • ASP.NET MVC系列 框架搭建(一)之仓储层的搭建

    大神勿喷,小神默默学。

    会了就是不值一提的东西,不会就是绝对的高大上。

    最后上传源码。希望能给读者带来一些新的认识及知识。

    还没上过头条。。各位大神,请点支持一下小弟。

    陆续更新。更新到你会为止!!

    我不是话唠,我只把重点点出来,细枝末节的不懂的可以留言探讨。这个系列的最后,我会再统一的把大家的问题,列在一篇新的Blog。工作需要规划,写博客也是如此。

    仓储层:待优化

    基接口:约束

    子接口:实现基接口。进一步约束子仓储中特殊的方法。

    *基仓储:具体实现,子类继承接口的方法。

        这步最难,最重要!

        ①db不能直接点出具体的Model,只能db.CreateObjectSet<T>().AddObject(entity);将实体“附加”到上下文。

        ②where和order方法需要传lamada。各自类型问题。where<T,bool>   order<T,S>的使用。

        ③Select时候IEumarable<T>转换IQueryable<T>需要AsQueryable()方法。

    子仓储:只写除了基类和接口的方法之外外特殊的需求方法。比如多表。

    以UserInforRespository为例:

    IBaseRespository.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    //添加System.Data.Entity引用
    namespace LKBS.CommunicationPlatform.IDAL
    {
      public interface IBaseRespository<T> where T : class ,new()
      {
        //接口不需要上下文db
        // int rowcount = 0;
        //public 对接口方法无效
        bool AddEntity(T entity);
        bool DeleteEntity(T entity);
        bool UpdateEntity(T entity);
        T SelectOneEntity(Func<T, bool> wherelamda);
        IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda);
    
        IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount);
      }
    }

    BaseRespository.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.Data.Entity;
    using LKBS.CommunicationPlatform.Model;
    using LKBS.CommunicationPlatform.IDAL;
    using System.Data;
    
    namespace LKBS.CommunicationPlatform.DAL
    {
       public class BaseRespository<T> where T : class,new()
       {
          ModelContainer db = new ModelContainer();
    
          /// <summary>
          /// 增加
          /// </summary>
          /// <param name="entity">增加实体,实体没赋的属性为NULL</param>
          /// <returns></returns>
          public bool AddEntity(T entity)
          {
             db.CreateObjectSet<T>().AddObject(entity);
             //db.ObjectStateManager.ChangeObjectState(entity, EntityState.Added);
             db.SaveChanges();
             return true;
          }
          /// <summary>
          /// 删除
          /// </summary>
          /// <param name="entity">方便附加到上下文一个实体,传入实体,用到只有ID</param>
          /// <returns></returns>
          public bool DeleteEntity(T entity)
          {
             //var temp=  db.UserInfor.Where(userinfor=>userinfor.ID>=id).FirstOrDefault<UserInfor>();
             //db.UserInfor.Attach(userinfor);
             //db.ObjectStateManager.ChangeObjectState(userinfor, EntityState.Deleted);
             db.CreateObjectSet<T>().AddObject(entity);
             db.ObjectStateManager.ChangeObjectState(entity, EntityState.Deleted);
             db.SaveChanges();
             return true;
          }
          /// <summary>
          ////// </summary>
          /// <param name="entity"></param>
          /// <returns></returns>
          public bool UpdateEntity(T entity)
          {
             //db.UserInfor.Attach(userinfor);
             //db.ObjectStateManager.ChangeObjectState(userinfor, EntityState.Modified);
             db.CreateObjectSet<T>().AddObject(entity);
             db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
             db.SaveChanges();
             return true;
          }
          /// <summary>
          /// 单个实体
          /// </summary>
          /// <param name="wherelamda"></param>
          /// <returns></returns>
          public T SelectOneEntity(Func<T, bool> wherelamda)
          {
             var rowTemp = db.CreateObjectSet<T>().Where<T>(wherelamda).FirstOrDefault<T>();
             return rowTemp;
          }
          /// <summary>
          /// 查询 整张表或者多个实体 
          /// </summary>
          /// <returns></returns>
          public IQueryable<T> SelectAllEntityList(Func<T, bool> wherelamda)
          {
             var rowsTemp = db.CreateObjectSet<T>().Where<T>(wherelamda);
             return rowsTemp.AsQueryable<T>();
          }
          int rowcount = 0;
          //泛型方法 <S>就是约束参数的类型,传什么参数,“自动识别”<S>. 定义需要<S>,调用该方法可以省略 :因为传的参数就已确定<S>的类型
          public IQueryable<T> SelectPageEntityList<S>(Func<T, S> orderLamda, Func<T, bool> whereLamda, int pageIndex, int pageSize, bool isAsc, out int rowcount)
          {//非页数pageCount,是总行数
             //IEnumerable<>
             var temp = db.CreateObjectSet<T>().Where<T>(whereLamda);
             rowcount = temp.Count<T>();
             if (isAsc)
             {
                //IQueryable
                IEnumerable<T> entityPageList =
                temp
                .OrderBy<T, S>(orderLamda)
                .Skip<T>((pageIndex - 1) * pageSize).
                Take<T>(pageSize);
                //pageCount=db.UserInfor.
                return entityPageList.AsQueryable();
             }
             else
             {
                IEnumerable<T> entityPageList =
                   //db.UserInfor
                   //.Where<UserInfor>(whereLamda)//u => u.ID > 0
                temp
                .OrderByDescending<T, S>(orderLamda)//u => u.ID
                .Skip<T>((pageIndex - 1) * pageSize).
                Take<T>(pageSize);
                //pageCount=db.UserInfor.
                return entityPageList.AsQueryable();
             }
    
          }
       }
    }

    IUserInforRespository.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using LKBS.CommunicationPlatform.Model;
    
    namespace LKBS.CommunicationPlatform.IDAL
    {
     public interface IUserInforRespository:IBaseRespository<UserInfor>
      {
      }
    }

    UserInforRespository.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using LKBS.CommunicationPlatform.Model;
    using System.Data;
    using LKBS.CommunicationPlatform.IDAL;
    
    
    namespace LKBS.CommunicationPlatform.DAL
    {
      public class UserInforRespository:BaseRespository<UserInfor>,IUserInforRespository
      {
      }
    }
  • 相关阅读:
    符合Web标准的表格——CSS表格
    导航 Jquery
    【IOS学习】之三、图像视图&文本字段
    【VC++积累】之四、文件删除,复制
    Xcode 4.4中LLVM compiler 4.0带来的ObjectiveC新语法特性
    最快的存储过程分页 50W
    【网络编程】之十一、重叠IO Overlapped IO 完成例程
    OD使用教程15 调试篇15
    线性表12|循环链表 数据结构和算法17
    线性表13|约瑟夫问题 数据结构和算法18
  • 原文地址:https://www.cnblogs.com/leee/p/4513420.html
Copyright © 2011-2022 走看看