Validation using volatile data

Validation using volatile data

Old forum URL: forums.lhotka.net/forums/t/2586.aspx


JdZ posted on Monday, March 26, 2007

Hello everybody,

I have the following issue with the CSLA framework that I hope you may be able to help me with.

Currently, I have two root objects: Activity and Employee. Since this is a sort of planning application, I want to be able to assign an Employee to an activity. The application will be responsible for checking that an Employee is available to perform the activity.

The Activity object has an AssignedEmployee property. When I assign an Employee to the AssignedEmployee property, I check the availability of the Employee using a custom validation method. This is were the "trouble" starts.

1) A significant amount of time may pass between the Activity.AssignedEmployee = SelectedEmployee call and the Activity.Save() call. In this time someone else may have assigned and saved the Employee to another Activity as well. If this would be the case, the former Activity object would have become invalid as the Employee is in fact no longer available for the job. How can I make sure the Activity object is revalidated when Save() is called, even if it did pass validation at assignment? I would prefer this validation to be performed in the context of a transaction. Can I just call ValidationRules.CheckRules inside DataPortal_Update?

2) For the validation rule, I took the approach of the book, and used a IHoldEmployee interface and a static ValidAvailableEmployee method which checks the availability in the underlying database. Because a second connection is opened to the database, the hated DTC error appears. Can I use the LocalContext object to work around this?

Thank you very much in advance,

Jos

ajj3085 replied on Monday, March 26, 2007

JdZ:
1) A significant amount of time may pass between the Activity.AssignedEmployee = SelectedEmployee call and the Activity.Save() call. In this time someone else may have assigned and saved the Employee to another Activity as well. If this would be the case, the former Activity object would have become invalid as the Employee is in fact no longer available for the job. How can I make sure the Activity object is revalidated when Save() is called, even if it did pass validation at assignment? I would prefer this validation to be performed in the context of a transaction. Can I just call ValidationRules.CheckRules inside DataPortal_Update?


Well, you can't really check until you hit the db on save.  I think the only way to be sure is to do your save, then see how many rows have that assigned employee.  If there's moer than one, thrown an exception and rollback your transaction.

JdZ:
2) For the validation rule, I took the approach of the book, and used a IHoldEmployee interface and a static ValidAvailableEmployee method which checks the availability in the underlying database. Because a second connection is opened to the database, the hated DTC error appears. Can I use the LocalContext object to work around this?

Yes, that's why the LocalContext was created.   Put the connection there, and have your object check if there's an existing connection to use already.  But I think if you do the check last, you won't need to call this rule again, thus avoiding this problem.

Curelom replied on Monday, March 26, 2007

If you happen to be hitting Sql Server 2005, you have ther option of using Sql/Command Notification, where the database will notify the program that there was a change in the data of the query.  This also avoids the DTC error, as the notification link to the database isn't a sql connection.

Copyright (c) Marimer LLC