c.Coarse Grained Lock Pattern

  1. Motivation
    1. Locks a set of related objects with a single lock.
  2. Summary
    1. A Coarse-Grained Lock is a single lock that covers many objects.
    2. It not only simplifies the locking action itself but also frees you from having to load all the members of a group in order to lock them.
  3. When to Use
    1. The most obvious reason to use a Coarse-Grained Lock is to satisfy business requirements. This is the case when locking an aggregate. Consider a lease object that owns a collection of assets. It probably doesn't make business sense for one user to edit the lease and another user to simultaneously edit an asset. Locking either the asset or the lease ought to result in the lease and all of its assets being locked.
    2. A very positive outcome of using Coarse-Grained Locks is that acquiring and releasing lock is cheaper. This is certainly a legitimate motivation for using them. The shared lock can be used beyond the concept of the aggregate, but be careful when working from nonfunctional requirements such as performance. Beware of creating unnatural object relationships in order to facilitate Coarse-Grained Lock.
  4. Implementation
    1. The first step in implementing Coarse-Grained Lock is to create a single point of contention for locking a group of objects.
    2. This makes only one lock necessary for locking the entire set. Then you provide the shortest path possible to finding that single lock point in order to minimize the group members that must be identified and possibly loaded into memory in the process of obtaining that lock.
    3. Using a root lock as a Coarse-Grained Lock makes it necessary to implement navigation to the root in your object graph. 
  5. References
    1. http://martinfowler.com/eaaCatalog/coarseGrainedLock.html