Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 319691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:41:05+00:00 2026-05-12T08:41:05+00:00

I’ve been noticing static classes getting a lot of bad rep on SO in

  • 0

I’ve been noticing static classes getting a lot of bad rep on SO in regards to being used to store global information. (And global variables being scorned upon in general) I’d just like to know what a good alternative is for my example below…

I’m developing a WPF app, and many views of the data retrieved from my db are filtered based on the ID of the current logged in user. Similarly, certain points in my app should only be accessable to users who are deemed as ‘admins’.

I’m currently storing a loggedInUserId and an isAdmin bool in a static class.

Various parts of my app need this info and I’m wondering why it’s not ideal in this case, and what the alternatives are. It seems very convienient to get up and running.

The only thing I can think of as an alternative is to use an IoC Container to inject a Singleton instance into classes which need this global information, the classes could then talk to this through its interface. However, is this overkill / leading me into analysis paralysis?

Thanks in advance for any insight.


Update

So I’m leaning towards dependency injection via IoC as It would lend itself better to testability, as I could swap in a service that provides “global” info with a mock if needed. I suppose what remains is whether or not the injected object should be a singleton or static. 🙂

Will prob pick Mark’s answer although waiting to see if there’s any more discussion. I don’t think there’s a right way as such. I’m just interested to see some discussion which would enlighten me as there seems to be a lot of “this is bad” “that is bad” statements on some similar questions without any constructive alternatives.


Update #2
So I picked Robert’s answer seeing as it is a great alternative (I suppose alternative is a weird word, probably the One True Way seeing as it is built into the framework). It’s not forcing me to create a static class/singleton (although it is thread static).

The only thing that still makes me curious is how this would have been tackled if the “global” data I had to store had nothing to do with User Authentication.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T08:41:05+00:00Added an answer on May 12, 2026 at 8:41 am

    Forget Singletons and static data. That pattern of access is going to fail you at some time.

    Create your own custom IPrincipal and replace Thread.CurrentPrincipal with it at a point where login is appropriate. You typically keep the reference to the current IIdentity.

    In your routine where the user logs on, e.g. you have verified their credentials, attach your custom principal to the Thread.

    IIdentity currentIdentity = System.Threading.Thread.CurrentPrincipal.Identity;
    System.Threading.Thread.CurrentPrincipal 
       = new MyAppUser(1234,false,currentIdentity);
    

    in ASP.Net you would also set the HttpContext.Current.User at the same time

    public class MyAppUser : IPrincipal
    {
       private IIdentity _identity;
    
       private UserId { get; private set; }
       private IsAdmin { get; private set; } // perhaps use IsInRole
    
       MyAppUser(userId, isAdmin, iIdentity)
       {
          if( iIdentity == null ) 
             throw new ArgumentNullException("iIdentity");
          UserId = userId;
          IsAdmin = isAdmin;
          _identity = iIdentity;          
       }
    
       #region IPrincipal Members
       public System.Security.Principal.IIdentity Identity
       {
          get { return _identity; }
       }
    
       // typically this stores a list of roles, 
       // but this conforms with the OP question
       public bool IsInRole(string role)
       {  
          if( "Admin".Equals(role) )
             return IsAdmin;     
    
          throw new ArgumentException("Role " + role + " is not supported");
       }
       #endregion
    }
    

    This is the preferred way to do it, and it’s in the framework for a reason. This way you can get at the user in a standard way.

    We also do things like add properties if the user is anonymous (unknown) to support a scenario of mixed anonymous/logged-in authentication scenarios.

    Additionally:

    • you can still use DI (Dependancy Injection) by injecting the Membership Service that retrieves / checks credentials.
    • you can use the Repository pattern to also gain access to the current MyAppUser (although arguably it’s just making the cast to MyAppUser for you, there can be benefits to this)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.