MVC 3中的新思考.通用模块完成增删改查功能

前言:

虽然.NET MVC给了我极大的方便,但是如果功能块多了,增删改查这些功能写写还是比较头疼的

所以我想了一个办法

通过继承公共模块的增删改查来实现自己的偷懒思想.

现在我给出通用公共部分代码

希望大家一起讨论这个功能的实现是否完美,还有资源耗费的相关问题.

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.ComponentModel;
 7 using System.ComponentModel.DataAnnotations;
 8 
 9 namespace Base
10 {
11     /// <summary>
12     /// 基本模型 所有模型继承他
13     /// </summary>
14     public class BaseModel
15     {
16         [Key]
17         public long Idx { get; set; }
18 
19         public DateTime EventDateTiem { get; set; }
20     }
21 
22 
23     public class BaseController<T> : Controller
24         where T : BaseModel, new()
25     {
26         #region 私有变量
27         private class Entities : System.Data.Entity.DbContext
28         {
29             public System.Data.Entity.DbSet<T> TModel { get; set; }
30         }
31         private Entities db = new Entities();
32         private dynamic TModel = new T();
33         #endregion
34 
35         public BaseController()
36         {
37         }
38 
39         #region 增删改查
40         public virtual ActionResult View(int Idx = 0)
41         {
42             var ViewModel = db.TModel.FirstOrDefault(p => p.Idx == Idx);
43             return View(ViewModel);
44         }
45 
46         public virtual ActionResult Create()
47         {
48             return View();
49         }
50 
51         [HttpPost]
52         public virtual ActionResult Create(T Model)
53         {
54 
55             if (ModelState.IsValid)
56             {
57                 db.TModel.Add(Model);
58                 db.SaveChanges();
59                 return Redirect("/" + TModel.GetType().Name + "/List?Message=添加成功");
60             }
61             return View(Model);
62         }
63 
64         public virtual string Delete(int Idx = 0)
65         {
66             return "Delete";
67         }
68         #endregion
69 
70     }
71 }
复制代码

以上我只实现了一个添加

在实际控制器中使用继承方法

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Base.BaseController<Models.TestTable>
    {

    }
}
复制代码

将基本控制器继承给需要的Home控制器并且设定这个控制器所需要的模型对象

而如果你有特殊的需求定制某些操作可以通过

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Base.BaseController<Models.TestTable>
    {
        public override ActionResult Create()
        {
            return base.Create();
        }
    }
}
复制代码

这样来实现.

我通过反射取得提交的模型名字

并且在控制器中加入了数据操作对象

操作可以通过后期继承进行个性化定制

不过这好像破坏了MVC的规范

大家都来发表一下自己的看法....

我写了个范例

https://files.cnblogs.com/mota/MvcApplication1.zip

大家可以下载过去看一下

环境:VS 2012 RTM + WIN 7 64

MVC 3,EF 5.0

---------------------------

修正

1.TModel.GetType() 应为 TModel.GetType().Name

2.基本控制器中我未写资源释放相关代码,见谅,我仅此提供一种新的思路与想法.

原文地址:https://www.cnblogs.com/Leo_wl/p/2846820.html