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 4028102
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:10:44+00:00 2026-05-20T11:10:44+00:00

More specifically, What’s the best approach for classes where state matters, within an application

  • 0

More specifically, What’s the best approach for classes where state matters, within an application which implements Dependency Injection.

Say I need access to an object that’s in a particular state. For example, this object might have been initiated in a different thread, or by a process I have no control over.

A good example of an object like this that already exists in .NET is the HttpContext.

In this case, Microsoft decided to go with the Static approach, so I just say:

var currentObj = HttpContext.Current;

And this gives me a particular instance of an object without having to worry where it came from.

The problem with the Static approach is that it doesn’t play very nicely with dependency injection.

The other option is to configure your certain class as a Singleton in your IoC Container. This means that you can inject it, and depending on the current IoC Container config it’ll be the correct instance of the class.

However, the downfall of this approach is that the stateful importance of the object is no longer explicit in the code, it’s not obvious by looking at it. With the Static class used to access and instance it’s more clear that the state is important. Maybe that doesn’t matter though.

So, is there a pattern that helps me out here?

Context:

For context, I’m working on an application which has many instances of a class performing IO operations. They exists within their own threads.

I want to be able to interact with those objects (background tasks) via a web interface, so a Controller. I want to be able to interrogate them, and manipulate them etc.

Update:

Sorry, I think my use of the term “stateful” is a bit misleading. let me explain some thing:

  1. “state” is probably the wrong word. I mean communicating with an object whereby I don’t have control over it’s lifecycle.
  2. It is funny that I use “stateful” when talking about static classes. That’s why I gave the HttpContext example, as that exactly what it does. The Current property gets you a very specific instance, not any new instance.
  3. When I say that static doesn’t play nice with DI, I meant, you can’t inject Static classes. I could create a wrapper, yes, but I’m just pushing the problem elsewhere no?
  4. I should have been more clear about my definition of Singleton. I meant a Singleton lifestyle, as defined in an IoC Container.
  • 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-20T11:10:45+00:00Added an answer on May 20, 2026 at 11:10 am

    I always prefer Singleton over static. In fact, I almost never use statics in my own classes.

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

Sidebar

Related Questions

More specifically I am trying to make the mailto component show within my template;
More specifically, let's say I have a class template with parameters A and B
More specifically, let's say I have a class template with parameters A and B
More specifically I want an interface to compare objects which can be only compared
More specifically, I'm trying to check if given string (a sentence) is in Turkish.
In .Net (VB more specifically, but that doesn't really matter), is there a way
I was of the idea that flash (more specifically Adobe's flash player browser plugin)
Is there stable linear algebra (more specifically, vectors, matrices, multidimensional arrays and basic operations
How do I tell if gcc (more specifically, g++) is optimizing tail recursion in
What is the MIME type of javascript? More specifically, what is the right thing

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.