近半年MVC使用後的一些習慣

來源:互聯網
上載者:User

標籤:

半年前接新項目, 來了一個前端, 由於只有我前後台都會, 就做業務層+輔助最上層顯示, 於是我決定使用MVC

上面那句無關緊要的話讓我改了好多遍, 轉載請註明出處: http://www.cnblogs.com/zaiyuzhong/p/personal-opinions-of-mvc.html

 

1. 查詢條件抽象為一個類

項目中有後台管理, 無非查增刪改嘛, 總有一些有很多參數的查詢, 比如

  public IEnumerable<Model> Select(各種參數);

我喜歡寫成這樣:

  public IEnumerable<Model> Select(Model, 增量);

增量常常是 DateTime 類型的, So 你懂我意思.

好處: a. 一個 Model 除了顯示列表,添加/修改之外還可以作為查詢條件的封裝;

    b. 從修改頁面跳回列表時可以根據修改後的Model再尋找, 不至於跳回列表再重新搜尋;

    c. 可以轉換為 Select(this T, 增量){ 反射Model屬性確定查詢條件 } /*一直想這麼幹來著*/;

扯遠了, 現在項目後台不是我做的, 而且介面跨平台不好改成我喜歡那樣, 就只有將查詢條件抽象為一個類;

好處: a. 查詢頁View和Controller傳資料方便;

    b. 方便一些查詢條件的初始值設定;

 

2. 新增, 編輯合并

這可能和 MVC 沒什麼關係, 只用 (id == 0) 來區別新增還是編輯, 以調用不同介面;

 

3. 許可權使用 Forms 存在Cookie中挺好, 粒度細可通過反射設定不同Action的許可權

參考: http://www.cnblogs.com/legendxian/archive/2010/01/22/1653368.html

 

4. 禁止瀏覽器緩衝

 1     using System; 2     using System.Web; 3     using System.Web.Mvc; 4  5     public class NoCacheAttribute : ActionFilterAttribute 6     { 7         public override void OnResultExecuting(ResultExecutingContext filterContext) 8         { 9             filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));10             filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);11             filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);12             filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);13             filterContext.HttpContext.Response.Cache.SetNoStore();14 15             base.OnResultExecuting(filterContext);16         }17     }
NoCacheAttribute

 

參考: http://stackoverflow.com/questions/1160105/disable-browser-cache-for-entire-asp-net-website

近半年MVC使用後的一些習慣

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.