Validating that a generic list contains at least one item

Validating that a generic list contains at least one item

Old forum URL: forums.lhotka.net/forums/t/5043.aspx


adamnationx posted on Tuesday, July 01, 2008

Hey guys,

Just started working with CSLA recently on a DB management app. It's definitely making my life easier in many ways, but I ran into a problem this morning when I started implementing some custom validation rules. Hopefully somebody has some advice for me!

I want to implement a rule that ensures that a generic list object contains at least one item (list.Count > 0). I am implementing this rule as part of an "Order" object which has a member "OrderItems" collection, but I'd like the business rule to accept generic lists. I was playing around with passing the type to the rule, but I'm not super well-versed in generics and certainly not with CSLA business rules. Any help would be greatly appreciated!

Thanks,
Adam

sergeyb replied on Tuesday, July 01, 2008

You can try to use in ICollection.  This should work with both ReadOnlyBLB and BLB.

 

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

Magenic ®

Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: adamnationx [mailto:cslanet@lhotka.net]
Sent: Tuesday, July 01, 2008 2:41 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Validating that a generic list contains at least one item

 

Hey guys,

Just started working with CSLA recently on a DB management app. It's definitely making my life easier in many ways, but I ran into a problem this morning when I started implementing some custom validation rules. Hopefully somebody has some advice for me!

I want to implement a rule that ensures that a generic list object contains at least one item (list.Count > 0). I am implementing this rule as part of an "Order" object which has a member "OrderItems" collection, but I'd like the business rule to accept generic lists. I was playing around with passing the type to the rule, but I'm not super well-versed in generics and certainly not with CSLA business rules. Any help would be greatly appreciated!

Thanks,
Adam


adamnationx replied on Tuesday, July 01, 2008

Don't I need to get the type of the collection objects? I'm passing to the validation rule the name of the field that contains the list. When I receive it in the validation rule, I want to use CSLA's Utilities.CallByName() method with the property name to get the list, then cast it as an ICollection? I can't declare an ICollection variable without a type argument. Not sure if I'm just missing the mark here completely or what. Thanks for the quick response, though!

JoeFallon1 replied on Wednesday, July 02, 2008

Dim mLines As SomeColl = SomeColl .NewSomeColl ()
Dim coll As ICollection = TryCast(mLines, ICollection)
If coll.Count > 0 Then

End
If

Copyright (c) Marimer LLC