0

I am developing a multi-tenant application using MVC. Once I launch my application in browser I find the TenantId and assign in to static property to maintain across the application like

 public static int TenantId
 {
     get { return _tenantId; }
     set
     {
         _tenantId = value;
         string path = HttpContext.Current.Request.ApplicationPath;

         TenantConfigurationModel tenantConfiguration = HospitalFacade.GetTenantIdByUrl(path);
         if (tenantConfiguration != null)
         {                   
             _tenantId = tenantConfiguration.TenantId;
         }                              
     }
}

But my co developers suggest session

What is the best way to maintain single props value across the application?

Is any advantages/disadvantage using static property ?

James
  • 80,725
  • 18
  • 167
  • 237
Niventh
  • 985
  • 2
  • 9
  • 23

2 Answers2

1

Sessions: Sessions are for a specific user. Which means that if 2 users visit your website then both of them will have different sessions. Each session will have its own values related to the users.

So in nutshell, if the value that you are trying to make available in the whole application is user dependent then use sessions.

Check the answer on an other question: Static fields vs Session variables

No, using static variables for this is not the way to go:

  1. If your AppDomain is recycled, all your static variables will be "reset"
  2. Static variables don't scale horizontally - if you load-balance your application, a user who hits one server then a different one won't see the data sotre in the static variables in the first server
  3. Most importantly, static variables will be shared by all access to that server... it won't be on a per-user basis at all... whereas from your description, you wouldn't want user X to see user Y's information.
Community
  • 1
  • 1
Behroz Sikander
  • 3,885
  • 3
  • 22
  • 36
  • Sessions can be maintained in MVC by three ways: tempdata, viewdata, and viewbag. – Niventh Apr 10 '14 at 09:40
  • @AbiRuban `TempData`/`ViewData` and `ViewBag` are not synonymous with `Session`. – James Apr 10 '14 at 09:42
  • But i don't want store the value to user specific – Niventh Apr 10 '14 at 09:46
  • @AbiRuban That's fine but with your current implementation if the static variables are reset (which *will* happen) then they will start returning default values. What you should have is a custom *get* accessor that validates the static backing field and then fetches it again if it's invalid. If you can't do this, you get it from some persistent source every time as per James' answer. – Ant P Apr 10 '14 at 10:05
1

There are many disadvantages of using a static property and not that many advantages (from a web application perspective).

Your question is a little confusing, you say it's a multi-tenant site yet you want to share the tenancy ID across all the users. Regardless, if the goal is to share data across every possible user on the site then consider just storing the value in a database or, if speed is a concern, some persistent caching solution like Redis.

James
  • 80,725
  • 18
  • 167
  • 237