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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:23:51+00:00 2026-06-04T03:23:51+00:00

In the project im working on, people wrote services class to access DAO. Almost

  • 0

In the project im working on, people wrote services class to access DAO.
Almost every business object has it’s own service which use it’s own DAO.
On some services, we are using references to other services.
At the moment, people are instantiating needed services inside the constructor.

But now, I have trouble because service A needs service B and service B needs service A so a call to either constructor results in stack overflow …

Example (pseudo-code) :

//Constructor of OrderService
public OrderService() {
     orderDAO = DAOFactory.getDAOFactory().getOrderDAO();
     itemService = new ItemService();
}

//Constructor of ItemService
public ItemService() {
     itemDAO = DAOFactory.getDAOFactory().getItemDAO();
     orderService = new OrderService();
}

How would you solve this ? using singleton pattern ?

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-06-04T03:23:52+00:00Added an answer on June 4, 2026 at 3:23 am

    The Spring Framework solves this problem by using dependency injection. In short, what it does is to instantiate all the DAOs, and then set the dao-dependencies after instantiation, but before main business logic.

    If you have to do this manually, here’s an example:

    /*
      OrderService
     */
    public OrderService ()
    {
         orderDAO = DAOFactory.getDAOFactory().getOrderDAO();
    }
    
    public setItemService (ItemService service)
    {
         itemService = service;
    }
    
    /*
      ItemService
     */
    public ItemService ()
    {
         itemDAO = DAOFactory.getDAOFactory().getItemDAO();
    }
    
    public setOrderService (OrderService service)
    {
         orderService = service;
    }
    
    /*
       Bring it together in some other class
     */
    ...
    // instantiate singletons
    orderService = new OrderService ();
    itemService = new ItemService ();
    
    // inject dependencies
    orderService.setItemService (itemService);
    itemService.setOrderService (orderService);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are working with severall people on the same project and are using Mercurial
When working on a project with several other people it's common to have several
I'm working on an old project written and then patched by several people over
i'm currently working on an ios project with some people, one of us decided
So i am currently working on a simple game project that most people start
I have a team of people working on a Visual Studio (C#) project, and
I have been working on a project where I have a Worker class that
I am currently working on a project where people write articles, which are stored
If a group of 8 people are working on a project is it needed
I'm working on a project that deals with lots of people editing binaries, and

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.