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 usi
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);