Validation, need suggestion

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 17-Aug-2005 18:40:45   

I'm new to LLBLGen, and I'm now writing an order entry winform application with remoting to evaluate if LLBLGen suitable for our projects or not. I'm confused how to do validation in my app.

In my application, it has a grid (Infragistics UltraGrid) for user to input item details, which has several columns, such as item no., description, price...

When user create a new line and input an item no., system will validate the item no. and retrieve the description and default price to set the value to description and price column. And uer is allowed to input a new price afterwards.

I've created a entity validator class for order line entity as the following:

public override bool Validate(object containingEntity) { OrderLineEntity orderLine = (OrderLineEntity )containingEntity; ValidateItemNo(line ); return true; }

public OrderLineEntity ValidateItemNo((OrderLineEntity orderLine ) { if (!orderLine .Fields["ItemNo"].IsChanged) return orderLine ; ItemManager itemValidator = new ItemManager(); ItemEntity item = itemManager.GetValidItem(orderLine.ItemNo); orderLine.Description = item .Description; orderLine.Price= item .Price; return orderLine; }

in my windows form, I call the validation logic by the grid event like this:

void grdDetails_BeforeCellUpdate(object sender, Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs e) { OrderLineEntity orderLine = (OrderLineEntity)DetailsBindingSource.Current; OrderLineValidator lineValidator = new OrderLineValidator (); switch (e.Cell.Column.Key) { case "ItemNo": orderLine.ItemNo = e.NewValue.ToString(); orderLine.Fields = lineValidator.ValidateItemNo(orderLine).Fields; DetailsBindingSource.ResetCurrentItem(); break; } }

I don't want to duplicate the validation logic in different tier, I create the entity validator class for both save operation of entity and validation for PL. Because I'm using remoting in my application, I create a validate method for each field and return the entity to PL in order to update the screen value.

I find a problem, when uese input an item no., the default price can be retrieved and display on screen, if the user edit the price, and save it. Since the validation logic will run again, the default price is retrieved again and the price entered by user is lost. I'm thinking to comparing the old value and new value of item no. before setting the price, but I cannot find how to get the old value in my entity.

Can anyone comment my coding? or give me some suggestion how I can do it in a better ways....

Thank you very much!!!

JimFoye avatar
JimFoye
User
Posts: 656
Joined: 22-Jun-2004
# Posted on: 17-Aug-2005 23:08:58   

I'm now writing an order entry winform application with remoting to evaluate if LLBLGen suitable for our projects or not.

Frans gives you different ways to validate, he'll probably answer you shortly on that. But let me assure you, LLBLGen IS suitable for your project! sunglasses You won't want to go back to ADO.NET after working with it.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 18-Aug-2005 03:29:46   

It looks as though you are using the validation to actually set the values of the fields instead of just checking them to ensure they are "valid". What I would suggest is doing something like this.


// This checks that the description for the item has not changed and that the item
// has a price greater than 0
public override bool Validate(object containingEntity)
{
    OrderLineEntity orderLine = (OrderLineEntity )containingEntity;
    if (!orderLine.Fields["ItemNo"].IsChanged) return true;
    ItemManager itemValidator = new ItemManager();
    ItemEntity item = itemManager.GetValidItem(orderLine.ItemNo);
    if(orderLine.Description != item.Description)
        return false;
    if(orderLine.Price <= 0)
        return false;
    return true;
}

void grdDetails_BeforeCellUpdate(object sender, Infragistics.Win.UltraWinGrid.BeforeCellUpdateEventArgs e)
{
    OrderLineEntity orderLine = (OrderLineEntity)DetailsBindingSource.Current;
    OrderLineValidator lineValidator = new OrderLineValidator ();
    orderLine.EntityValidatorToUse = lineValidator;
    switch (e.Cell.Column.Key)
    {
        case "ItemNo":
            orderLine.ItemNo = e.NewValue.ToString();
            // Retrieve the price from the datagrid and set it as the price of the orderLine
            orderLine.Price  = e.Price.ToString();
            DetailsBindingSource.ResetCurrentItem();
            break;
    }
}