Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Static is equivalent to singleton – common for the entire application thus all users. You need session-based approach to achieve this. However, if you don't have access to session (such as in business libraries), you can use singleton approach (code example to follow.)</p> <p>Edit: code example to achieve this with singleton approach (similar to static but more maintainable). It uses EF code first approach so you should adapt it if you don't use EF:</p> <p>Edit 2: This is how you should use it:</p> <p>To get time in user timezone:</p> <pre><code>var userId = 5; // assuming 5 a valid user. If not found, current local timezone will be used (`DateTime.Now`) var localTime = UserDateTime.Instance.GetTime(userId);` </code></pre> <p>If a new user is added or existing is modified, you can re-load timezones: (you can optimize it further as per your needs.)</p> <pre><code>UserDateTime.Instance.LoadTimezones(); </code></pre> <p>Implementation:</p> <pre><code>namespace YourApp { using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; class UserDateTime { public static readonly UserDateTime Instance = new UserDateTime(); private UserDateTime() // singleton { LoadTimezones(); } private Dictionary&lt;int, string&gt; _userTimezones = new Dictionary&lt;int, string&gt;(); public DateTime GetTime(int userId) { if (_userTimezones.ContainsKey(userId)) return TimeZoneInfo.ConvertTime(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(_userTimezones[userId])); else return DateTime.Now; // You could throw an error. } public void LoadTimezones() { using (var db = new YourDbContext()) { _userTimezones = db.UserTimezones.ToDictionary(t =&gt; t.UserId, t =&gt; t.TimezoneId); } } } class UserTimezone { public int UserId { get; set; } public string TimezoneId { get; set; } } class YourDbContext : DbContext { public DbSet&lt;UserTimezone&gt; UserTimezones { get; set; } } } </code></pre> <p>Edit: Derived from <a href="http://ASPSecurityKit.net/features#fLocalization" rel="nofollow">ASP Security Kit</a>. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload