Asp.Net MVC +WCF+EF+Spring 實現雲效果!

來源:互聯網
上載者:User

標籤: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 實現雲效果!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.