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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:19:39+00:00 2026-05-29T07:19:39+00:00

I am trying to use Dependency Injection as much as possible, but I am

  • 0

I am trying to use Dependency Injection as much as possible, but I am having trouble when it comes to things like short-lived dependencies.

For example, let’s say I have a blog manager object that would like to generate a list of blogs that it found in the database. The options to do this (as far as I can tell) are:

  1. new Blog();
  2. $this->loader->blog();
    • the loader object creates various other types of objects like database objects, text filters, etc.
  3. $this->blogEntryFactory->create();

However, #1 is bad because it creates a strong coupling. #2 still seems bad because it means that the object factory has to be previously injected – exposing all the other objects that it can create.

Number 3 seems okay, but if I use #3, do I put the “new” keywords in the blogEntryFactory itself, OR, do I inject the loader into the blogEntryFactory and use the loader?

If I have many different factories like blogEntryFactory (for example I could have userFactory and commentFactory) it would seem like putting the “new” keyword across all these different factories would be creating dependency problems.

I hope this makes sense…

NOTE

I have had some answers about how this is unnecessary for this specific blog example, but there are, in fact, cases where you should use the Abstract Factory Pattern, and that is the point I am getting at. Do you use “new” in that case, or do something else?

  • 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-29T07:19:40+00:00Added an answer on May 29, 2026 at 7:19 am

    I’m no expert, but I’m going to take a crack at this. This assumes that Blog is just a data model object that acts as a container for some data and gets filled by the controller (new Blog is not very meaningful). In this case, Blog is a leaf of the object graph, and using new is okay. If you are going to test methods that need to create a Blog, you have to simultaneously test the creation of the Blog anyway, and using a mock object doesn’t make sense .. the Blog does not persist past this method.

    As an example, say that PHP did not have an array construct but had a collections object. Would you call $this->collectionsFactory->create() or would you be satisfied to say new Array;?

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

Sidebar

Related Questions

I am trying to use dependency injection with WCF REST (WebGet) and am having
Using MVC and trying to use dependency injection for controllers, but when I try
We're trying to use Dependency Injection for a WCF Service. The Service has a
I'm trying to use Dependency Injection and Factory class. I've read quite a bit
I'm trying to understand the best way to use Spring (for dependency injection) with
I am trying to use NSManagedObjectContext dependency injection as recommended by Marcus Zarra --
I am trying to work out a way to use dependency injection with ASP.NET
I was trying to use tis depencency property in my code but it gives
Trying to use a guid as a resource id in a rest url but
I am trying to implement Dependency Injection to make my app tester friendly. I

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.