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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:32:43+00:00 2026-05-11T16:32:43+00:00

I have a problem to understand how to use IoC in a scenario where

  • 0

I have a problem to understand how to use IoC in a scenario where I need to create objects dynamically. Lets assume I have this classes:

abstract class Field {
  public Field( ICommandStack commandStack ) {}
}

abstract class Entity {
  public readonly Collection<Field> Fields { get; }
}

class EntityA {
  public EntityA( ICommandStack commandStack ) {
    Fields.Add( new StringField( commandStack ) );
  }
}

class EntitiyB {
  public EntityB( ICommandStack commandStack ) {
    Fields.Add( new IntField( commandStack ) );
    Fields.Add( new IntField( commandStack ) );
    Fields.Add( new IntField( commandStack ) );
  }
}

So my problem is the creation of Fields in the constructors. My Fields need an ICommandStack, but the Entities do not. They only get the ICommandStack for the creation of their Fields.

It could be easier to request the Fields as an argument in each Entity’s constructor. But the number of Fields could be >10 for single Entities. I don’t want to create constructors with so many parameters.

So my idea was to hand over a FieldFactory to the Entites:

class EntityA {
  public EntityA( IFieldFactory fieldFactory ) {
    // create as many fields as needed via the factory
    Fields.Add( fieldFactory.CreateStringField() );
  }
}

At least the (for Entity) unneccessary ICommandStack is now gone. But how does the FieldFactory create a Field? It only can get the ICommandStack injected – but the creation of Fields has still to be done via the ‘new’ keyword. Or should I give the factory a reference to my DI-container?

What is a good design solution here?

  • 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-11T16:32:43+00:00Added an answer on May 11, 2026 at 4:32 pm

    I’d use a FieldFactory and inject the factory with a reference to the container (or to an interface that abstracts it if you are not happy with taking a strong dependency on your container).

    Otherwise, it’s turtles all the way down. You need some object to ask the container for a new instance at some point. If you want your fields to be DI-injected, then you need to ask the container to build them or you.

    So to summarize, I’d go with the factory.

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

Sidebar

Related Questions

I've been have trouble understand this problem. If I change the variable name fifthViewController
I have tried to understand and use urlencode, but the problem can't be solved.
I have a problem which I do not understand. If I use percentage in
I'm trying to use ILGeoNames classes in my project. But I have problem with
I discovered to have some problem to fully understand callbacks scoping when trying to
I have a problem that is quite simple to understand, but for me it
I have a problem with my search on my site I don't understand why
I have got a strange problem about in_array recently which I cannot understand. e.g.
Can't understand what is a problem here: I have got main.cpp file where I
I am having a strange problem which I don't understand. I have the following

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.