this blog to introduce you how to pass Asp.net+wcf+ef Achieve cloud effects!
First of all to introduce what is the cloud effect.
This time we do the project called the University Cloud Platform, "cloud" is actually a metaphor of the internet, "cloud computing" is actually using the Internet to access storage or run on the remote server side of the application, data, or services. In this project we uphold the Saas:software-as-a-service (software as a service) principle, that is, we develop a system after the deployment on the remote server, other people want to use this system to run on the network, is the SaaS.
University Cloud Platform He is the first to serve the university system, the processing of school freshmen, student information maintenance, final exams, evaluation of teaching ... etc. But the problem is, since we have provided a set of services for each school, then we should not for the data security, data independence, think about it! We think of three solutions for how to store data.
1 . Standalone Database
2. Sharing the database, isolating the data table
3. Shared database, shared data table
Each of the three programs have advantages and disadvantages, independent database, each school a database, independence is undoubtedly very good, but he will be very occupied space, public testing stage so many people registered users, each user needs to have a library, so the resource is too occupied! Share the database, isolate the data table, which means that all schools use a database, but the database has more than one database table, in fact, this way feels like the independent database. You may be free to set up a database! Share the database, share the data table, so that all users use a set of databases, a database table, but each school's data in a field to express, it will be very space -saving, but after a large amount of data, we will be very difficult to protect the efficiency.
later after analysis, we chose, each school with a set of databases, so that the independence of the database, security, will be guaranteed. Below to see how we achieve this effect, everyone can see their data after landing!
First talk about the idea, after landing ---> access to the user's login information ----> via WCF Pass to the server ----> Write thread variables, and remove from thread variables when we instantiate the database context!
Below is a few key areas of code!
1. Define a Basecontroller, which has a variable of the type of protection, so that the other controller Inherit this controller.
<span style= "FONT-SIZE:18PX;" >namespace lfbidsystem.controllers{public class Basecontroller:controller { // There is a variable for each controller. Used to store protect string connectionString = "";} } </span>
2. Define an interceptor, in each Controller within the Action before executing, you have to intercept and execute Controller in the Method!
<span style= "FONT-SIZE:18PX;" >namespace lfbidsystem.controllers{public class Swtichdbfilter:actionfilterattribute { public override void OnActionExecuting (ActionExecutingContext filtercontext) { //Get the Power of the current controller Basecontroller cl = (basecontroller) Filtercontext.controller; Assign a value to the controller that is currently called, the false data used at the beginning! cl.connectionstring = "Data source=192.168.24.233;user id=sa;password=123456;persist security info=true; Database=switchdatasource ";}} } </span>
3. Thus, the connectionString variable of the controller we are currently invoking will have a value! so that we can pass this parameter to the server when we call WCF ,
<span style= "FONT-SIZE:18PX;" >namespace lfbidsystem.controllers{public class Testcontroller:basecontroller {IUSERINFOWCF IUserInfoW CF = Servicefactory.getuserinfosservice (); Public ActionResult Index () {String userInfo = request["UserInfo"]; viewdata["UserInfo"] = UserInfo; return View (); public string Login () {Userinfoviewmodel enuser = new Userinfoviewmodel (); Gets the user name string strUserName = request["UserName"]; Get password string strpassword = request["PassWord"]; Call the WCF method to log in Userinfoviewmodel ENUSERINFOVM = new Userinfoviewmodel (); Enuserinfovm.username = strUserName; Enuserinfovm.password = strpassword; Gets the value of the link string, which is already assigned in the filter! Enuserinfovm.datasource = this.connectionstring; Try {Pass in the variable, including the value of ConnectionString! Enuser = IUSERINFOWCF.LOgin (ENUSERINFOVM); } catch (Exception e) {} return enuser.other; }}}</span>
4. we get the connectionStringpassed inand write this value to the thread variable!
<span style= "font-size:18px;" >namespace lfbidsystem.wcfservice{public partial class SERVICEBUSINESS:IUSERINFOWCF {//Get interface to the business logic layer IUSERMANAGERBLL IUSERMANAGERBLL = springhelper.getobject<iusermanagerbll> ("UserManager"); IUSERMANAGERBLL IUSERMANAGERBLL = null; #region Login Zhang Hongjie June 6, 2015 16:37:19//<summary>//Login//</summary> <param name= "ENUSERINFOVM" > User information Entities </param>//<returns> Cue bar information </returns> public U Serinfoviewmodel Login (Userinfoviewmodel enuserinfovm) {//Set connection string to thread variable Callcontext.setdata ("DatabaseId", Enuserinfovm.datasource); IUSERMANAGERBLL = springhelper.getobject<iusermanagerbll> ("Usermanager"); Return Iusermanagerbll.login (ENUSERINFOVM); } #endregion}}</span>
5. When we instantiate the database context, we are going to get the link string to instantiate! So we instantiate the context object is the link string corresponding to the library to operate!
<span style= "FONT-SIZE:18PX;" >namespace lfbidsystem.dal{public class basedal<t>: corebasedal<t> where T:class,new () {p Ublic Schoolwoedbcontext switchdatasourceentity; public override void Setdbcontext () {//Remove the context link information from the cache string databaseId = CallContext.GetData ("DatabaseId"). ToString (); DbContext db = CallContext.GetData ("Dbcontextfactory") as Schoolwoedbcontext; if (db = = null) {//The database context is instantiated based on the link information of the database context. The context is created with a link string! db = new Schoolwoedbcontext (databaseId); Get this information from the cache//db = Springhelper.getobject<dbcontext> ("switchdatasourceentities"); Todo:dbcontext, in-thread cache, not for clustering, post-distributed cache processing, key GUID callcontext.setdata ("Dbcontextfactory", db); } this. Mybasedbcontext = db; Switchdatasourceentity = (schoolwoedbcontext) db; }}}</span>6. our ORM Framework uses EF, we all know that he has three models, specific that the advantages of the model, there are shortcomings, we can look up some information, This architecture is based on Codefirst. the data source context class.
<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>
Through these operations, our dynamic switching data source can be very well implemented!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ASP. NET MVC +wcf+ef+spring for cloud effects!