Fetching strategies and complex hierarchies

ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST.

Fetching strategies can be applied to aggregates with complex hierarchies, not only to the first level inside the aggregate.

In order to optimize the queries that will be run, each element (entity or list) from the main hierarchy which has a list specified in its internal hierarchy should have the parent field configured; this is because separated selects are generated for the lists, and joins need to be made to reach the initial filters; if the element does not know its parent, the select will bring much more information than it should.

In the ComplexHierarchy action of the FetchingStrategiesController, we first instruct NHibernate to use a certain fetching strategy when executing the load task:

Task<CustomerOrderSet5LoadTask>().ShouldUseFetchingStrategy<CustomerOrderSet5FetchingForFetchingStrategiesComplexHierarchy>();

and after that we execute the task:

var orderSet = _customerOrderSet5LoadTask.Execute(1);

Here is how the fetching strategy looks:

public class CustomerOrderSet5FetchingForFetchingStrategiesComplexHierarchy: IFetchingStrategy<CustomerOrderSet5>
{
    public void ApplyStrategyOn(IMultiCriteriaFetcher<CustomerOrderSet5> repository)
    {
        repository
            .InitFetchingHierarchy()
            .WhichAlsoContainsList(p => p.CustomerOrders, u => u.CustomerOrderSet5)
            .WhichAlsoContainsList(p => p.OrderDetails)
            .WhichAlsoContainsEntity(p => p.Product5)
            .BackTo<CustomerOrderSet5>()
            .WhichAlsoContainsEntity(p => p.Client5)
            .WhichAlsoContainsList(p => p.Addresses)
            .BackTo<CustomerOrderSet5>()
            .WhichAlsoContainsEntity(p => p.PaymentType5);
    }
}

This is all that is required to load the CustomerOrderSet with Id 1 together with its orders, each order with its order details, each order detail with its product, also with the client and its addresses, and also with the payment type.

You can see here:

.WhichAlsoContainsList(p => p.CustomerOrders, u => u.CustomerOrderSet5)

how we tell FwkLight in the second parameter about the parent field for the CustomerOrders list, in order to load the list contained in each CustomerOrder (the list of order details) efficiently.

Advertisement

About this entry