Improving incremental code reuse

There is SOC between code invoked directly through an execution context and the application business logic code within an application. This knowledge is important to ensure that you can effectively reuse application logic easily, incrementally, and with minimal effort. It is the developer's responsibility to maintain this SOC by following patterns and guidelines set out in the chapters of this book and those you devise yourself as part of your own coding guidelines.

Reuse needs may not always be initially obvious. However, by following SOC and the design patterns in this book, when they do arise as your application evolves and new platform features are added, the process is more organic without refactoring, or, worse, copy and pasting the code!

To illustrate a reuse scenario, imagine that in the first iteration of the FormulaForce application, awarding points to a race contestant was accomplished by the user through an Award Points button on the Contestant detail page for each contestant. Also, the newsletter functionality was accessed by manually clicking the Send News Letter button on the Season detail page each month.

The class for the first iteration is shown in the next diagram. Note that you can already see the beginning of SOC between the Apex Controller logic and application business logic, which is contained in the classes ending in Service. The Service layer is discussed in more detail later in this chapter:

Now, imagine that in the second iteration of the application, user feedback requested that the awarding of points should also be available at the race level and applied in one operation for all contestants through a button on the Race detail page. In the following class diagram, you can see that the ContestService class is now reused by the RaceService class in order to implement the RaceController.awardPoints service method:

An additional enhancement was also requested to permit the newsletter functionality to use the platform scheduler to automatically issue the newsletter. The following diagram shows that the SeasonService class is now reused by both the SeasonNewsLetterSchedule class as well as the SeasonController class:

The preceding examples have shown that code from the first iteration can be reused either from the new application business logic or from a new execution context in the case of the scheduler. In the next chapter, we will take a further look at the Service layer, including its design guidelines that make this kind of reuse possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.85.33