How to check for entry of duplicate PK

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 12-Sep-2007 20:53:36   

Hi, Using V2.0, Adapter, VB

I have a grid (UI layer) thats populated with its collection via a Manager Class (BL), and validation is within a ValidatorClass (DAL). After a row is added I can confirm within the validator that the row is new by:

Dim toValidate As ISORegionEntity = CType(involvedEntity, ISORegionEntity)
If toValidate.IsNew = True Then
...

But what is the fastest and easiest way of checking within the ValidatorClass, if the new PK entered in the grid is a duplicate? Note that items in the grid may not yet have been persisted.

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-Sep-2007 05:03:20   

Hi Markiemac,

As you want validate multiple entities, you can't use Validator Class. However you could validate that at your GUI or BL using EntityCollection.FindMatches(IPredicateExpression). That method should tell you if the PK already exists in your collection.

Is that helpful in your scenario?

David Elizondo | LLBLGen Support Team
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 13-Sep-2007 13:36:19   

Daelmo, Thats helpful. simple_smile Thanks

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 13-Sep-2007 14:13:19   

this would only work with data in memory. you would also need to query the db to determine if the PK exists. here's a quick, untested example.

int pk = 2;
object exists = adapter.GetScalar(MyTableFields.PKField, null, AggregateFunction.Count, MyTableFields.PKField == pk);
if (object != DBNull.Value)
{
    if ((int)exists == 1)
    {
        Throw new ORMEntityValidationException("PK already exists.");
    }
}
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 13-Sep-2007 14:42:16   

Jim, Thats great. I was planning on using the FindMatches from the UI/BL and in the ValidatorClass use the GetScaler method.

The only hesitation I have is where to do it. From my ValidatorClass, it would be nice to access an existing GetScaler function within my BL instead of creating another within the ValidatorClass.

Given that BL calls DAL, it doesn't seem feasible, though I guess I could move my existing GetScaler function down into DatabaseGeneric and use it from there. After all is LLBL not Lower Level Business Layer etc.. Am I on the right tracks here?

Thanks for any comments

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 13-Sep-2007 14:48:57   

sounds like a plan to me

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 13-Sep-2007 16:19:27   

From my ValidatorClass, it would be nice to access an existing GetScaler function within my BL instead of creating another within the ValidatorClass.

Given that BL calls DAL, it doesn't seem feasible

Using v.2.5 you may define your ValidatorClasses anywhere (eg. in their own assembly), and then use Dependency Injection, to set the Validator property of the target entity.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 13-Sep-2007 19:30:59   

Using v.2.5 you may define your ValidatorClasses anywhere (eg. in their own assembly), and then use Dependency Injection, to set the Validator property of the target entity.

... but not sure I have time to get to grips with Ver 2.5. I'll have to see what the potential benefits are.