Question

...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.

Was it helpful?

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.

OTHER TIPS

Ultimately I went for the former option and created a lightweight generic "result bundle" class that encapsulates successes and failures of an action on a collection of objects. A success or failure is keyed to the corresponding object identifier.

A bundlesis populated inside each business logic call and returned to the view layer. Exceptions thrown in the 'perform action on object' business logic loop are caught and a corresponding failure added to the result bundle (keyed to the object ID).

The view layer has a simple helper method that presents the feedback contained in the result bundle to the user.

This works very well.

I think the approach you take with this is whatever fits best with your system.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top