prevent validation when fetch entity

Posts   
 
    
James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 04:53:10   

I am using the following code to fetch an entity CustomerEntity _Customer=new CustomerEntity(Customerid); using (DataAccessAdapterBase adapter=GetCompanyDataAccessAdapter()) { bool found=adapter.FetchEntity(_Customer, prefetchPath, null, fieldList); if (!found) throw new RecordNotFoundException("Invalid Customer"); }

also set the validation for this entity

but it will raise my validation code such as

public override bool ValidateFieldValue(IEntityCore involvedEntity, int fieldIndex, object value) { }

how can I prevent this validation when fetch data from database?

I find a tip: OnValidateEntityAfterLoad, override in DatabaseAdapter, but it does't work still raise that validation, pls help

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Dec-2011 08:32:01   

Sorry, I'm not following.

Which validation is raised, could you please post the exception text? Do you have a validation class for this entity?

James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 08:39:39   

yes,already set validator for this entity when I fetch data record from database,LLBL will rasie validation and throw my custom exception, my validator just want to validate the entity before saving into database, when fetch entity, no need to validation

sample code,

here I fetch customer entity, have to create a new entity CustomerEntity _Customer=new CustomerEntity(Customerid); using (DataAccessAdapterBase adapter=GetCompanyDataAccessAdapter()) { bool found=adapter.FetchEntity(_Customer, prefetchPath, null, fieldList); if (!found) throw new RecordNotFoundException("Invalid Customer"); }

