Is a generic entity exist function possible?

Posts   
 
    
Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 05-Apr-2007 21:29:08   

Hi,

Using VB, Adapter, LLBLGen 2. I find that I'm duplicating the following code, modified for different entities, to check if a record exists for one entity or another. Is it possible to produce a generic version of the code to enable me to pass in an entity name and a PK field name and so avoid the duplicated behaviour? If so, could someone help out?

    Function ProductExist(ByVal aRowID As Byte) As Boolean
        Dim product As New EntityCollection(Of ProductEntity)(New ProductEntityFactory())
        Dim filter As IRelationPredicateBucket = New RelationPredicateBucket()
        filter.PredicateExpression.Add(ProductFields.ProductID = aRowID)
        Dim adapter As New DataAccessAdapter()
        Try
            adapter.FetchEntityCollection(product, filter, 0)
            If product.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Finally
            adapter.Dispose()
        End Try
    End Function

Thanks simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Apr-2007 11:27:25   
Function EntityExists(ByVal myEntity As IEntity2) As Boolean

        Dim adapter As New DataAccessAdapter()
        Try
            Return adapter.FetchEntity(myEntity)
        Finally
            adapter.Dispose()
        End Try

End Function

Call the above function and pass in the entity you want to fetch with its PK set to a value.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 06-Apr-2007 16:03:59   

Walaa Thanks

I knew it was simple, but just couldn't see it, and the function + Reference manual solved more than the immediate problem smile

Thanks