Update restriction in SaveEntity method

Posts   
 
    
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 07-May-2007 11:50:47   

LLBLGen v.2.0.0.0 / .NET 2 / Adapter

Hi guys.

I'm playing around with the update restriction predicate that can be passed to the Adapter.SaveEntity method. I have the following objects:

Airplane with one property IsTakingOff Passenger with one property IsInWashRoom

There is a 1:m Relation between Airplane and Passenger. Now there should be an update restriction so that the property Airplane.IsTakingOff cannot be set to true as long as any passengers are in the washrooms.

Airplane.IsTakingOff = true;
PredicateExpression predicate = new PredicateExpression();
predicate.Add(PassengerFields.IsInWashRoom == false);
mAdapter.SaveEntity(Airplane, true, predicate, true);

It throws the following exception at runtime: _ System.Data.SqlClient.SqlException The multi-part identifier "xxx.dbo.Passenger.IsInWashRoom" could not be bound. _

Apparently, I misunderstand something. How can I check the state of linked entities when saving an entity?

Thanks in advance for your help!

Aurelien avatar
Aurelien
Support Team
Posts: 162
Joined: 28-Jun-2006
# Posted on: 07-May-2007 15:09:21   

Hi,

The predicate should be set on the same entity witch is saved.

As i far know, there is on way to check the state of linked entities when saving an entity.

KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 08-May-2007 08:22:59   

Is this really true?? frowning Is there no way to check an entity from another type before saving? I guess this is quite a common problem. It's the same like an order can only be shipped if all order items are in stock. Without an O/R mapper I would write a trigger for that. But now I have LLBLGen and I really wanna get rid of DB logic. Is there really no way to do that?

confused

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-May-2007 08:57:31   

Is possible with UpdateEntitiesDirectly. For instance, here I want to update the order 10248 field IsShipped=true only if there are products in stock:

// order to be updated
int orderIdToUpdate = 10248;

// values to update
OrdersEntity valuesToUpdate = new OrdersEntity();
valuesToUpdate.IsShipped = true;

// making possible the related entity filter update
IRelationPredicateBucket updateRestrictionFilter = new RelationPredicateBucket();
updateRestrictionFilter.Relations.Add(OrdersEntity.Relations.OrderDetailsEntityUsingOrderId);
updateRestrictionFilter.Relations.Add(OrderDetailsEntity.Relations.ProductsEntityUsingProductId);

// restrict the update action: only the order orderIdToUpdate and only orders that have products in stock
updateRestrictionFilter.PredicateExpression.Add(OrdersFields.OrderId == orderIdToUpdate);
updateRestrictionFilter.PredicateExpression.Add(ProductsFields.UnitsInStock > 0);

// make the update
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.UpdateEntitiesDirectly(valuesToUpdate, updateRestrictionFilter);
}

The key is to provide the PK filterElement updateRestrictionFilter.PredicateExpression.Add(OrdersFields.OrderId == orderIdToUpdate); so only the order you want will be updated.

David Elizondo | LLBLGen Support Team
KIT
User
Posts: 59
Joined: 04-Apr-2007
# Posted on: 08-May-2007 11:20:30   

Thanks for your answer and the code example. That was helpful!

However, with such an implementation it is hard to make a business layer with its own business rules. I'd like to implement all this rules in a validator class. Apparently, that's not possible. Another problem is that this specific field (e.g. IsShipped) should not be updateable in the upper layers. How can I be sure that only the business layer updates that field?

If somebody has an idea about how to implement a business layer with such rules I wouldn't mind if he or she can post it. simple_smile