当前位置: 首页 > news >正文

三层与mvc

三层与mvc

第一部分

模型层:

(1)实体属性   数据库字段

(2)数据库上下文类  dbContext 封装ado.net

第二部分

数据访问层

说明,每一张表对应有crud综合分析可以得知区别在于对应的类型不同以及一些参数不一样,

故考虑,对于类型的不同使用泛型进行封装,

          对于不同的参数使用父类定义虚方法子类重写父类的方法解决。

因此,提出一个公共类出来:

using System;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using Model;

namespace Dal
{
    //类型不一样可以使用泛型封装,对于某些参数不一样可以在父类中定义为虚方法在子类中重写
    public abstract partial class BaseDal<T>//泛型类
        where T : class//class标识为引用类型
    {
        DbContext dbContext = new MyContext();//生成数据库

        public IQueryable<T> GetList(int pageSize, int pageIndex)//查询所用
        {
            return dbContext.Set<T>()
                .OrderByDescending(GetKey()) //u=>u.Tid
                .Skip((pageIndex - 1) * pageSize) //3,5   10
                .Take(pageSize);
        }

        public T GetById(int id)//查询一条
        {
            return dbContext.Set<T>()
                .Where(GetByIdKey(id))
                .FirstOrDefault();
        }

        public int Add(T bookType)//添加
        {
            dbContext.Set<T>().Add(bookType);
            return dbContext.SaveChanges();
        }

        public int Edit(T bookType)//修改
        {
            dbContext.Set<T>().Attach(bookType);
            dbContext.Entry(bookType).State = EntityState.Modified;
            return dbContext.SaveChanges();
        }

        public int Remove(int id)//删除
        {
            var bookType = GetById(id);
            dbContext.Set<T>().Remove(bookType);
            return dbContext.SaveChanges();
        }

        public abstract Expression<Func<T, int>> GetKey();  //参数不一样的定义
        public abstract Expression<Func<T, bool>> GetByIdKey(int id);

        public int GetCount()
        {
            return dbContext.Set<T>().Count();
        }
    }
}

具体表的crud:

(1)

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace Dal
{
    /// <summary>
    /// 其中一张表的crud操作
    /// </summary>
    public partial class BookInfoDal:BaseDal<BookInfo>
    {
        public override Expression<Func<BookInfo, int>> GetKey()//重写父类
        {
            return u => u.BookId;
        }

        public override Expression<Func<BookInfo, bool>> GetByIdKey(int id)
        {
            return u => u.BookId == id;
        }
    }
}

(2)

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace Dal
{
    /// <summary>
    /// 另外一张表的操作
    /// </summary>
    public partial class BookTypeDal : BaseDal<BookType>
    {
        public override Expression<Func<BookType, bool>> GetByIdKey(int id)
        {
            return u => u.TypeId == id;
        }

        public override Expression<Func<BookType, int>> GetKey()
        {
            return u => u.TypeId;
        }
    }
}

第三部分  业务逻辑层

对数据访问层的进一步封装

同样有一张基础类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dal;

namespace Bll
{
    public abstract partial class BaseBll<T>
        where T:class 
    {
        private BaseDal<T> dal;
        public abstract BaseDal<T> GetDal();

        public BaseBll()
        {
            dal = GetDal();
        }

        public IQueryable<T> GetList(int pageSize, int pageIndex)
        {
            return dal.GetList(pageSize, pageIndex);
        }

        public T GetById(int id)
        {
            return dal.GetById(id);
        }

        public bool Add(T t)
        {
            return dal.Add(t) > 0;
        }

        public bool Edit(T t)
        {
            return dal.Edit(t) > 0;
        }

        public bool Remove(int id)
        {
            return dal.Remove(id) > 0;
        }

        public int GetCount()
        {
            return dal.GetCount();
        }
    }
}

具体的表

