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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:20:31+00:00 2026-05-13T22:20:31+00:00

My Dispatcher is choosing correct Controller; then creating Controller’s instance (DependencyInjectionContainer is passed to

  • 0

My Dispatcher is “choosing” correct Controller; then creating Controller’s instance (DependencyInjectionContainer is passed to Controller constructor); then calling some Controller’s method…

class UserController extends Controller
{

  public function __construct(DependencyInjectionContainer $injection) {
    $this->container = $injection;
  }

  public function detailsAction() {
    ...
  }

}

DependencyInjectionContainer contains DB adapter object, Config object etc.
Now let’s see what detailsAction() method contains…

public function detailsAction() {

  $model = new UserModel();
  $model->getDetails(12345);

}

As you see I’m creating new instance of UserModel and calling getDetails methods.
Model’s getDetails() method should connect to db to get information about user. To connect to DB UserModel should be able to access DB adapter.

What is the right way to pass DependencyInjectionContainer to the UserModel?
I think that this way is wrong…

public function detailsAction() {

  $model = new UserModel($this->container);
  $model->getDetails(12345);

}
  • 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-13T22:20:31+00:00Added an answer on May 13, 2026 at 10:20 pm

    Instead of injecting the entire DI Container into your classes, you should inject only the dependencies you need.

    Your UserController requires a DB Adapter (let’s call this interface IDBAdapter). In C# this might look like this:

    public class UserController
    {
        private readonly IDBAdapter db;
    
        public UserController(IDBAdapter db)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }
    
            this.db = db;
        }
    
        public void DetailsAction()
        {
            var model = new UserModel(this.db);
            model.GetDetails(12345);
        }
    }
    

    In this case we are injectiing the dependency into the UserModel. In most cases, however, I would tend to consider it a DI smell if the UserController only takes a dependency to pass it on, so a better approach might be for the UserController to take a dependency on an Abstract Factory like this one:

    public interface IUserModelFactory
    {
        UserModel Create();
    }
    

    In this variation, the UserController might look like this:

    public class UserController
    {
        private readonly IUserModelFactory factory;
    
        public UserController(IUserModelFactory factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory");
            }
    
            this.factory = factory;
        }
    
        public void DetailsAction()
        {
            var model = this.factory.Create();
            model.GetDetails(12345);
        }
    }
    

    and you could define a concrete UserModelFactory that takes a dependency on IDBAdapter:

    public class UserModelFactory : IUserModelFactory
    {
        private readonly IDBAdapter db;
    
        public UserModelFactory(IDBAdapter db)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }
    
            this.db = db;
        }
    
        public UserModel Create()
        {
            return new UserModel(this.db);
        }
    }
    

    This gives you better separation of concerns.

    If you need more than one dependency, you just inject them through the constructor. When you start to get too many, it’s a sign that you are violating the Single Responsibility Principle, and it’s time to refactor to Aggregate Services.

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

Sidebar

Ask A Question

Stats

  • Questions 344k
  • Answers 344k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: $(clickReceiver).parents("li:last").attr('p_node') May 14, 2026 at 5:40 am
  • Editorial Team
    Editorial Team added an answer That's a common problem with DOM : you have to… May 14, 2026 at 5:40 am
  • Editorial Team
    Editorial Team added an answer For those of you with a similair issue, this is… May 14, 2026 at 5:40 am

Related Questions

I think I need some clarifications regarding WPFs Dispatcher.Invoke and Dispatcher.BeginInvoke usage. Suppose I
I have a very bad feeling about using lock in my code but now
My handler forwards to internalresourceview 'apiForm' but then i get error 404 RequestURI=/WEB-INF/pages/apiForm.jsp. I'm
I am attempting to write a dynamic dispatcher for a function that's templated on
I've put all of my user-authentication code in one place, namely lib/auth.rb. It looks

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.