Unity 在MVC上的應用(上:思路整理)
2011-05-15 11:01 bugfly 閱讀(3478) 評論(7) 收藏 舉報前言
臥病數(shù)月,頹廢的日子。以前向往的未來一一被真幾個月的辛酸打碎了,但未來依然要吃飯,所以打算寫一個簡短MVC系列來回顧IOC的應用,過中將會有實際開發(fā)中解決常見問題的方法。
注意:本系列每一篇文章和上一篇并無必然的承接關(guān)系,但所應用到的技術(shù)點是有先后順序的,你將會從文章的應用上發(fā)現(xiàn)。
系列目錄
Unity 在MVC上的應用(擴展篇:JQuery AJAX)
Unity 在MVC上的應用(擴展篇:事務控制-前篇ActionFilter)
Unity 在MVC上的應用(擴展篇:事務控制-后篇Unit Of Work)
Unity 在MVC上的應用(擴展篇:日志管理NLog)
正題
在上篇,我會通過一個簡單的MVC例子來做一個DEMO,而并沒有使用Unity,目的在于告訴對IOC應用場合不清晰的朋友面對什么情況使用IOC。各個這個例子很簡單 (*^__^*) 嘻嘻……
本次教程使用的是MVC開發(fā)框架,結(jié)合Repository模式來展開。上篇暫時沒有使用到Unity。
一如既往,先看看DEMO的物理結(jié)構(gòu)圖。

那些文件我就不一一敘述了,也不難理解。
先看看大致的運行流程。
Step 1

登錄賬號是HuntSoul 密碼是123456 (*^__^*) 嘻嘻……
Step 2

這里登錄成功的畫面。我們點開“任務列表” Check Check今日有什么任務 ~(X X)~
Step 3

返回一下 XD,點開“打開倉庫”!看看我們倉庫的武器 XD
Step 4

