Validate Entity added to Child Collection...

Posts   
 
    
Posts: 32
Joined: 01-Oct-2007
# Posted on: 21-Dec-2007 10:47:38   

hi Chaps

What is the best way to acheive the following...

Structure Booking - 1:m - BookingRoom - n:1 - Room

When adding a BookingRoom item to the collection on the Booking I want to validate that there does not already exist a BookingRoom item for the same Room (can't book the same room twice on 1 booking).

However I want to do this inside the Booking entity so that it raises an exception and cancels the add when and invalid item is added.

It looked like I could catch the _bookingRoom.ItemAdding event inside the Booking and set cancel to true, but the only point I can see to add the event handler is inside InitClassMembers but the line


_bookingRoom.EntityAdding += new EventHandler<CancelableCollectionChangedEventArgs>(_bookingRoom_EntityAdding);

fails as _bookingroom is still null at that point.

Any ideas welcome...

Cheers

Matt

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Dec-2007 10:57:34   

IMHO This shouldn't be implemented inside the bookingEntity, as a bookingRoom entity can be inserted in the database from another place. What about a UC on the table?

Posts: 32
Joined: 01-Oct-2007
# Posted on: 21-Dec-2007 11:11:59   

Walaa wrote:

IMHO This shouldn't be implemented inside the bookingEntity, as a bookingRoom entity can be inserted in the database from another place. What about a UC on the table?

That is certainly one argument, but I really want to be able to catch it and warn the user before the save...

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 21-Dec-2007 12:41:05   

You can validate the BookingRoom entity using the OnValidateEntityBeforeSave (Entity Validation), preferably in a validator class, in which you should check the database for a duplicate record.

Also check the following thread which is some how relevant to your issue: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=9147

Posts: 32
Joined: 01-Oct-2007
# Posted on: 21-Dec-2007 15:58:23   

Thanks again. But what I really want to do is to validate the addition of the child entry at the time when it happens, and prevent it occurring if it does. This way the entity cannot get into an invalid state in the first place...

Posts: 254
Joined: 16-Nov-2006
# Posted on: 22-Dec-2007 22:44:39   

Going way back to your original solution attempt at using the EntityAdding events seems perfect feasible

If the member is null in InitClassMembers are you sure this at the correct point in this method, can you post the generated code for this method please?