前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >三层与mvc

三层与mvc

作者头像
wfaceboss
发布2019-04-08 11:33:25
6290
发布2019-04-08 11:33:25
举报
文章被收录于专栏:wfacebosswfaceboss

第一部分

模型层:

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

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

第二部分

数据访问层

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

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

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

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

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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;
        }
    }
}

第三部分  业务逻辑层

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

同样有一张基础类:

代码语言:javascript
复制
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)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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  其中一张表

代码语言:javascript
复制
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:

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Edit</title>
</head>
<body>
    <div>
        @ViewBag.Id
    </div>
</body>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档