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

  • SEARCH
  • Home
  • 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 6975415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:21:54+00:00 2026-05-27T17:21:54+00:00

Everywhere in my application i often have to grab the same information (current logged

  • 0

Everywhere in my application i often have to grab the same information (current logged in user) and declare the same services (authorization, settings etc). So far i have combated this problem by doing the following in my Global.asax file:

public class App : HttpApplication {
    private static IMembershipService _membershipService;
    public static IAuthorizationService Authorization;
    public static ISettingsService Settings;

    public static User CurrentUser {
        get { return HttpContext.Current.User.Identity.IsAuthenticated ? _membershipService.GetUser(((UserIdentity)HttpContext.Current.User.Identity).Id) : null; }
    }

    protected void Application_Start() {
        // Create the ioc container
        var container = new UnityContainer();

        ... Register Types Here

        _membershipService = container.Resolve<IMembershipService>();
        Authorization = container.Resolve<IAuthorizationService>();
        Settings = container.Resolve<ISettingsService>();
    }
}

Now to access the current logged in user i simply have to say App.CurrentUser. Note although it is declared static the get is not cached into a static variable and therefore it is different for each request.

I was just wondering if this is a good solution. There’s something i don’t like about it but i’d appreciate the opinions of some ASP.NET experts.

Thanks

  • 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-27T17:21:55+00:00Added an answer on May 27, 2026 at 5:21 pm

    this is an abuse of the static keyword, especially since you are using an IoC container.

    assuming this is an MVC app you should only need a very limited number of calls to the container. The primary call would be the controller factory that would resolve the controller from the container. there may be a few other places to call the continer, but ideally only one.

    if this is webforms you could setup a base class with protected read-only properties to resolve components in the ctor or page_init event. not ideal, but a step in the right direction.

    the other thing to consider is that static classes imply singletons. in which case the components cannot have state. otherwise you get into all kinds of bugs with shared state and race conditions.

    I find it’s best to using transient objects as much as possible, and only keep them around for a short time. lower memory consumption and the object can be tailored to the context in which it’s used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have prepared an application that is a small demo of Student information manipulation.
I would like to access a class everywhere in my application, how can I
Django tutorials everywhere use constant-set application name all around - in urls file, in
Everywhere I look always the same explanation pop ups. Configure the view resolver. <bean
I have been searching everywhere for the following functionality in Lisp, and have gotten
Searched SO and Everywhere else, including the .net developers guide to directory services programming
I have been searching everywhere but I could not find an answer. I need
I need to tidy user input in a web application so that I remove
Let's create WinForms Application (I have Visual Studio 2008 running on Windows Vista, but
I have just installed paperclip into my ruby on rails blog application. Everything is

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.