第一部分
模型层:
(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> <a href='javascript:GoPage(" + (pageIndex - 1) +
")'>上一页</a>");
}
if (pageIndex == pageCount)
{
pager.Append("下一页 末页");
}
else
{
pager.Append("<a href='javascript:GoPage(" + (pageIndex + 1) +
")'>下一页</a> <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>