標籤:wcf asp.net mvc 雲 切換資料來源
這篇部落格給大家介紹一下如何通過Asp.Net+WCF+EF實現雲效果!
首先給大家介紹一下什麼是雲效果。
這次我們做的項目叫做高校雲平台,“雲”其實是互連網的一個隱喻,“雲端運算”其實就是使用互連網來接入儲存或者運行在遠程伺服器端的應用,資料,或者服務。在這個項目當中我們秉承著SaaS:Software-as-a-Service(軟體即服務)的原則,就是說我們開發一個系統後部署在遠程伺服器上,其他想用這個系統的人都通過網路來運行就可以了,就是SaaS了。
高校雲平台他首先是為高校服務的一套系統,處理的學校的新生入學,學生資訊維護,期末考試,評教…….等工作。但是問題來了,我們既然為每個學校都提供了一套服務,那麼我們是不是應該為資料的安全性,資料獨立性,考慮一下呢!針對如何儲存資料我們想到了三個解決方案。
1.獨立資料庫
2.共用資料庫,隔離資料表
3.共用資料庫,共用資料表
這三個方案各有優缺點,獨立資料庫,每個學校一個資料庫,獨立性無疑是很好的,但是他會很佔用空間,公測階段那麼多人註冊了使用者,每個使用者都需要有一個庫,這樣太佔用資源了!共用資料庫,隔離資料表,這樣就是說,所有的學校用一個資料庫,但是資料庫下有多套資料庫表,其實這種方式感覺跟獨立資料庫差不多。可能就免的建立資料庫了!共用資料庫,共用資料表,這樣所有的使用者就用一套資料庫,一個資料庫表,但是每個學校的資料用一個欄位來表示,這樣做確實會很省空間,但是資料量一大之後,我們就會效率就會很難被保障了。
後來經過分析,我們選擇了,每個學校用一套資料庫,這樣資料庫的獨立性,安全性,都會被保障。下邊來看一下,我們是如何?這種效果的,每個人登陸後,能看到自己的資料!
先說一下思路,登陸以後--->取到該使用者的登陸資訊---->通過wcf傳遞到服務端---->寫入線程變數中,在我們執行個體化資料庫內容相關的時候,從線程變數中取出!
下邊給出幾個關鍵地方的代碼!
1.定義一個BaseController,該類中有一個保護類型的變數,讓其他的controller繼承這個controller。
<span style="font-size:18px;">namespace LFBidSystem.Controllers{ public class BaseController : Controller { //每個controller都有的一個變數。用來存放 protect string connectionString = ""; }}</span>
2.定義一個攔截器,在每個controller內的action執行之前,都要攔截,並執行controller中的方法!
<span style="font-size:18px;">namespace LFBidSystem.Controllers{ public class SwtichDbfilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { //擷取當前controller 的實力 BaseController cl =(BaseController) filterContext.Controller; //給當前調用的controller賦值,本初採用的假資料! cl.connectionString = "Data Source=192.168.24.233;user id=sa;password=123456;persist security info=True;database=SwitchDataSource"; } }}</span> 3.這樣,我們當前調用的controller的connectionString變數就會有值了!這樣我們就可以在調用wcf的時候,將這個參數傳遞到服務端,
<span style="font-size:18px;">namespace LFBidSystem.Controllers{ public class testController : BaseController { IUserInfoWCF iUserInfoWcf = ServiceFactory.GetUserInfosService(); public ActionResult Index() { string userInfo = Request["userInfo"]; ViewData["userInfo"] = userInfo; return View(); } public string Login() { UserInfoViewModel enUser = new UserInfoViewModel(); //擷取使用者名稱 string strUserName = Request["userName"]; //擷取密碼 string strPassWord = Request["passWord"]; //調用wcf 的方法 進行登入 UserInfoViewModel enUserInfoVM = new UserInfoViewModel(); enUserInfoVM.UserName = strUserName; enUserInfoVM.PassWord = strPassWord; //取得連結字串的值,這個值在filter 中已經賦值了! enUserInfoVM.DataSource = this.connectionString; try {傳遞的變數中,包括connectionString 的值! enUser = iUserInfoWcf.Login(enUserInfoVM); } catch (Exception e) { } return enUser.other; } }}</span>
4.我們拿到傳遞過來的connectionString,並將這個值寫入到線程變數中!
<span style="font-size:18px;">namespace LFBidSystem.WCFService{ public partial class ServiceBusiness : IUserInfoWCF { //拿到商務邏輯層的介面 // IUserManagerBll iUserManagerBll = SpringHelper.GetObject<IUserManagerBll>("UserManager"); IUserManagerBll iUserManagerBll = null; #region Login 登入 張宏傑 2015年6月6日16:37:19 /// <summary> /// 登入 /// </summary> /// <param name="enUserInfoVM">使用者資訊實體</param> /// <returns>提示條資訊</returns> public UserInfoViewModel Login(UserInfoViewModel enUserInfoVM) { //設定連接字串到線程變數中 CallContext.SetData("databaseId", enUserInfoVM.DataSource); iUserManagerBll = SpringHelper.GetObject<IUserManagerBll>("UserManager"); return iUserManagerBll.Login(enUserInfoVM); } #endregion }}</span> 5.在我們執行個體化資料庫內容相關的時時候,我們要拿到這個連結字串來進行執行個體化!這樣我們執行個體化的內容物件就是對連結字串對應的那個庫進行操作了!
<span style="font-size:18px;">namespace LFBidSystem.DAL{ public class BaseDal<T> : CoreBaseDal<T> where T : class,new() { public SchoolWoeDbContext switchDataSourceEntity; public override void SetDbContext() { //從緩衝中取出內容相關的連結資訊 string databaseId = CallContext.GetData("databaseId").ToString(); DbContext db = CallContext.GetData("DbContextFactory") as SchoolWoeDbContext; if (db == null) { //根據資料庫內容相關的連結資訊,執行個體化資料庫上下文。該上下文會採用才連結字串進行建立! db = new SchoolWoeDbContext(databaseId); //從緩衝中拿到這個資訊 // db = SpringHelper.GetObject<DbContext>("SwitchDataSourceEntities"); //TODO:DbContext,線程內緩衝,不適用於叢集,後期分布式緩衝進行處理,key為guid CallContext.SetData("DbContextFactory", db); } this.MyBaseDbContext = db; switchDataSourceEntity = (SchoolWoeDbContext)db; } }}</span> 6.我們的ORM架構採用的是EF,都知道他有三種模式,具體那種模式的好處,有缺點,我們可以查一些資料,本架構採用的是codeFirst。資料來源上下文類。
<span style="font-size:18px;">namespace ModelText{ public class SchoolWoeDbContext : DbContext { public SchoolWoeDbContext(string ConncectionString) : base(ConncectionString) { this.Database.CreateIfNotExists(); } public virtual DbSet<T_User> T_User { get; set; } public virtual DbSet<T_DataSource> T_DataSource { get; set; } }}</span>
通過這些操作,我們的動態切換資料來源就可以很好的實現了!
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Asp.Net MVC +WCF+EF+Spring 實現雲效果!