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

... and how best to handle success / failure feedback at the presentation level.

Parameters:

doBusinessLogic(things)

      

or

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

      

Assuming we want the presentation tier to receive success / error feedback in a consistent way (i.e. from single or multiple operations on value objects), what's the best approach?

Clarification:

Handling multiple types of exceptions thrown by invoking business logic at the presentation layer is heavy code and also causes maintenance issues (new exceptions are introduced that the presentation layer does not know about). It seems best if the business logic triggers error handling on multiple value objects and "packs" them for presentation to deal with sequentially.

+2


source to share


2 answers


How about something as per your last 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 traverse all business objects, informing each of them about their activities. As each of its business is located in the logic of the business object in the business layer. You can handle errors, etc., by returning values ​​from the doBusinessLogic method, or by throwing exceptions for nasty occurrences.

If you are doing cross-business object logic (that is, logic that runs on multiple business objects), perhaps you can create a businessObjectManager class that can have methods that take one or more business objects and run them. So by putting the manager object in the business layer, you still keep all your business logic in the business layer.

+1


source


I ended up going with the old option and creating a lightweight generic "result set" that encapsulates the successes and failures of an activity in a collection of objects. Success or failure is tied to the corresponding object ID.

Each call to the business logic populates a package that is returned to the presentation tier. Exceptions thrown by the "take action on object" business logic loop are caught and the corresponding fault is added to the result set (tied to the object ID).

The presentation layer has a simple helper method that presents the user with the feedback contained in the result set.



It works really well.

I think the approach you take with this is what works best for your system.

0


source







All Articles