Where should I put validation logic?

Posts   
 
    
Barry
User
Posts: 232
Joined: 17-Aug-2005
# Posted on: 28-Nov-2005 04:56:52   

I'm thinking where is the best place to place my validation logic, I do field level validation at set method of each property as the following, but I don't know the code is executed everytime I fetch the entity, however, the similar code in a entity of child collection is not executed when fetched by preftech path. confused


public virtual System.String OrderNo
{
    get
    {
        object valueToReturn = base.GetCurrentFieldValue((int)OrderFieldIndex.OrderNo);
        if(valueToReturn == null)
        {
            valueToReturn = TypeDefaultValue.GetDefaultValue(typeof(System.String));
        }
        return (System.String)valueToReturn;
    }
    set {
        if (OrderNo != value)
        {
            if (IsNew)
            {
                // check if order no. is already used or not
                OrderManager manager = new OrderManager();
                manager.GetOrder(value);
                throw new Exception("Order No. is already existed");
            }
        }

        SetNewFieldValue((int)OrderFieldIndex.OrderNo, value);
    }
}

public virtual System.String CustomerNo
{
    get
    {
        object valueToReturn = base.GetCurrentFieldValue((int)OrderFieldIndex.CustomerNo);
        if(valueToReturn == null)
        {
            valueToReturn = TypeDefaultValue.GetDefaultValue(typeof(System.String));
        }
        return (System.String)valueToReturn;
    }
    set {
        if (CustomerNo != value)
        {
            try
            {
                // check customer no., copy customer name to CustomerName property if it is valid
                CustomerManager manager = new CustomerManager();
                CustomerEntity customer = manager.GetCustomer(value);
                this.CustomerName = customer.CustomerName;
            }
            catch(Exception ex)
            {
                throw new Exception("Invalid customer no.", ex);
            }
        }

        SetNewFieldValue((int)OrderFieldIndex.CustomerNo, value);
    }
}

For some collection validation rules, I place my code in ListChanged event as the following:


OrderLines.ListChanged += new ListChangedEventHandler(OrderLines_ListChanged);

void OrderLines_ListChanged(object sender, ListChangedEventArgs e)
{
    if (e.ListChangedType == ListChangedType.ItemAdded)
    {
        // auto increment and assign line no for new entity
        OrderLineEntity line = (OrderLineEntity)QueryColumns[e.NewIndex];
        if (line.IsNew && line.LineNo == 0)
        {
            int newLineNo = 1;
            foreach(OrderLineEntity existedLine in line.Order.OrderLines)
            {
                if (existedLine.LineNo > newLineNo) newLineNo = existedLine.LineNo + 1;
            }
            line.LineNo = newLineNo;
        }
    }
    else if(e.ListChangedType == ListChangedType.ItemChanged)
    {
        // check if item no. is blank or not
        OrderLineEntity line = (OrderLineEntity)QueryColumns[e.NewIndex];
        if (line.ItemNo == string.Empty) throw new Exception("Item No. is required");
    }
}

Could anyone give me some comments or sugesstion on how to perform validation? Thank you very much!!!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 28-Nov-2005 14:37:37   

Please read the section "Using the generated code - Validation per field or per entity" in the LLBLGen Pro documentation.

This section describes ways to use & extend the generated validation schema.