ASP.NET MVC使用心得:理解含義和用法
這些天開始學(xué)習(xí)asp.net mvc,用傳統(tǒng)的asp.net已經(jīng)快四的年了,剛開始接觸asp.net mvc確認(rèn)感覺有點(diǎn)不適應(yīng),主要體現(xiàn)在asp.net mvc的實(shí)現(xiàn)上。
ASP.NET MVC使用心得:?jiǎn)栴}總結(jié)
問題一:要想學(xué)習(xí)asp.net mvc,我個(gè)人覺的最重要的一步是知道m(xù)vc路由機(jī)制,傳統(tǒng)的asp.net程序要想訪問一個(gè)頁(yè)面,都是根據(jù)頁(yè)面路徑來(lái)訪問,但MVC并不能直接訪問aspx頁(yè)面。
問題二:理解MVC三部分的含義和用法。當(dāng)我們創(chuàng)建一個(gè)asp.net mvc應(yīng)用程序時(shí),系統(tǒng)會(huì)默認(rèn)生成三個(gè)文件夾:
1:Controllers,對(duì)應(yīng)MVC中的C,主要是處理所有請(qǐng)求與做出對(duì)應(yīng)的響應(yīng);
2:Models,對(duì)應(yīng)MVC中的M,相當(dāng)時(shí)我們平時(shí)創(chuàng)建工程中的實(shí)體工程,只不過在MVC中它充當(dāng)了存放數(shù)據(jù)模型的作用;
3:Views,對(duì)應(yīng)MVC中的V,這里就是存放用戶訪問的頁(yè)面文件,但是這個(gè)文件不能在瀏覽器中根據(jù)路徑訪問。
對(duì)于系統(tǒng)生成的asp.net mvc項(xiàng)目,我對(duì)其做了如下擴(kuò)展:
擴(kuò)展點(diǎn)一:系統(tǒng)之所以在web工程中直接創(chuàng)建了三個(gè)文件夾,是為了更加直觀的體現(xiàn)MVC模式,真正項(xiàng)目中我們需要把它們分開。
擴(kuò)展點(diǎn)二:MVC中重要的路由處理,默認(rèn)情況是在Global.asax文件中,我們也可以把這塊內(nèi)容獨(dú)立出來(lái)。
擴(kuò)展點(diǎn)三:把Controller類和業(yè)務(wù)邏輯分離,這里可以采用Repository模式。
ASP.NET MVC使用心得:案例DEMO
創(chuàng)建一個(gè)簡(jiǎn)單的留言簿的項(xiàng)目,數(shù)據(jù)存儲(chǔ)采用sql,本想用linq to entity,但總覺的這部分還相關(guān)不完善,且性能存在問題,故使用傳統(tǒng)ado.net實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)。下面是這個(gè)項(xiàng)目的分層。
1:GuestBook.Web,頁(yè)面表示層 ,MVC中的V。
2:GuestBook.MVC.Controller,存放項(xiàng)目所有的Controller,MVC中的C。我們知道Controller有兩個(gè)作用:第一,處理請(qǐng)求;第二,做出對(duì)應(yīng)的響應(yīng)。第二點(diǎn)就是我們平時(shí)理解的后臺(tái)功能實(shí)現(xiàn),例如數(shù)據(jù)的增刪改查等。我們可以把這部分功能與Controller分離,即所有的業(yè)務(wù)邏輯都寫在業(yè)務(wù)邏輯層,不直接依賴Controller,我們可以進(jìn)一步把這些功能點(diǎn)抽象出來(lái),讓Controller依賴一個(gè)公共的接口。這個(gè)思想我之前的一篇文章有點(diǎn)異曲同工之處:對(duì)增刪改查用面向?qū)ο筮M(jìn)行包裝
首先:創(chuàng)建一個(gè)Repository接口:IRepository.cs,里面包含些常見數(shù)據(jù)處理操作方法:這個(gè)接口是一個(gè)泛型接口,以實(shí)現(xiàn)所有實(shí)體類的通用性。
- public interface IRepository< T>
- {
- List< T> FindAllInfo();
- T GetInfo(T model);
- bool Add(T model);
- bool Delete(T model);
- bool Edit(T model);
- }
然后:實(shí)現(xiàn)一條留言的數(shù)據(jù)處理:
- public List< GuestBookInfo> FindAllInfo()
- {
- string sql = "select * from GuestBook";
- List< GuestBookInfo> list = new List< GuestBookInfo>();
- using(SqlDataReader dr=SqlHelper .ExecuteReader (conn ,CommandType .Text ,sql ))
- {
- while (dr.Read())
- {
- GuestBookInfo model = new GuestBookInfo();
- model.ID = int.Parse (dr["ID"].ToString());
- model.sTitle = dr["sTitle"].ToString();
- model.sContent = dr["sContent"].ToString();
- list.Add(model);
- }
- }
- return list ;
- }
- public GuestBookInfo GetInfo(GuestBookInfo model)
- {
- string sql = "select * from GuestBook where ID="+model.ID .ToString ();
- using (SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.Text, sql))
- {
- if (dr.Read())
- {
- model.ID = int.Parse(dr["ID"].ToString());
- model.sTitle = dr["sTitle"].ToString();
- model.sContent = dr["sContent"].ToString();
- }
- }
- return model ;
- }
- public bool Add(GuestBookInfo model)
- {
- string sql = "insert into GuestBook (sTitle,sContent) values ('" + model.sTitle + "','" + model.sContent + "')";
- int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
- if (i > 0)
- { return true; }
- return false ;
- }
- public bool Delete(GuestBookInfo model)
- {
- string sql = "delete GuestBook where ID=" + model.ID.ToString();
- int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
- if (i > 0)
- { return true; }
- return false;
- }
- public bool Edit(GuestBookInfo model)
- {
- string sql = "update GuestBook set sTitle='" + model.sTitle + "',sContent='" + model.sContent + "' where ID=" + model.ID.ToString();
- int i = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
- if (i > 0)
- { return true; }
- return false;
- }
其實(shí):Controller依賴IRepository接口。
- public class GuestBookController : System.Web.Mvc.Controller
- {
- IRepository< GuestBookInfo> inter = new BLL_GuestBook();
- public ActionResult Index()
- {
- var models = inter.FindAllInfo();
- return View("Index", models);
- }
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Create(GuestBookInfo model)
- {
- inter.Add(model );
- return RedirectToAction("Index");
- }
- public ActionResult Create()
- {
- GuestBookInfo model = new GuestBookInfo();
- return View(model );
- }
- public ActionResult Details(int id)
- {
- GuestBookInfo model=new GuestBookInfo ();
- model .ID =id;
- model =inter.GetInfo (model );
- if (string .IsNullOrEmpty (model.sTitle ))
- { return View("NotFound"); }
- else
- {
- return View("Details",model );
- }
- }
- public ActionResult Edit(int id)
- {
- GuestBookInfo model = new GuestBookInfo();
- model.ID = id;
- model = inter.GetInfo(model);
- if (string.IsNullOrEmpty(model.sTitle))
- { return View("NotFound"); }
- else
- {
- return View("Edit", model);
- }
- }
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult Edit(int id, FormCollection formValues)
- {
- GuestBookInfo model = new GuestBookInfo();
- model.ID = id;
- model = inter.GetInfo(model);
- UpdateModel(model );
- inter.Edit(model);
- return RedirectToAction("Index");
- }
- public ActionResult Delete(int id)
- {
- GuestBookInfo model = new GuestBookInfo();
- model.ID = id;
- model = inter.GetInfo(model);
- if (model == null)
- return View("NotFound");
- inter.Delete(model);
- return RedirectToAction("Index");
- }
- }
3:GuestBook.Model,MVC中的M。
4:GuestBook.RouteManager,路由管理項(xiàng)目,把路由處理從Global.asax中分離開。我們創(chuàng)建一個(gè)新類:MyMvcAppliation.cs
- public class MyMvcAppliation:HttpApplication
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- "Default", // Route name
- "{controller}/{action}/{id}", // URL with parameters
- new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
- new string[] { "GuestBook.MVC.Controller" }
- );
- }
- protected void Application_Start()
- {
- ControllerBuilder.Current.DefaultNamespaces.Add("GuestBook.MVC.Controller");
- RegisterRoutes(RouteTable.Routes);
- }
- }
5:GuestBook.Data,數(shù)據(jù)處理工具類,例如SqlHelp等等。
6:GuestBook.DAL,數(shù)據(jù)處理層。
7:GuestBook.BLL,業(yè)務(wù)邏輯層。
8:GuestBook.MyInterface,相關(guān)接口,本項(xiàng)目中包含Repository模式中的接口類。
這篇文章主要是探討了MVC項(xiàng)目的分層以及部分?jǐn)U展,歡迎大家提出更好的想法。這些就是我ASP.NET MVC的使用心得。
【編輯推薦】