利用Repository模式统一业务层接口,对UI层进行统一调用。下面是我的一个构想
1.Repository代码
1 public interface BaseBizObject<TModel>
2 {
3 List<TModel> GetObjects(NameValueCollection collection);
4 TModel GetObject(object id);
5 void Add(TModel model);
6 void Update(TModel model);
7 void LogicDelete(object id);
8 void Delete(Expression<Func<TModel, bool>> predicate);
9 }
2 {
3 List<TModel> GetObjects(NameValueCollection collection);
4 TModel GetObject(object id);
5 void Add(TModel model);
6 void Update(TModel model);
7 void LogicDelete(object id);
8 void Delete(Expression<Func<TModel, bool>> predicate);
9 }
2.继承类代码 ,对接口的实现。了解的话可以忽略这段代码,重点是在UI层的应用
1 public class BaseBizObjectImpl<TModel> : BaseBizObject<TModel> where TModel : class
2 {
3 public virtual List<TModel> GetObjects(NameValueCollection collection)
4 {
5 throw new System.NotImplementedException();
6 }
7
8 public TModel GetObject(object id)
9 {
10 return DaoAccess.Instance<TModel>().GetById(Int32.Parse(id.ToString()));
11 }
12
13 public void Add(TModel model)
14 {
15 DaoAccess.Instance<TModel>().Add(model);
16 }
17
18 public void Update(TModel model)
19 {
20 DaoAccess.Instance<TModel>().SubmitChanges();
21 }
22
23 public void LogicDelete(object id)
24 {
25 DaoAccess.Instance<TModel>().SubmitChanges();
26 }
27
28 public void Delete(Expression<Func<TModel, bool>> predicate)
29 {
30 DaoAccess.Instance<TModel>().Delete(predicate);
31 }
32 }
2 {
3 public virtual List<TModel> GetObjects(NameValueCollection collection)
4 {
5 throw new System.NotImplementedException();
6 }
7
8 public TModel GetObject(object id)
9 {
10 return DaoAccess.Instance<TModel>().GetById(Int32.Parse(id.ToString()));
11 }
12
13 public void Add(TModel model)
14 {
15 DaoAccess.Instance<TModel>().Add(model);
16 }
17
18 public void Update(TModel model)
19 {
20 DaoAccess.Instance<TModel>().SubmitChanges();
21 }
22
23 public void LogicDelete(object id)
24 {
25 DaoAccess.Instance<TModel>().SubmitChanges();
26 }
27
28 public void Delete(Expression<Func<TModel, bool>> predicate)
29 {
30 DaoAccess.Instance<TModel>().Delete(predicate);
31 }
32 }
Catalog 继承自 BaseBizObjectImpl
1 public class CatalogMgr : BaseBizObjectImpl<Catalog>
2 {
3 public override List<Catalog> GetObjects(NameValueCollection collection)
4 {
5 // collection 存放动态查询字段
6 return DaoAccess.Instance<Catalog>().GetAll().ToList();
7 }
8 }
2 {
3 public override List<Catalog> GetObjects(NameValueCollection collection)
4 {
5 // collection 存放动态查询字段
6 return DaoAccess.Instance<Catalog>().GetAll().ToList();
7 }
8 }
3.UI层编辑页面的基类
1 public abstract class BaseEditPage<TBiz, TEntity> : System.Web.UI.Page where TBiz : BaseBizObject<TEntity>, new()
2 {
3 public BaseEditPage()
4 {
5 //
6 //TODO: 在此处添加构造函数逻辑
7 //
8 }
9
10 protected bool IsNew { get; set; }
11
12 protected abstract void PageInit();
13
14 protected abstract TEntity UpdateModel();
15
16 protected abstract void UpdateForm(TEntity entity);
17
18 protected override void OnLoad(System.EventArgs e)
19 {
20 base.OnLoad(e);
21 if (!IsPostBack)
22 {
23 PageInit();
24 object id = Request.QueryString["pkid"];
25 if (id == null)
26 {
27 IsNew = true;
28 }
29 else
30 {
31 TBiz obj = new TBiz();
32 TEntity entity = obj.GetObject(id);
33 UpdateForm(entity);
34 }
35 }
36 }
2 {
3 public BaseEditPage()
4 {
5 //
6 //TODO: 在此处添加构造函数逻辑
7 //
8 }
9
10 protected bool IsNew { get; set; }
11
12 protected abstract void PageInit();
13
14 protected abstract TEntity UpdateModel();
15
16 protected abstract void UpdateForm(TEntity entity);
17
18 protected override void OnLoad(System.EventArgs e)
19 {
20 base.OnLoad(e);
21 if (!IsPostBack)
22 {
23 PageInit();
24 object id = Request.QueryString["pkid"];
25 if (id == null)
26 {
27 IsNew = true;
28 }
29 else
30 {
31 TBiz obj = new TBiz();
32 TEntity entity = obj.GetObject(id);
33 UpdateForm(entity);
34 }
35 }
36 }
4.Catalog编辑页面
1 public partial class Admin_Catalog_EditCatalog : BaseEditPage<CatalogMgr,Catalog>
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7
8 protected override void PageInit()
9 {
10 IsNew = false;
11 }
12
13 protected override Catalog UpdateModel()
14 {
15 return ReflectForm<Catalog>.UpdateModel(Request.Form);
16 }
17
18 protected override void UpdateForm(Catalog entity)
19 {
20 ReflectForm<Catalog>.UpdateForm(this.form1,entity);
21 }
22
23 protected void btnSave_Click(object sender, EventArgs e)
24 {
25 if (!IsNew)
26 {
27 new CatalogMgr().Update(UpdateModel());
28 }
29 else
30 {
31 new CatalogMgr().Add(UpdateModel());
32 }
33 }
34 protected void btnCancel_Click(object sender, EventArgs e)
35 {
36 Response.Redirect("ListCatalog.aspx");
37 }
38 protected void btnDel_Click(object sender, EventArgs e)
39 {
40 }
41 }
42
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5
6 }
7
8 protected override void PageInit()
9 {
10 IsNew = false;
11 }
12
13 protected override Catalog UpdateModel()
14 {
15 return ReflectForm<Catalog>.UpdateModel(Request.Form);
16 }
17
18 protected override void UpdateForm(Catalog entity)
19 {
20 ReflectForm<Catalog>.UpdateForm(this.form1,entity);
21 }
22
23 protected void btnSave_Click(object sender, EventArgs e)
24 {
25 if (!IsNew)
26 {
27 new CatalogMgr().Update(UpdateModel());
28 }
29 else
30 {
31 new CatalogMgr().Add(UpdateModel());
32 }
33 }
34 protected void btnCancel_Click(object sender, EventArgs e)
35 {
36 Response.Redirect("ListCatalog.aspx");
37 }
38 protected void btnDel_Click(object sender, EventArgs e)
39 {
40 }
41 }
42
如此如此这般这般,编辑页面的代码少了很多。列表页面同理可得。可能会碰到一个页面会编辑多个MODEL,这里提供一个思路。