(1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dal;
using Model;

namespace Bll
{
    /// <summary>
    /// 其中一张表
    /// </summary>
    public partial class BookInfoBll:BaseBll<BookInfo>
    {
        public override BaseDal<BookInfo> GetDal()
        {
            return new BookInfoDal();
        }
    }
}

(2)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dal;
using Model;

namespace Bll
{
    /// <summary>
    /// 另外一张表
    /// </summary>
    public partial class BookTypeBll:BaseBll<BookType>
    {
        public override BaseDal<BookType> GetDal()
        {
            return new BookTypeDal();
        }
    }
}

第四部分  展示层  UI  有mvc组成

(1)controller  其中一张表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using Bll;
using Model;

namespace UI.Controllers
{
    public class BookInfoController : Controller
    {
        BookInfoBll bookInfoBll=new BookInfoBll();
        BookTypeBll bookTypeBll=new BookTypeBll();
        //
        // GET: /BookInfo/

        public ActionResult Index()
        {
            //ViewData.Model = bookInfoBll.GetList(10, 1);
            return View();
        }

        public ActionResult LoadList(int pageSize,int pageIndex)
        {
            var list= bookInfoBll
                .GetList(pageSize, pageIndex)
                .Select(u=>new//select  使用匿名对象筛选一部分需要的数据(其中不能出现导航属性)
                {
                    Id=u.BookId,
                    Title=u.BookTitle,
                    TypeTitle=u.BookType.TypeTitle
                })
                .ToList();

            int rowsCount = bookInfoBll.GetCount();
            int pageCount = Convert.ToInt32(Math.Ceiling(rowsCount*1.0/pageSize));

            StringBuilder pager = new StringBuilder();
            if (pageIndex == 1)
            {
                pager.Append("首页 上一页");
            }
            else
            {
                pager.Append("<a href='javascript:GoPage(1)'>首页</a>&nbsp;<a href='javascript:GoPage(" + (pageIndex - 1) +
                             ")'>上一页</a>");
            }

            if (pageIndex == pageCount)
            {
                pager.Append("下一页 末页");
            }
            else
            {
                pager.Append("<a href='javascript:GoPage(" + (pageIndex + 1) +
                             ")'>下一页</a>&nbsp;<a href='javascript:GoPage(" + pageCount +
                             ")'>末页</a>");
            }

            var temp = new    //创建新对象封装多个需要返回的数据
            {
                list = list,
                pager = pager.ToString()
            };

            return Json(temp,JsonRequestBehavior.AllowGet);
        }


        public ActionResult Add()
        {
            List<SelectListItem> list=new List<SelectListItem>();
            var list1 = bookTypeBll.GetList(100, 1);
            foreach (var item in list1)
            {
                list.Add(new SelectListItem()
                {
                    Text = item.TypeTitle,
                    Value = item.TypeId.ToString()
                });
            }
            ViewBag.TypeList = list;

            return View();
        }
        [HttpPost]
        public ActionResult Add(BookInfo bookInfo)
        {
            bookInfoBll.Add(bookInfo);

            return Redirect(Url.Action("Index"));
        }

        public ActionResult Edit(int id)
        {
            ViewBag.Id = id;
            return View();
        }
    }
}

 

(2)view  其中一张表

   (2.1)index:

@model IQueryable<Model.BookInfo>
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script>
        $(function() {
            LoadList(1);
        });

        function GoPage(pageIndex) {
            LoadList(pageIndex);
        }

        function LoadList(pageIndex) {
            $.getJSON(
                '@Url.Action("LoadList","BookInfo")',
                {
                    pageSize: 2,
                    pageIndex:pageIndex
                },
                function(list1) {
                    var list = $('#list');
                    list.empty();
                    $.each(list1.list, function(index,item) {
                        list.append('<tr><td>'+item.Id+'</td>' +
                            '<td>'+item.Title+'</td>' +
                            '<td>'+item.TypeTitle+'</td>' +
                            '<td><a href="@Url.Action("Edit","BookInfo")?id=' + item.Id + '">修改</a></td>' +
                            '<td></td>' +
                            '</tr>');
                    });

                    list.append('<tr><td colspan=3>'+list1.pager+'</td></tr>');
                }
            );
        }
    </script>

</head>
<body>
    <div>
        @Html.ActionLink("添加","Add","BookInfo")
        <hr/>
        <table border="1">
            <tr>
                <th width="100">编号</th>
                <th width="100">标题</th>
                <th width="100">分类名称</th>
                <th width="100">修改</th>
                <th width="100">删除</th>
            </tr>
            <tbody id="list">
                
            </tbody>
            @*@foreach (var item in Model)
            {
                <tr>
                    <td>@item.BookId</td>
                    <td>@item.BookTitle</td>
                    <td>@item.BookType.TypeTitle</td>
                </tr>
            }*@
        </table>
    </div>
</body>
</html>

(2.2)Add

@model Model.BookInfo
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Add</title>
</head>
<body>
    <div>
        @using (Html.BeginForm("Add", "BookInfo", FormMethod.Post))
        {
            @Html.TextBoxFor(u=>u.BookTitle)
            <br/>
            @Html.TextBoxFor(u=>u.BookContent)
            <br/>
            @Html.DropDownListFor(u=>u.TypeId,(IEnumerable<SelectListItem>)ViewBag.TypeList)
            <br/>
            <input type="submit" value="添加"/>
        }
    </div>
</body>
</html>

(2.3)Edit

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <div>
        @ViewBag.Id
    </div>
</body>
</html>

 

posted on 2017-03-03 15:34 WFaceBoss 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/wfaceboss/p/6496934.html

相关文章:

  • ERPS实例演示
  • SuSE Linux 开启VNC服务
  • HDU1572:下沙小面的(2)(DFS)
  • Android-实现Animation everywhere
  • 使用agvtool更改app version/build
  • 关于position的小总结
  • 《剑指offer》二叉树镜像
  • 走向全栈之MongoDB的使用
  • RN开发之如何升级自己的本地RN项目
  • Android 倒计时的五种实现方式
  • Linux运维工程师如何找一份好工作?
  • 编码小结2
  • Nginx | 负载均衡(一)
  • VS链接错误: LNIK1123
  • Angular 2 DI - IoC DI - 1
  • 【159天】尚学堂高琪Java300集视频精华笔记(128)
  • eclipse的离线汉化
  • JS变量作用域
  • Mysql5.6主从复制
  • Phpstorm怎样批量删除空行?
  • Python中eval与exec的使用及区别
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • 安装python包到指定虚拟环境
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 记一次和乔布斯合作最难忘的经历
  • 记一次删除Git记录中的大文件的过程
  • 两列自适应布局方案整理
  • 深度学习入门:10门免费线上课程推荐
  • 体验javascript之美-第五课 匿名函数自执行和闭包是一回事儿吗?
  • 由插件封装引出的一丢丢思考
  • Java性能优化之JVM GC(垃圾回收机制)
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • #Java第九次作业--输入输出流和文件操作
  • #pragma预处理命令
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • (1)(1.9) MSP (version 4.2)
  • (1)安装hadoop之虚拟机准备(配置IP与主机名)
  • (1)常见O(n^2)排序算法解析
  • (3)(3.2) MAVLink2数据包签名(安全)
  • (4.10~4.16)
  • (aiohttp-asyncio-FFmpeg-Docker-SRS)实现异步摄像头转码服务器
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (C++17) optional的使用
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (附源码)php新闻发布平台 毕业设计 141646
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (简单有案例)前端实现主题切换、动态换肤的两种简单方式
  • (免费分享)基于springboot,vue疗养中心管理系统
  • (七)理解angular中的module和injector,即依赖注入
  • (算法设计与分析)第一章算法概述-习题
  • (一)eclipse Dynamic web project 工程目录以及文件路径问题