My question is in the header. I would like to know is there architectural solution that enables to perform nested transactions on the business model. Let me to explain - the standard flow for web service is
- validate input data
- process request
- persist results
- write response
However, there are cases when i Can not validate data before request is processed - meaning i should validate system after request execution - lets call this case inverted validation case
. If system becomes invalid - i would like to roll back changes in business model to the initial state. I should be noted that system should has good performance. Processing of request consumes big amount of computational power - so it is impossible to perform changes twice - once for validation and once for actual request processing. At the moment i am using temporary storage for results of request processing, than i perform validation if it passes - i apply data from temporary storage to business model. However, this solution is working great if i have one transaction - meaning i have to perform inverted validation
only once, but is there another inverted validation
- this turns into spaghetti code.
Here is example: Lets say we have logistics service that gets as input list of cargos with there amounts and outputs - list of warehouses for each cargo where cargo can be stored. Each warehouse can store only some amount of cargo another warehouse will store the rest. For example warehouse A store 10% of cargo, warehouse B store 50% and warehouse C 40% of cargo. To compute the list of warehouses - server should perform heavy computations. Request should be rejected if cargo can not be stored in less than 10 warehouses. Also it can turn out that some cargo supplier has not enough founds - so this supplier should be rejected. NOTE system should consider that warehouse capacity can be changed by already processed cargos from current request. So here we have two transactions - one big transaction for whole request and two nested - for case when there no warehouses and when there no funds - as you can see you can not apply changes to the business model right away. NOTE: the problem in the example is a toy problem - i created it only to show the actual problem.
entity
right away - as it can turn out at the end of transaction that this changes are invalid. At the same time i need accamulate computed data in temporary storage. I would like to getsafepoints
(like for DB transactions) in EF or JPA for business entities - when you can rollback you Unit of work to some point in the past.