The Unit Of Work scope

To make the most effective use of the Unit Of Work's ability to coordinate database updates and transaction management, maintain a single instance of the Unit Of Work within the scope of the Service method, as illustrated in the previous section.

If you need to call other classes that also perform database work, be sure to pass the same Unit Of Work instance to them, rather than allowing them to create their own instance. We will explore this a little later in this chapter.

It may be tempting to maintain a static instance of a Unit Of Work so that you can refer to it easily without having to pass it around. The downside of this approach is that the access to becomes broader than a given Service method execution scope, since Apex can be invoked from many different places its hard to guarantee it will always be from within a Service method. Even if a static is used, code registering records with a static Unit Of Work must still depend on an outer Apex code unit committing the work. Finally, it also gives a false impression that the Unit Of Work is useable after a commit has been performed.
..................Content has been hidden....................

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