Print Page | Close Window

Checking an entity for errors

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1941
Printed Date: 10-Jun-2026 at 5:28pm


Topic: Checking an entity for errors
Posted By: cjohnson84
Subject: Checking an entity for errors
Date Posted: 06-Jul-2010 at 6:06am
I have bound an observable collection of entities retrieved from my database via my DevForce 2010 data model to a ComponentOne DataGrid.  I have the "Validation Attribute Mode" set to "DotNetValidation" in my data model.  This has allowed me to get validation working with the validation built into the grid.
 
When I edit the collection bound to the grid I see the validation errors.  We are allowing the users to modify the contents of the grid and then click a Save button which will commit the changes back to the database.
 
The issue I'm having is if the user clicks the Save button and validation errors exist (they are displayed visually) how do I check for the validation errors and not allow the commit operation?
 
I was thinking I could loop through the observable collection and check each entity to see if it has errors since ultimately the Entities inherit from INotifyDataErroInfo (in the EntityWrapper class).  I can't seem to find the implementation of the HasErrors property though and where this property of the interface is exposed.
 
I guess ultimately then my question is how do I check to see if an entity "HasErrors"?
 
 
 



Replies:
Posted By: GregD
Date Posted: 06-Jul-2010 at 6:00pm
DevForce automatically runs instance verification, server-side, against all entities submitted for a save.

If you want to check client-side, you can do so in an EntityManager.Saving handler. In that, you have access to the collection of entities being saved, and can simply order instance verification against each.


_em1.Saving += new EventHandler<EntitySavingEventArgs>(_em1_Saving);
_em1.ExecuteQueryAsync<Customer>(customersQuery, GotCustomers, null);

...

void _em1_Saving(object sender, EntitySavingEventArgs e) {
foreach (Entity anEntity in e.Entities) {
    anEntity.EntityAspect.EntityManager.VerifierEngine.Execute(anEntity);
}
}


If any instance verification returns a VerifierResult that indicates a problem, you just set the Cancel property of the EntitySavingEventArgs object to true, and the save won't be submitted.


Posted By: cjohnson84
Date Posted: 07-Jul-2010 at 11:24am
Thank you for clearing that up for me Greg!


Posted By: jwooten
Date Posted: 31-Aug-2010 at 3:31pm
I have the following scenario in a Silverlight 4 application using DevForce 2010...
 
I have 2 entities, say "Employee" and "Office"...  And "Employee" table has non-nullable column "PrimaryOfficeID" with foreign key relation to "OfficeID" column in "Office" table....
 
When I create new "Employee" entity in the application and then save it without assigning it a primary office... 
 
Why might it be that upon saving the new Employee entity that the result of executing verification on that entity (as shown above) does not reflect an error?
 
I assume this is because the PrimaryOffice property contains a null Office entity which is valid.  Is that correct?
 
If so, how do I make it so that attempting the save without assigning a primary office would cause this verification to fail? 
 
 
 


Posted By: ting
Date Posted: 01-Sep-2010 at 7:02pm

Since the PrimaryOfficeID is non-nullable, it has to be assigned a default value on creation.  There is no required entity / foreign key validator in .NET or DevForce (perhaps we should create one), and the required field validator doesn't apply because the field is non-nullable.  So, there is nothing to say that the default value fails a verification check.

You can create an instance verifier on Employee that makes sure that:
  this.PrimaryOffice.EntityAspect.IsNullEntity == false

You can also create it as a property verifier on Employee.PrimaryOffice that does the same thing.

Here's the link to read about verification and see some samples:

http://drc.ideablade.com/xwiki/bin/view/Documentation/Topic_Validation - http://drc.ideablade.com/xwiki/bin/view/Documentation/Topic_Validation

If you have trouble, we can have someone write up a quick example.




Print Page | Close Window