{"id":2083,"date":"2019-10-03T14:06:19","date_gmt":"2019-10-03T19:06:19","guid":{"rendered":"https:\/\/xlinesoft.com\/blog\/?p=2083"},"modified":"2021-05-31T15:13:27","modified_gmt":"2021-05-31T20:13:27","slug":"saas-application-design","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2019\/10\/03\/saas-application-design\/","title":{"rendered":"SaaS application design"},"content":{"rendered":"<p>SaaS applications, also known as multi-tenant applications, are very popular these days. A single instance of the software is available to multiple customers\/tenants. Each tenant&#8217;s data is isolated and remains invisible to other tenants. All tenant databases have the same structure. The data is different, of course. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_database_list.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_database_list.png\" alt=\"\" width=\"261\" height=\"222\" class=\"alignnone size-full wp-image-2087\" \/><\/a><br \/>\n<!--more--><\/p>\n<p>There are multiple ways to design and implement this kind of application in PHPRunner or ASPRunner.NET. You can get away using a single data to host all customers&#8217; data but using a dedicated database for each customer is easier and more reliable. In this article, we&#8217;ll show the easiest way to build such an app.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_datasource_tables.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_datasource_tables.png\" alt=\"\" width=\"870\" height=\"631\" class=\"alignnone size-full wp-image-2088\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_datasource_tables.png 870w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_datasource_tables-300x218.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/scr_saas_datasource_tables-768x557.png 768w\" sizes=\"(max-width: 870px) 100vw, 870px\" \/><\/a><\/p>\n<p>You can see that we have a common <strong>saasdb<\/strong> database here and multiple identical client databases. We only need to add to the project one of those client databases, database1 one in our example.  <\/p>\n<p>Common <strong>saasdb<\/strong> database only contains a single table named <strong>users<\/strong>. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_users.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_users.png\" alt=\"\" width=\"872\" height=\"503\" class=\"alignnone size-full wp-image-2091\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_users.png 872w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_users-300x173.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_users-768x443.png 768w\" sizes=\"(max-width: 872px) 100vw, 872px\" \/><\/a><\/p>\n<p>We will select this table as a login table on the Security screen. Based on the username we redirect users to their database. We would only need to add two lines of code to make this happen. Things will be slightly different in PHPRunner and ASPRunner.NET.<\/p>\n<h3>PHPRunner<\/h3>\n<p><strong>1. AfterSuccessfulLogin event<\/strong><\/p>\n<pre name=\"code\" class=\"php:nocontrols\">$_SESSION[\"dbname\"] = $data[\"database\"];<\/pre>\n<p>Based on who is logged in we save database name in the session variable. <\/p>\n<p><strong>2. Server Database Connection<\/strong><\/p>\n<p>Proceed to the Output Directory screen, add a new Server Database Connection for the second database connection (database1) and modify the line where we specify the database name. This example is for MySQL. The point is to use $_SESSION[&#8220;dbname&#8221;] instead of the hardcoded database name. <\/p>\n<pre name=\"code\" class=\"php:nocontrols\">$host=\"localhost\";\r\n$user=\"root\";\r\n$pwd=\"\";\r\n$port=\"\";\r\n$sys_dbname=$_SESSION[\"dbname\"];<\/pre>\n<h3>ASPRunner.NET<\/h3>\n<p><strong>1. AfterSuccessfulLogin event<\/strong><\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">XSession.Session[\"database\"]=data[\"database\"];<\/pre>\n<p><strong>2. BeforeConnect event<\/strong><\/p>\n<p>.NET applications are compiled before they can be deployed meaning that we cannot use any code in a new Server Database Connection. For this specific reason, we have added BeforeConnect event in ASPRunner.NET v10.3. <\/p>\n<p>This is how default connection string looks (as shown in the event Description):<\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nGlobalVars.ConnectionStrings[\"database1_at_localhost\"] = \"Server=localhost;Database=database1;User Id=root;Password=\";<\/pre>\n<p>This is how we need to change it to grab database name from the session variable:<\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\nGlobalVars.ConnectionStrings[\"database1_at_localhost\"] = \r\nString.Format(\"Server=localhost;Database={0};User Id=root;Password=\", XSession.Session[\"database\"]);\r\n<\/pre>\n<p>This is it. If you logon as user1 now you will see two records in the Customers table.<\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user1.png\" alt=\"\" width=\"879\" height=\"547\" class=\"alignnone size-full wp-image-2085\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user1.png 879w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user1-300x187.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user1-768x478.png 768w\" sizes=\"(max-width: 879px) 100vw, 879px\" \/><\/a><\/p>\n<p>And if you logon as user2 you will see three records in the same table, because we are connected to <strong>database2<\/strong> now. <\/p>\n<p><a href=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user2.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user2.png\" alt=\"\" width=\"879\" height=\"547\" class=\"alignnone size-full wp-image-2086\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user2.png 879w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user2-300x187.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2019\/10\/saas_user2-768x478.png 768w\" sizes=\"(max-width: 879px) 100vw, 879px\" \/><\/a><\/p>\n<p>Of course, creating a fully-featured SaaS application takes more than two lines of code. You need to take care of user registration, create new databases on the fly, upgrade all databases when database structure changes, maybe add the billing part etc. But this article should definitely help you get started. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>SaaS applications, also known as multi-tenant applications, are very popular these days. A single instance of the software is available to multiple customers\/tenants. Each tenant&#8217;s data is isolated and remains invisible to other tenants. All tenant databases have the same structure. The data is different, of course.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,10,1,8],"tags":[],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2083"}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":12,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"predecessor-version":[{"id":2411,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions\/2411"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}