Skip to content

This library provides tooling for post-processing and delayed execution with possibility to merge operations.

License

Notifications You must be signed in to change notification settings

BrandEmbassy/unit-of-work

Repository files navigation

Unit of work

This library provides tooling for post-processing. Lets explain it on the example:

You have some domain entity (lets call it a Document). And in your logic you do a lot of different changes and data transformations and you want to persist those changes for example into ElasticSearch database.

The problem is that if you save it right in place after the change, you may end up with multiple writes.

By using this library you can track all of those changes and merge them.

function doBoringStuff(Document $document): UnitOfWork {
	$document->changeBoringStuff();
	$unitOfWork = new UnitOfWork();
	$unitOfWork->registerOperation(new SaveDocumentOperation($document));
	
	return $unitOfWork;
}

function doFunkyStuff(Document $document): UnitOfWork {
	$document->changeFunkyStuff();
	$unitOfWork = new UnitOfWork();
	$unitOfWork->registerOperation(new SaveDocumentOperation($document));
	
	return $unitOfWork; 
}


$boringWork = doBoringStuff($document);
$funkyWork = doFunkyStuff($document)
$unitOfWork = $boringWork->concatenate($funkyWork);

// Every Processor has its own Accessor for Dependecy Injection Container
$accessors = [new SaveDocumentOperationProcessorAccessor()]; 

$naiveExecutor = new NaiveUnitOfWorkExecutor($accesors);
$executor = new ReducingUnitOfWorkExecutor($naiveExecutor); // Decorator pattern to separate merging logic

// And document will be saved only once if your SaveDocumentOperation is implemented
// as mergable. See: `MergeableOperation` as example.
$executor->execute($unitOfWork); 

Example of usage inside Nette Dependecy Injection container:

unitOfWorkExecutor: BrandEmbassy\UnitOfWork\ReducingUnitOfWorkExecutor(
    BrandEmbassy\UnitOfWork\NaiveUnitOfWorkExecutor([@saveDocumentOperationProcessorAccessor])
)

- Foo\Bar\SaveDocumentOperationProcessor
saveDocumentOperationProcessorAccessor: Foo\Bar\SaveDocumentOperationProcessorAccessor

About

This library provides tooling for post-processing and delayed execution with possibility to merge operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages