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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:52:28+00:00 2026-05-11T07:52:28+00:00

What is the best practice on setting and keeping these global objects ? I

  • 0

What is the best practice on setting and keeping these ‘global’ objects ?

I have encountered this problem couple of times with

  • ORM session object (XPO from DevExpress)

  • IOC container (Microsoft.Unity)

  • Logger (from The Object Guy)

I have come up with three options on how to deal with this:

  1. Dependency injection. This looks extremely cumbersome to me and I feel like every class is bloated by extra three getters/setters that all almost all the classes would use. On the other hand it gets rid of the global variables fear and makes the code bits less dependant of each other.

  2. Create a static class (for example ContainerKeeper) that is settable once and returns the kept object. I know this resembles a singleton and I should use singleton instead.

  3. Using a Singleton pattern. I’ve read some discussion against the use of singletons and how bad they are etc. so I am frankly unsure how many principles of good OO design will I break by using one in these cases.

So my question rephrased is :

Is there some other option, Am I completely off the track ? How would you do it ?

  • 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. 2026-05-11T07:52:28+00:00Added an answer on May 11, 2026 at 7:52 am

    I usually use dependency injection, that would be option 1. But instead of using constructor parameters for injection I usually use properties. This cleans up my constructors. I use Castle Windsor as an IoC container. It allows me to move setup code from the constructor to an Initialize method that is called after all dependencies are set whenever I implement an IInitializable interface. But my guess is that Unity also supports something like this.

    Option 2 and 3 would not be optimal. By calling statics you tie your code to implementations of the classes it depends on. Having them injected only ties your code to the interface of the dependencies.


    You do need the container object. But usually only at the highest level. Dependencies of dependencies etc. are resolved automatically, castle windsor does this and i’m pretty sure unity does this too. I normally don’t use the IoC container for objects that need to be constructed on the fly so I don’t often need to have a reference to the container on lower levels. When I do need this there are some tricks where you can have the container inject itself into classes that depend on it. Got to write a blog post on this. I’ll let you know when it is done.

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

Sidebar

Ask A Question

Stats

  • Questions 211k
  • Answers 211k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the test setup, get the root logger and set… May 12, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer That is because you have two copies of InterfaceA. See… May 12, 2026 at 10:04 pm
  • Editorial Team
    Editorial Team added an answer If you used plain HTML, your radio buttons will all… May 12, 2026 at 10:04 pm

Related Questions

I'm soon going to check in the very first commit of a new Java
If I use a configuration file to store for example the sitename, database host,
What is the best practice way to do unit testing with Spring? I assume
I ask this partly because I want to know the best practice way of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.