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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:04:35+00:00 2026-05-13T19:04:35+00:00

Sometimes I have classes which need to get some information for construction. I am

  • 0

Sometimes I have classes which need to get some information for construction. I am not talking about references to other objects (which will be injected) but about (for instance) strings which are holding unique information:

// Scoped as singleton!
class Repository
{
    public Repository( InjectedObject injectedObject, string path ) { ... }
}

How do you get this string injected? One possiblity is to write an Init() method and to avoid injection for the string:

class Repository
{
    public Repository( InjectedObject injectedObject ) { ... }
    public void Init( string path ) { ... }
}

Another possibility is to wrap the information into an object, which can be injected:

class InjectedRepositoryPath
{
    public InjectedRepositoryPath( string path ) { ... }
    public string Path { get; private set; }
}

class Repository
{
    public Repository( InjectedObject injectedObject, InjectedRepositoryPath path ) { ... }
}

This way I’d have to create an instance of InjectedRepositoryPath during the initialisation of my DI-Container and register this instance. But I need such an unique configuration object for every similar class.

Of course I can resolve a RepositryFactory instead of the Repository object, so the factory would ask me for the path:

class RepositoryFactory
{
    Repository Create( string path ) { ... }
}

But again, this is one factory just for a singleton object …
Or, finally, since the path will be extracted from a configuration file, I could skip passing around the string and read the config in my constructor (which is probably not as optimal, but possible):

class Repository
{
    public Repository( InjectedObject injectedObject )
    {
        // Read the path from app's config
    }
}

What’s your favorite method? For non-singleton classes you have to use imho the Init() or factory solution, but what about singleton-scoped objects?

  • 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-13T19:04:35+00:00Added an answer on May 13, 2026 at 7:04 pm

    I prefer not having a DI Container dictate my API design. The container should conform to proper design, not the other way around.

    Design your classes in a DI-friendly manner, but without making concessions to your DI Container. If you need a connection string, then take a string through the constructor:

    public class Repository : IRepository
    {
        public Repository(string path) { //... }
    }
    

    Many DI Containers can deal with primitive values. As an example, here’s one way to do it with Windsor:

    container.Register(Component.For<IRepository>()
        .ImplementedBy<Repository>()
        .DependsOn( new { path = "myPath" } ));
    

    However, if your container of choice can’t deal with primitive parameters, you can always decorate Repository with an implementation that knows how to find the string:

    public class ConfiguredRepository : IRepository
    {
        private readonly Repository decoratedRepository;
    
        public ConfiguredRepository()
        {
            string path = // get the path from config, or whereever appropriate
            this.decoratedRepository = new Repository(path);
        }
    
        // Implement the rest of IRepository by
        // delegating to this.decoratedRepository
    }
    

    Now you can simply tell your container to map IRepository to ConfiguredRepository, while still keeping the core Repository implementation clean.

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

Sidebar

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.