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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:41:22+00:00 2026-05-13T06:41:22+00:00

Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and

  • 0

Just assume I have some class Foo, that has two dependencies: an ISerializer<T> and an IFileAccessHandler.

Now this class also has other dependencies, functional dependencies. I don’t want anyone instantiating this class in an invalid state, so I’d also need to pass a domain object in the constructor.

But how can I have that handled by IoC when I also know what domain object to pass in the moment I’m actually creating class Foo?

I made the domain object a property that I have set by a Factory. So the Factory makes a Service Locator call to get a properly instantiated “Foo” class with it’s dependencies, and further fills it up with the correct domain object and returns it.

But is this the best way to go? I would have preferred having the domain object part of my constructor to make it apparant you actually need to work with “Foo”.

Any ideas?
Am I missing something here?

  • 1 1 Answer
  • 1 View
  • 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-13T06:41:22+00:00Added an answer on May 13, 2026 at 6:41 am

    The default solution to DI when you can’t wire up a concrete type at registration time is to use an Abstract Factory

    In your case, I would define an IFooFactory interface:

    public interface IFooFactory
    {
        Foo Create(DomainClass dc);
    }
    

    This will allow you to define a concrete implementation that knows about your infrastructure services.

    public class FooFactory : IFooFactory
    {
        private readonly ISerializer serializer;
        private readonly IFileAccessHandler fileHandler;
    
        public FooFactory(ISerializer serializer, IFileAccessHandler fileHandler)
        {
            if(serializer == null)
            {
                throw new ArgumentNullException("serializer");
            }
            if(fileHandler == null)
            {
                throw new ArgumentNullException("fileHandler");
            }
    
            this.serializer = serializer;
            this.fileHandler = fileHandler;
        }
    
        public Foo Create(DomainClass dc)
        {
            return new Foo(this.serializer, this.fileHandler, dc);
        }
    }
    

    In this way you can protect the invariants of your Foo class, enabling you to stay with Constructor Injection.

    In the DI container, you can register the IFooFactory and corresponding implementation. Everywhere you have a DomainClass instance and need a Foo instance, you would then take a dependency on IFooFactory and use that.

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

Sidebar

Related Questions

Assume we have a trivial Java program that consists of just one class: public
I had assume I could just do this, but I don't have a way
I have an immutable class with some private fields that are set during the
Assume that I have some HTML page, and a corresponding CSS file. I'd like
Assume I have some program that pulls airline data. Each airline data uses a
I have a C++/CLI class library project in VS2005 that i am having some
FieldInfo has an IsStatic member, but PropertyInfo doesn't. I assume I'm just overlooking what
I have a class WebServiceCaller that uses NSURLConnection to make asynchronous calls to a
Ok, So I have the following situation. I originally had some code like this:
I have some code that I want to refactor. I have lots of methods

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.