September 17, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Introduction to Multi-Tenant Architecture

  • February 11, 2009
  • By Neal Schneider
  • Send Email »
  • More Articles »

Now, when a user accesses one of the tenants the first time, an app setting is used. You need to determine which tenant it is and load the app settings for that tenant. To prevent constant database hits and increase performance, I have created a class to store the app settings in the HTTP Cache inside a hash table that will help maintain the concept of accessing the settings from the Web.Config file. Quick note, I am using a LINQ To SQL Data Context to handle all the database interaction.

namespace MultiTenantSite.Data
{

/// <summary>
/// This class is used to manage the Cached AppSettings
/// from the Database
/// </summary>
public class AppSettings
{
   /// <summary>
   /// This indexer is used to retrieve AppSettings from Memory
   /// </summary>
   public string this[string Name]
   {
      get
      {
         //See if we have an AppSettings Cache Item
         if (HttpContext.Current.Cache["AppSettings"] == null)
         {
            int? TenantID = 0;
            //Look up the URL and get the Tenant Info
            using (MultiTenantDataContext dc =
               new MultiTenantDataContext())
            {
               AppURL result =
                      dc.AppURLs
                      .Where(a => a.vc_URL =
                         HttpContext.Current.Request.Url.
                            Host.ToLower())
                      .FirstOrDefault();

               if (result != null)
               {
                  TenantID = result.in_Tenant_Id;
               }
            }
            App.AppSettings.LoadAppSettings(TenantID);
         }

         Hashtable ht =
           (Hashtable)HttpContext.Current.Cache["AppSettings"];
         if (ht.ContainsKey(Name))
         {
            return ht[Name].ToString();
         }
         else
         {
            return string.Empty;
         }
      }
   }

   /// <summary>
   /// This Method is used to load the app settings from the
   /// database into memory
   /// </summary>
   public static void LoadAppSettings(int? TenantID)
   {
      Hashtable ht = new Hashtable();

      //Now Load the AppSettings
      using (MultiTenantDataContext dc =
         new MultiTenantDataContext())
      {
         var results = dc.AppSettings.Where(a =>
            a.in_Tenant_Id == TenantID);

         foreach (var appSetting in results)
         {
            ht.Add(appSetting.vc_Name, appSetting.vc_Value);
         }
      }

      //Add it into Cache (Have the Cache Expire after 1 Hour)
      HttpContext.Current.Cache.Add("AppSettings",
         ht, null,
         System.Web.Caching.Cache.NoAbsoluteExpiration,
         new TimeSpan(1, 0, 0),
         System.Web.Caching.CacheItemPriority.NotRemovable, null);

         }
      }
   }
}

With this App Settings class, I can access app settings for the current tenant efficiently and, more importantly, when I write my code, the settings will adapt as long as they are updated respectively for each tenant.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel