Adding to the UoW from within a validator

Posts   
 
    
Posts: 134
Joined: 04-Mar-2005
# Posted on: 23-Mar-2006 18:54:40   

I'm using the Adapter model with a Unit of Work. As part of my app I need to maintain a sort of summary table. The summary table holds the resources that are on a project. As I add resources to tasks the resource should be added to the project if they're not already there. I'm doing this in the validator of the task-level resource. The problem I'm seeing is that even though I call the save of the project-level resource entity (which adds to the UoW and caches it) the entity is never saved.

My code goes something like:

  • Add various entities to the UoW extracting the UoW from the session cache and re-caching it each time an entity is added
  • Call the UoW commit
  • Task-Level Resource validator fires
  • As part of the validation add a Project-Level Resource and 'save' which writes to the UoW in the same way as other entities
  • Commit completes (without commiting the new Project-Level Resource to the DB)
  • Remove UoW from cache

So my questions are: - Would it be better to fire the Project-Level resource add from an event on the Task-Level resource, rather than from the validator? - Alternatively can I run a "pre-validation" which would insert the Project-Level resource into the UoW which I could then commit? - Am I crazy to be even attempting this? confused

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 24-Mar-2006 04:16:13   

The UoW may not execute your saves because it is already being committed. I would try saving these entities outside of the UoW. If this doesn't help then maybe post some of the code for adding to the unit of work and when the commits are called.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 24-Mar-2006 12:08:06   

The cause of this is that the save queues are already created. So the validation routine is too late. At the moment I'd advice you to do something like: task.Resources.Add(newResource); AddResourceToProject(newResource); and in AddResourceToProject() you add it if it's not already there.

This is necessary because you can't tap into the add method at the moment. This is a thing we can't address properly today, but we will definitely address this in v2, as we can then make the necessary architectural changes to make it possible. Effectively, what you want is: bind to a PreAdd() event (or something like that, AfterAdd can also be the one) of the Resources collection in Task. you add that code in Task. In that event handler, you then do: task.Project.AddResource(addedResource) or something like that, which means: you mange it from the task entity, not from code outside the task entity.

This is a known problem, and at the moment the workaround is to place the logic which assigns the resource into control of adding the resource to the project as well.

Frans Bouma | Lead developer LLBLGen Pro