I recently finished a project where our Domain objects were in charge of managing persistence and behavior (if someone changes a shipping address, recalculate their warehouse based on an account). This requires calls back to the persistence layer via a Home-type service.
The new Core J2EE patterns book suggests Business Objects that wrap simple state objects:
Code:
public class CompanyBO
{
protected CompanyData companyData;
public CompanyBO(CompanyData _companyData)
{
// validate data
this.companyData = _companyData;
}
public ContactBO getContact()
{
return new ContactBO(companyData.getContactData());
}
}
So we split our application into Business Objects and Data Objects where the Business Objects operate upon the Data Objects. So now from the application layer, we can talk to the data model and grab lightweight objects, or if we need to do complex business operations, we take the data and wrap it in a business object. Business Objects would pass their data objects by copy.
Example: List all Orders with light weight Order data objects to save processing, then once an order is selected by ID, ask the data model for a full OrderData object and wrap it with an OrderBO to actually do your operations with (OrderBO would take care of recalculating the warehouse if the shipto changes, etc).
But what would be the proper way to have OrderBO objects communicate that some data needs to persisted?
Any suggestions would be super helpful in such a case of "changing a shipto in order to recalculate the warehouse based on x-variables".
Many Thanks!
Jacob