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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:33:56+00:00 2026-05-22T17:33:56+00:00

I have always felt uncomfortable when dealing with classes that needed to instantiate a

  • 0

I have always felt uncomfortable when dealing with classes that needed to instantiate a lot of objects since I’ve been using Dependency Injection principles.

For instance, let’s say I have a class that’s supposed to raise a lot of different kind of events. Each event has a different type, so what I’d do is to have a different factory for each different event type. If I have 10 events, then I’ll have to have 10 factories. That doesn’t seem nice. I could also have a single factory for all the different kinds of events, but that doesn’t seem too right, also.

(for the C# crowd, I’m not talking about .NET’s events here. This was just an example to get to the point, just think of them as regular classes!)

This was just an example. I don’t have a problem in having a factory here or there, but in certain kinds of projects where one has to create lot of objects at run-time, it seems as I have to make a factory for almost every class I define!

How do you handle this situation? Am I missing something?

I’ve seen people just passing around a reference to the IoC Container they use, but that doesn’t seem any good to me. IMO, the domain model shouldn’t even know a IoC Container is being used!

Thanks

  • 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-22T17:33:57+00:00Added an answer on May 22, 2026 at 5:33 pm

    There’s nothing wrong with a class that instantiates many other objects. Instead, that class should be treated as an aggregate root domain entity. As for different “types” of entity, if you assume they implement the same interface or inherit from the same base class, then passing a type argument to Factory.create(type) is how I usually go about approaching this issue. The internals of create() may delegate to other classes ala the Strategy pattern, but the client facing API is straightforward.

    Now if you’re creating a factory for every class you’re dealing with, that sounds like something of anti-pattern. Study the aggregate root pattern mentioned above and see if you can organize your entities into graphs – you should be able to – such that one factory suffices for generating the whole graph.

    As for IoC, the domain model should not know about the container. When I have entities that need references to singletons – usually factories – in the container I usually inject one factory into another as such:

    class FactoryA {
        void setFactoryB(FactoryB factoryB) { /* sets into state */ }
        EntityA create(Enum type) {
            EntityA entityA = new EntityA();
            /* DI FactoryB - this method is probably default access */
            entityA.setFactoryB(getFactoryB());
        }
    }
    
    class FactoryB {}
    

    So in the above example both FactoryA and FactoryB are singletons managed by the IoC container. EntityA needs a reference to FactoryB, so FactoryA is injected with a reference to FactoryB that’s passed along inside the create() method.

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

Sidebar

Related Questions

I have always been using virtualenv for testing my app in localhost since I
I've always felt that my graphic design skills have lacked, but I do have
I have always felt that in general the main work of a class should
I've been using JSF for a while but there's something that has always confused
I have always been taught that programming against an interface is better, so parameters
I have always wondered that since Qt uses almost every C++ feature in the
I have always thought that using -1 in a condition is alway the same
I have always thought that the .equals() method in java should be overridden to
I have always made a point of writing nice code comments for classes and
I have always been for documenting code, but when it comes to AJAX +

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.