jeudi 26 mars 2015

Accessing intermittently available resources with transactions and post-access cleanup in a generalized, functional, composable way


The following psuedo-code illustrates what I'd like to be able to do. Is there a way to accomplish something like this in Scala?:



trait IntermittentlyAvailableResource
trait AlwaysAvailableResource
trait Transactional
trait CleanupAfter
sealed abstract class TransactionResults[A](val result: A)

class Something {
def grabItemFromQueue: Future[Item] = ...
def ifNoResourceAvailable = ...
def queueItemCleanup(item: Item) = ....
def queueItemTransactionCommit(item: Item) = /* delete from queue*/

val QueueGrabber grabs an IntermittentlyAvailableResource and performs an operation with Transactional and CleanupAfter ...
(ifResourceNotAvailable execute ifResourceNotAvailable)(cleanup execute queueItemCleanup)(commit execute queueItemTransaction)

def something = {
QueueGrabber.with{
resource =>
//operate on resource without worrying about cleanup, commit, availability, etc
}

}
}




Aucun commentaire:

Enregistrer un commentaire