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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:34:37+00:00 2026-06-09T14:34:37+00:00

I have a project that is using CQRS and Dependency Injection. The Query side

  • 0

I have a project that is using CQRS and Dependency Injection. The Query side of the system is fine.

For the command side of the system I have chosen to use a queue:

BlockingQueue<Command> commandQueue;

This stores the commands as they are received along with their arguments from multiple threads. The commands all implement a common interface with an execute method:

public interface Command extends Serializable {
    void execute();
}

The arguments for the Commands are stored as data in the concrete implementations of the Command interface. The types and potentially number of arguments will vary depending on which command it represents, using this structure means that this detail is all encapsulated away from the command queue logic.

The idea is that the commands are later executed in sequence by a worker thread which calls execute() on each Command in turn without caring about which command it is under the hood.

The commands only require injection once they are taken off the queue ready for execution (this is mostly because I would like to be able to serialize commands, but also because the execution of commands needs different modules to the part of the application that receives and queues them)

My problem is this:
Because the commands need to wait until they are taken off the queue to get their dependencies, I end up passing a lightly wrapped Injector to their ‘execute’ method so they can create themselves an object graph. This feels more like the Service Locator pattern than Dependency Injection.

public interface Command extends Serializable {
    void execute(**ExecutorLocator locator**);
}

Is there something I’m missing or is it inevitable that DI has to look like a service locator at some point in the stack?

  • 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-06-09T14:34:38+00:00Added an answer on June 9, 2026 at 2:34 pm

    It’s been a while since I laid my hands on Java code but good architecture and design is not bound to a language.

    First: ServiceLocator is an anti-pattern.

    Second: Tell, don’t ask. If anything build-up the commands from the outside and don’t let them ask a locator for their dependencies.

    Third: I would create handlers that are registered for the commands and know how to handle the information encapsulated in the commands. Thus you would not need to inject or build-up your commands at all. Setup your handlers and make sure your commands get there.

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

Sidebar

Related Questions

I have an existing project that builds fine using my IDE. I'd like to
I have a project that using some third-party libraries. My questions is how to
We have a project that’s using many C++11 facilities, and we thought about this
I have a project that's using git. When I received the repo, it had
I have got a project that is using jni to connect java wrapper and
I have a small project that I was using node-dirty for, but it's not
I have a Java project that I build using an Ant script. I am
I have a project that needs to be done using ASP.NET. Currently, I'm using
I have a project that require me to do a auto complete search using
I have a project that defines an idl and it compiles it using MIDL.

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.