views:

54

answers:

0

I need implement business using nhibernate ORM I have two entites

Project (Id,Name,Effort)

Task (Id,Name,Effort)

I need update Effort in Project when Effort in Task changes. How to do it ? I have planed to use event system build in nhibernate but still don't know how to access to Project entity within Task Event

Here is EventListener

public class PreUpdateEventListener : IPreUpdateEventListener
  {
    public bool OnPreUpdate(PreUpdateEvent @event)
    {
      if(@event.Entity is Task)
      {
        Task task = @event.Entity as Task;
        int effort = Convert.ToInt32(GetState(@event.Persister, @event.OldState, "Effort"));
        if (effort != task.Effort)
        {
          task.Project.Effort += effort - task.Effort;                    
        }
      }
      return false;
    }

private object GetState(IEntityPersister persister, object[] state, string propertyName)
    {
      var index = Array.IndexOf(persister.PropertyNames,propertyName);
      if (index == -1)
      {
        throw new Exception();
      }
      return state[index];
    }
}