javabusiness-logicbulk-operations

Business logic on multiple value objects - where to put the loop?


...and how best to handle success/failure feedback to the view layer.

Options are:

doBusinessLogic(things)

or

for (Thing thing : things) {
  doBusinessLogic(thing)
}

Assuming that we want a view layer that receives success/error feedback in a consistent way (i.e. from single or multiple operations on value objects), what is the best approach?

Clarification:

Handling multiple exception types thrown from a business logic call in the view layer is code heavy and also causes maintenance problems (new exceptions are introduced which the presentation layer doesn't know about). It seems better for the business logic call to handle errors on multiple value objects and 'package' them for the view to deal with in a consistent way.


Solution

  • How about something along the lines of your latter suggestion:

    for (businessObject : businessObjects) { businessObject.doBusinessLogic() }
    

    The idea is to put the business logic in a method of the business object. Your view layer can loop over all the business objects, telling each one to do their business. How each does its business is in the logic of the business object in the business layer. You can handle errors and such by returning values from the doBusinessLogic method or by throwing exceptions for nasty occurrences.

    If you are doing cross-business-object logic (i.e., logic that operates on more than one single business object), perhaps you could build a businessObjectManager class that could have methods that take in one or more business objects and operates on them. Thus, by putting the manager object in the business layer, you still keep all of your business logic in the business layer.