Java Reference
In-Depth Information
The call to
FetchConfiguration.addField()
tells Kodo
JDO
to eagerly load the
PendingOrder
's line items and restaurant.
To dynamically configure object loading, the application must call
FetchCon-
figuration.addField()
with the required fields prior to calling
Persistence-
Manager.getObjectById()
. For the Place Order use case, one option is for the
PendingOrderRepository
to define multiple versions of the
findPending-
Order()
method that calls
FetchConfiguration.addField()
with the appropri-
ate fields. For example, the
findPendingOrderWithLineItemsAndRestaurant()
method, which is called by
PlaceOrderService.updatePaymentInformation()
,
would add the
PendingOrder.lineItems
and
PendingOrder.restaurant
fields
to the active fields using code similar to that shown earlier.
However, the trouble with this approach is that it requires changing the
domain model. Although some objects are loaded because they are required by
the business logic, other objects are loaded because they are needed by the
UI
. It
is undesirable to couple the core business logic to the
UI
design because we might
have to repeatedly change the business logic to reflect changes to the
UI
. Further-
more, it also makes the domain model less reusable. To avoid these problems, we
have to separate the business logic from the code that configures object loading.
Let's see how to do this.
Using AOP to dynamically configure eager loading
We can use a Spring
AOP
interceptor to separate the business logic from the code
that configures object loading. The interceptor, which intercepts requests to the
business logic, adds the fields that must be eagerly loaded to the
FetchConfigu-
ration
. Listing 5.8 shows the
KodoFetchGroupInterceptor
, which is a Spring
AOP
interceptor that configures the
FetchConfiguration
based on the method
that is invoked. When the interceptor is invoked, it uses the method name to
determine which fields to add to the
FetchConfiguration
. The set of fields to
use for a given method invocation is specified by the map that is passed to the
interceptor's constructor. The key of each map entry is the name of a service
method, and the value is a list of fully qualified field names.
Listing 5.8
KodoFetchGroupInterceptor
public class KodoFetchGroupInterceptor
implements MethodInterceptor {
private PersistenceManagerFactory pmf;
private Map fetchGroupConfig;
Search WWH ::
Custom Search