Validating an Entity Collection

Posts   
 
    
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 18-Jan-2010 21:21:39   

Hi Guys,

I am using llblgen 2.6 SelfServing and SQL2k8 my question is how do you validate that an entity that is being inserted into a collection. In my case i have three entities. 1. Class Entity which defines classes students can register to take. 2. Student Entity which defines the student 3. Class Registration which defines a class a particular student has registered to take.

The relationship is as follows. Class to ClassRegistration 1:m Student to ClassRegistration 1:m

I would like to enforce a validation rule that a student cannot be registered twice for the same class. How do you enforce this from the ClassRegistration Entity.

Regards Kioko

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Jan-2010 02:40:25   

Hi Kioko,

Will try to illustrate with an example. This is an approximate code to validate that:

public void SaveClassRegistration(ClassRegistration toSave)
{
     // validate the student registration
     int times = (int)orderDetails.GetScalar(ClassRegistrationFieldIndex.ClassId, 
    null, AggregateFunction.Count,  (RegistrationClassFields.StudentId == toSave.StudentId));

      if (times >0)
     {
          throw new Exception("Dude already there");
     }

     toSave.Save();
}

As this is a cross-entity validation (multiple entities are involved) you must do that in your business class (or inside your entity collection as a custom method). Here is more information about Validation.

David Elizondo | LLBLGen Support Team
kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 19-Jan-2010 17:55:58   

Hi,

Thanks for your response. But i am still confused as to where to put this validation code. You see the my parent object is the Class which as a collection of ClassRegistration Entities of which each should have only one student registered for such a class. What i am looking for is any time someone tries to register a student who is already registered for the class an exception is thrown which i can capture in the user interface. My thinking is that ideally this code should be put in the collection class. I really wish i knew where.

Kioko

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-Jan-2010 18:07:02   

In this case you need to provide your own "AddClassRegistration" method, either in a user code region or a partial class - both of these are detailed in the documentation.

You can then perform the validation, and if it passes, add the new item to the parent's collection, or throw an exception if it fails.

Matt

kioko
User
Posts: 29
Joined: 12-Dec-2009
# Posted on: 19-Jan-2010 19:30:35   

Thanks Matt,

I have figured it out. I have overridden the OnEntityAdding method and added my validation check there. When adding entities to the collection classes i ensure that i first populate them with their values before i add them to the collection. It works the way i want it to work.

Kioko