好了,大致的流程就是這個摸樣,正如我所說很簡單~下面我們來看看這些簡單而丑陋的代碼 XD。
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class Entity
{
public int ID { get; set; }
}
}
Entity類作用在于標示對象。
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class Account:Entity
{
public string AccountID { get; set; }
public string AccountPwd { get; set; }
public string LickName{get;set;}
public string Age { get; set; }
public string Email { get; set; }
public string Cellphone { get; set; }
public List<Task> Tasks { get; set; }
public List<StoreHouse> Stores { get; set; }
}
}
這個是我們的 賬號模型,和傳統(tǒng)的游戲賬號相比,當然這個簡單很多,不過基于教程,我們無需太過復雜,恰到理解就可以了,以上英文我想你們都不用我一一解釋其屬性了吧?XD 要提一提的是,本教程沒有使用到數(shù)據(jù)庫,所以對于DB First的程序員,可能比較難理解,不過這個剛好是給你們一個新的視野去嘗試面向?qū)ο螅∣O),這種方式貌似就是業(yè)界所說的Code First吧?或者我所理解的Model First~XD. 還是要解釋一下關(guān)鍵的地方。 Account類有兩個聚合列表對象Task(任務)和StoreHouse(倉庫),是一對多關(guān)系。這里的倉庫有點糾結(jié),寫教程的時候一時想歪了,明白我說的朋友就湊合看吧,哈哈。墨跡了這么多,下面的Model代碼就不一一解釋了。
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class Task : Entity
{
public string TaskName { get; set; }
public string TaskContent { get; set; }
public string TaskStatus { get; set; }
public Account Account { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class StoreHouse : Entity
{
public string GoodsName { get;set;}
public int GoodsNum{get;set;}
public float GoodsKg { get; set; }
public Account Account { get; set; }
}
}
好了,剛才說到我沒使用數(shù)據(jù)庫,那圖片上的數(shù)據(jù)如何解釋?恩,聰明的你已經(jīng)想到了,那個DBContext是一個數(shù)據(jù)生成類,說白了就是一個假的數(shù)據(jù)庫,為了測試,這個必需的,大家無需太過在意我的方式,你喜歡的可以隨意模擬一個DB,用NBuilder也可以~不一一解釋用法。來看看DBContext
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class DBContext
{
public List<Account> Accounts { get; set; }
public DBContext()
{
Accounts = new List<Account>();
Account n_account = new Account()
{
ID = 1,
AccountID = "HuntSoul",
AccountPwd = "123456",
LickName = "桀驁的靈魂",
Age = "44",
Cellphone = "139XXXXXXXXX",
Email = "31580941X@qq.com",
Tasks = new List<Task>() {
new Task(){
ID=1,
TaskName="Unity And MVC practice Demo2",
TaskContent="...",
TaskStatus="已完成"
}
,
new Task(){
ID=2,
TaskName="每日打醬油",
TaskContent="...",
TaskStatus="未完成"
}
},
Stores = new List<StoreHouse>()
{
new StoreHouse(){
ID=1,
GoodsName="希利歐之魂",
GoodsNum=10,
GoodsKg=80
},
new StoreHouse(){
ID=2,
GoodsName="海格力斯之襲",
GoodsNum=30,
GoodsKg=150
},
new StoreHouse(){
ID=3,
GoodsName="克羅莉斯之吻",
GoodsNum=25,
GoodsKg=132
},
}
};
Accounts.Add(n_account);
}
}
}
此時此刻,我們應該看看Controller類了。打開HomeController
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UnityAndMVC2_Demo2.Models;
namespace UnityAndMVC2_Demo2.Controllers
{
[HandleError]
public class HomeController : Controller
{
public AccountRepository _AccountRepository = new AccountRepository();
public TaskRepository _TaskRepository = new TaskRepository();
public StoreHouseRepository _StoreHouseRepository = new StoreHouseRepository();
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(FormCollection Model)
{
Account n_account=new Account();
UpdateModel(n_account);
var n_dbUser=_AccountRepository.find(n_account.AccountID, n_account.AccountPwd);
if (n_dbUser != null)
{
HttpContext.Session["User"] = n_account.AccountID;
return View("Index", n_dbUser);
}
return View();
}
public ActionResult Index()
{
return View();
}
public ActionResult TaskDetails()
{
List<Task> n_taskList = this._TaskRepository.find(HttpContext.Session["User"].ToString());
return View(n_taskList);
}
public ActionResult StoreHouseDetails()
{
List<StoreHouse> n_storeHouseList = this._StoreHouseRepository.find(HttpContext.Session["User"].ToString());
return View(n_storeHouseList);
}
}
}
不算復雜,就幾個ACTION.對應的就是登錄畫面和菜單管理畫面的操作。不墨跡意思了。
如代碼上可以看到,有三個Repository實體類。見以下代碼。
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class AccountRepository
{
public DBContext _Context=new DBContext();
public Account find(int ID)
{
var n_result = from a in _Context.Accounts
select a;
return n_result.ToList().FirstOrDefault();
}
public Account find(string AccountID,string AccountPwd) {
var n_result = from a in _Context.Accounts
where a.AccountID.Equals(AccountID)&&a.AccountPwd.Equals(AccountPwd)
select a;
return n_result.ToList().FirstOrDefault();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class TaskRepository
{
public DBContext _Context = new DBContext();
public List<Task> find(string AccountID)
{
var n_result = from a in _Context.Accounts
where a.AccountID.Equals(AccountID)
select a;
return n_result.FirstOrDefault().Tasks;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UnityAndMVC2_Demo2.Models
{
public class StoreHouseRepository
{
public DBContext _Context = new DBContext();
public List<StoreHouse> find(string AccountID)
{
var n_result = from a in _Context.Accounts
where a.AccountID.Equals(AccountID)
select a;
return n_result.FirstOrDefault().Stores;
}
}
}
這三個Repository實體,內(nèi)容都是那么相似。。聰明的你一定在笑哥,為什么不重構(gòu)一下,對對對,那個什么泛型Repository,不用急,在最后一篇將會重構(gòu)Repository的。這里暫不列出內(nèi)容,,,因為中篇還沒寫,(*^__^*) 嘻嘻……
我們再看看我們的HomeController代碼,你會發(fā)現(xiàn),有不少直接new的對象,就是那三個Repository啦,全篇重點就是這里,直接new的結(jié)果是,過分耦合,repository的生命周期完全和Controller綁定了,結(jié)果就是,有一天要重構(gòu)這幾個Repository實體的方法,你修改的就不是單單那幾個要重構(gòu)的Repository了,而是每個引用到他們的地方,可能你還覺得問題不明顯,這樣吧,如果有1000個Repository實體,有100個Controller引用它們,重構(gòu)它們原本find的行為,改為Lookout()行為。。這個悲劇能看到了吧?所以呢,我們不能直接就new它們到其他類里面,要降低耦合程度,為未來的維護做好準備,什么方法可以解開耦合呢?可以告訴你的很多,不過,我覺得最好的方式就是使用IOC容器,通過容器去關(guān)系注入關(guān)系,恩~完了,下篇我們將會引入Unity來解開這段耦合關(guān)系。很久沒用過了。給點時間我,(*^__^*) 嘻嘻……
最后附上本篇的DEMO代碼
作者:桀驁的靈魂
出處:http://www.rzrgm.cn/HuntSoul/
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權(quán)利。
浙公網(wǎng)安備 33010602011771號