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

Related Questions

Best practice question about setting Mongo indexes. Mongoengine, the Python ORM wrapper, allows you
Whats the best practice for setting up package structures in a Java Web Application?
What's the best practice for setting up a subversion repository on a linux development
Is there a best practice when it comes to setting client side onclick events
I have been wondering what the best practices or ramifications are in setting up
Is the best practice of setting an alarm on OS X to create a
What's the best practice for setting zoom factor of an image within IKImageView via
I just have a quick best practice question regarding custom cells in a UITableView.
What is a best practice when setting up subversion to use vendor branches? Our
I ask this partly because I want to know the best practice way of

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.