UserContext.GetUserContext C# (CSharp) Method

GetUserContext() public static method

Gets the data context out of the system cache, or the database.
public static GetUserContext ( string user, Cache, cache, CodeReviewDataContext context ) : UserContext,
user string Authenticated user alias (no DOMAIN).
cache Cache, System cache.
context CodeReviewDataContext Data connection to the database.
return UserContext,
    public static UserContext GetUserContext(string user, Cache cache, CodeReviewDataContext context)
    {
        Hashtable allUserContexts = (Hashtable)cache[UserContextTableName];
        if (allUserContexts == null)
            cache[UserContextTableName] = allUserContexts = new Hashtable();

        UserContext uc = (UserContext)allUserContexts[user];
        if (uc == null)
        {
            uc = new UserContext(user);
            var dataContexts = from cc in context.UserContexts
                               where cc.UserName == user
                               select cc;
            foreach (DataModel.UserContext dataContext in dataContexts)
            {
                if (dataContext.KeyName.Equals(UserContext.TEXT_SIZE, StringComparison.OrdinalIgnoreCase))
                {
                    int value;
                    if (int.TryParse(dataContext.Value, out value))
                        uc.TextSize = value;
                }
                else if (dataContext.KeyName.Equals(UserContext.TEXT_FONT, StringComparison.OrdinalIgnoreCase))
                {
                    uc.TextFont = dataContext.Value;
                }
                else if (dataContext.KeyName.Equals(UserContext.HINT_MASK, StringComparison.OrdinalIgnoreCase))
                {
                    int value;
                    if (int.TryParse(dataContext.Value, out value))
                        uc.HintsMask = value;
                }
                else if (dataContext.KeyName.Equals(UserContext.MAX_LINE_LENGTH, StringComparison.OrdinalIgnoreCase))
                {
                    int value;
                    if (int.TryParse(dataContext.Value, out value))
                        uc.MaxLineLength = value;
                }
                else if (dataContext.KeyName.Equals(UserContext.SPACES_PER_TAB, StringComparison.OrdinalIgnoreCase))
                {
                    int value;
                    if (int.TryParse(dataContext.Value, out value))
                        uc.SpacesPerTab = value;
                }
                else if (dataContext.KeyName.Equals(UserContext.UNIFIED_DIFF_VIEW, StringComparison.OrdinalIgnoreCase))
                {
                    bool value;
                    if (bool.TryParse(dataContext.Value, out value))
                        uc.UnifiedDiffView = value;
                }
                else if (dataContext.KeyName.Equals(UserContext.COMMENT_CLICK_MODE, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        uc.CommentClickMode = (CommentClickMode)Enum.Parse(
                            typeof(CommentClickMode), dataContext.Value);
                    }
                    catch (ArgumentNullException) { }
                    catch (ArgumentException) { }
                }
                // Compat with previous "double/single" click database entries
                else if (dataContext.KeyName.Equals("commentstyle", StringComparison.OrdinalIgnoreCase))
                {
                    int value;
                    if (int.TryParse(dataContext.Value, out value))
                    {
                        uc.CommentClickMode =
                            (value == 0 ? CommentClickMode.SingleClickAnywhere
                                        : CommentClickMode.DoubleClickAnywhere);

                        // Save translated setting
                        context.SetUserContext(UserContext.COMMENT_CLICK_MODE, uc.CommentClickMode.ToString());
                    }

                    // Remove old setting from database
                    context.UserContexts.DeleteOnSubmit(dataContext);
                    context.SubmitChanges();
                }
                else if (dataContext.KeyName.Equals(
                    UserContext.AUTO_COLLAPSE_COMMENTS,
                    StringComparison.OrdinalIgnoreCase))
                {
                    bool value;
                    if (bool.TryParse(dataContext.Value, out value))
                        uc.AutoCollapseComments = value;
                }
            }

            allUserContexts[user] = uc;
        }

        return uc;
    }

Usage Example

        public ActionResult AddCourseDialog(String learningAimRef)
        {
            UserContext.UserContextInfo uco = UserContext.GetUserContext();
            if (uco.ContextName != UserContext.UserContextName.Provider)
            {
                return(HttpNotFound());
            }

            Provider provider = db.Providers.Find(userContext.ItemId);

            if (provider == null)
            {
                return(HttpNotFound());
            }

            AddCourseModel model = new AddCourseModel();

            if (learningAimRef == null)
            {
                model.CourseHasLearningAimRef = 0; /* Used for Adding a new course - no default */
            }
            else if (learningAimRef == "")
            {
                model.CourseHasLearningAimRef = 1; /* No */
            }
            else
            {
                model.CourseHasLearningAimRef = 2; /* Yes */
                model.LearningAim             = db.LearningAims.Find(learningAimRef);
            }

            return(View(model));
        }
All Usage Examples Of UserContext::GetUserContext