after fetch ,it will raise this validation, of course _Customer is new ,so it throw my custom exception public bool ValidateCustomerId(string customerId, CustomerEntity customer) { if (customer.IsNew && !string.IsNullOrEmpty(customerId)) { //check existing customer } } }

so, I just want to prevent validation after fetch customer entity, please help I am using LLBL Gen 3.1

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Dec-2011 09:19:08   

How does yur validator class look like? Could you please attach it? Do you handle ValidateEntityAfterLoad() in there.

James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 09:25:21   

in customer entity,I use this code to set validator protected override void OnInitialized() { this.Validator =CustomerValidator; }

the validator calss look like this [Serializable] public partial class CustomerValidator : ValidatorBase {

public override void ValidateEntityBeforeSave(IEntityCore involvedEntity)
    {
        base.ValidateEntityBeforeSave(involvedEntity); 
     }


 public override bool ValidateFieldValue(IEntityCore involvedEntity, int fieldIndex, object value)
    {
        bool result = base.ValidateFieldValue(involvedEntity, fieldIndex, value);

        if (!result) return false;

        switch ((CustomerFieldIndex)fieldIndex)
        {
            case CustomerFieldIndex.CustomerId:
                return this.ValidateCustomerId((string)value, (CustomerEntity)involvedEntity);   

        }

}

public bool ValidateCustomerId(string customerId, CustomerEntity customer) { if (customer.IsNew && !string.IsNullOrEmpty(customerId)) { //for new record, the existing in database, //if exist,throw duplicate exception

        }

        return true;
    }

}

create a new entity in order to fetch from database fetch customer entity, have to create a new entity CustomerEntity _Customer=new CustomerEntity(Customerid); using (DataAccessAdapterBase adapter=GetCompanyDataAccessAdapter())

_Customer is new entity,so it raise validation ,call ValidateCustomerId(), throw duplicate exception

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Dec-2011 09:31:40   

According to your code, ValidateCustomerId() could never throw an exception for a fetched entity. (IsNew = false).

Could you please ost the exception text and stack trace?

James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 09:44:50   

for code CustomerEntity _Customer=new CustomerEntity(Customerid); using (DataAccessAdapterBase adapter=GetDataAccessAdapter()) { bool found=adapter.FetchEntity(_Customer, prefetchPath, null, fieldList); if (!found) throw new RecordNotFoundException("Invalid Customer"); }

let me show the stack trace

string CustomerId="Northwind"; CustomerEntity _Customer=new CustomerEntity(CustomerId); //1

go to public CustomerEntity(System.String userGroup):base("CustomerEntity") { InitClassEmpty(null, null); this.CustomerId= customerId; //2 }

go to

public virtual System.String UserGroup { get { return (System.String)GetValue((int)UserGroupFieldIndex.UserGroup, true); } set { SetValue((int)UserGroupFieldIndex.UserGroup, value); } //3 }

public override bool ValidateFieldValue(IEntityCore involvedEntity, int fieldIndex, object value) { bool result = base.ValidateFieldValue(involvedEntity, fieldIndex, value); if (!result) return false; CustomerEntity customer = (CustomerEntity)involvedEntity;

        switch ((CustomerFieldIndex)fieldIndex)
        {
            case CustomerFieldIndex.CustomerId:
                return ValidateCustomerId((string)value, customer);  //4
         }

public bool ValidateCustomerId(string customerId, CustomerEntity customer) { //of course, the customer is new when fetch its from database if (customer.IsNew && !string.IsNullOrEmpty(customerId)) { //check existing customer //5 //because the customer "Northwind" already exist in database, should throw exception } } }

I have mark the sequence of call stack in number. it prevent me fetch existing data because it already exist and IsNew=true, check existing record failed

I just want to prevent validation after fetch customer entity so I can execute the following code to get my customer

using (DataAccessAdapterBase adapter=GetDataAccessAdapter()) { bool found=adapter.FetchEntity(_Customer, prefetchPath, null, fieldList); if (!found) throw new RecordNotFoundException("Invalid Customer"); }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Dec-2011 10:46:07   

Something is not right here.

First of all:

public virtual System.String UserGroup
{
    get { return (System.String)GetValue((int)UserGroupFieldIndex.UserGroup, true); }
    set { SetValue((int)UserGroupFieldIndex.UserGroup, value); } //3
}

What's this have to do with anything?

2nd: I was expecting to see the stack trace and the exception text as they appear to you in VS output window & stack trace window.

3rd: Why do you throw an exception if the entity isNew and the Id was set?! Why do you need to validate for that?

James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 11:01:59   

Why do you throw an exception if the entity isNew and the Id was set?! Why do you need to validate for that?

I think this is the key point for this isse.

when add a new record,such as a new "Northwind" customer, customer id is "Northwind" and this is the tables's key field.

before save it,we should check the database to see whether it already in the database, if it does, should throw an exception and prevent use add this "Northwind" customer. if don't do this, absolutelly it will throw duplicate key error from database

that's why I add the following validation code

public bool ValidateCustomerId(string customerId, CustomerEntity customer) { if (customer.IsNew && !string.IsNullOrEmpty(customerId)) { //check existing customer } } }

customer.IsNew =false, which it means it is not an new entity, no need to check in database

for LLBL 2.6, the following code works fine. CustomerEntity _Customer=new CustomerEntity(Customerid); using (DataAccessAdapterBase adapter=GetDataAccessAdapter()) { bool found=adapter.FetchEntity(_Customer, prefetchPath, null, fieldList); if (!found) throw new RecordNotFoundException("Invalid Customer"); }

why LLBL 3.1 activate validation when fetch an record? did I mising something setting? am I using the right way to fetch an entity from database? that code works fine with LLBL 2.6, rage

handle ValidateEntityAfterLoad() in there,could tell me something more detail?

James Li
User
Posts: 6
Joined: 08-Dec-2011
# Posted on: 08-Dec-2011 11:19:08   

finally,resolve this issue in public CustomerEntity(System.String userGroup):base("CustomerEntity") { InitClassEmpty(null, null); this.CustomerId= customerId; //2 }

replace this.CustomerId= customerId; //2

with

this.Fields[(int)CustomerFieldIndex.CustomerId].CurrentValue = customerId;

this will not raise validation and fetch data successfully seams my template is missing and have to change LLBL template before generate the code

thank you for your help. simple_smile