Exposing the IEntityField...

Posts   
 
    
jimw
User
Posts: 5
Joined: 13-Jul-2006
# Posted on: 18-Jan-2008 22:40:06   

Using LLBLGen Pro v 2.5 Final in a VS 2008 project.

I've been going around and around with this and I've felt like I was close to the solution a couple of times, but I can't quite break through. Can anyone give me a push in the right direction?

I have created a Manager Class for some business logic for each Entity Class. For example, my BranchEntity has a BranchEntityManager.


    Public Class BranchEntityManager
        Inherits ManagerBase

        Protected ReadOnly Property DefaultSort() As SortExpression
            Get
                Dim Sort As New SortExpression()
                    Sort.Add(New SortClause(BranchFields.BranchCode, Nothing, SortOperator.Descending))
                    Sort.Add(New SortClause(BranchFields.BranchName, Nothing, SortOperator.Ascending))
                Return Sort
            End Get
        End Property

        Protected ReadOnly Property MaxItemsToReturn() As Integer
            Get
                Return 0
            End Get
        End Property

        Protected ReadOnly Property CompanyPrefetch() As PrefetchPath2
            Get
                Dim PrefetchPath As New PrefetchPath2(CType(EntityType.BranchEntity, Integer))
                PrefetchPath.Add(BranchEntity.PrefetchPathCompany)
                Return PrefetchPath
            End Get
        End Property

        Public Function GetList() As EntityCollection(Of BranchEntity)
            Return GetList(Me.DefaultSort)
        End Function

        Public Function GetList(ByVal sort As SortExpression) As EntityCollection(Of BranchEntity)
            Dim BranchList As New EntityCollection(Of BranchEntity)
            Dim Bucket As New RelationPredicateBucket

            Using MyAdapter As DataAccessAdapter = MyBase.GetAdapter()
                MyAdapter.FetchEntityCollection(BranchList, Bucket, Me.MaxItemsToReturn, sort, Me.CompanyPrefetch)
            End Using

            Return BranchList
        End Function

    End Class

As you can see, I have overloaded by GetList method to use a default sort or to accept a sort expression provided by the consumer.

Next, I wanted to create a class within the BranchManager class which is used to limit the sort fields to only BranchEntityFields.


        Public Class SortOrder
            Private _Sort As SortExpression

            Public ReadOnly Property Sort() As SortExpression
                Get
                    If _Sort Is Nothing Then
                        _Sort = New SortExpression
                    End If
                    Return _Sort
                End Get
            End Property

            Public Sub AddSortItem(ByVal fieldIndex As BranchFieldIndex, ByVal direction As SortDirection)
                If _Sort Is Nothing Then
                    _Sort = New SortExpression
                End If
                Dim Field As EntityField2 = EntityFieldFactory.Create(CType(fieldIndex, BranchFieldIndex))
                Sort.Add(New SortClause(Field, direction))
            End Sub
        End Class

It will be used like so:


Dim BranchManager As New BranchEntityManager
Dim SortOrder As New BranchManager.SortOrder
SortOrder.AddSortItem(BranchFieldIndex.City, SortDirection.Ascending)
SortOrder.AddSortItem(BranchFieldIndex.StateCode, SortDirection.Ascending)
Dim BranchList As EntityCollection(Of BranchEntity) = BranchManager.GetList(SortOrder.Sort)

The problem I run into is that in the SortOrder class, when I do a Sort.Add in the AddSortItem method, I get an error because Sort.Add expects an IEntityField and not the IEntityField2 that is created by the EntityFieldFactory.Create method.

I'm sure I'm just overlooking something obvious disappointed or maybe I'm headed down the wrong pathflushed . A little direction or a big correction. Either would be appreciated.

...Jim...

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Jan-2008 03:31:07   

Hi Jim,

You have to use this overload:


SortClause Constructor(IEntityFieldCore,IFieldPersistenceInfo,SortOperator)

Public Function New( _
   ByVal fieldToSort As IEntityFieldCore, _
   ByVal persistenceInfo As IFieldPersistenceInfo, _
   ByVal sortOperatorToUse As SortOperator _
)

so, your code should loook like

...
Sort.Add(New SortClause(Field, Nothing, direction))
...
David Elizondo | LLBLGen Support Team
jimw
User
Posts: 5
Joined: 13-Jul-2006
# Posted on: 21-Jan-2008 14:16:33   

daelmo wrote:

Hi Jim,

You have to use this overload:


SortClause Constructor(IEntityFieldCore,IFieldPersistenceInfo,SortOperator)

Public Function New( _
   ByVal fieldToSort As IEntityFieldCore, _
   ByVal persistenceInfo As IFieldPersistenceInfo, _
   ByVal sortOperatorToUse As SortOperator _
)

so, your code should loook like

...
Sort.Add(New SortClause(Field, Nothing, direction))
...

Thanks for the response!

Sorry, I should have specified that I tried to use that overload but I get the following compiler error:


Overload resolution failed because no accessible 'New' can be called without a narrowing conversion:
    'Public Sub New(fieldToSort As SD.LLBLGen.Pro.ORMSupportClasses.IEntityFieldCore, persistenceInfo As SD.LLBLGen.Pro.ORMSupportClasses.IFieldPersistenceInfo, sortOperatorToUse As SD.LLBLGen.Pro.ORMSupportClasses.SortOperator)': Argument matching parameter 'sortOperatorToUse' narrows from 'System.Web.UI.WebControls.SortDirection' to 'SD.LLBLGen.Pro.ORMSupportClasses.SortOperator'.
    'Public Sub New(fieldToSort As SD.LLBLGen.Pro.ORMSupportClasses.IEntityField, sortOperatorToUse As SD.LLBLGen.Pro.ORMSupportClasses.SortOperator, objectAlias As String)': Argument matching parameter 'fieldToSort' narrows from 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField2' to 'SD.LLBLGen.Pro.ORMSupportClasses.IEntityField'.
    'Public Sub New(fieldToSort As SD.LLBLGen.Pro.ORMSupportClasses.IEntityField, sortOperatorToUse As SD.LLBLGen.Pro.ORMSupportClasses.SortOperator, objectAlias As String)': Argument matching parameter 'objectAlias' narrows from 'System.Web.UI.WebControls.SortDirection' to 'String'.    C:\Documents and Settings\wort1010\My Documents\Visual Studio 2008\WebSites\ISPManager\App_Code\BLL\AdminBranchEntityManager.vb 34  26  C:\...\ISPManager\

goose avatar
goose
User
Posts: 392
Joined: 06-Aug-2007
# Posted on: 21-Jan-2008 19:14:40   

it seems you are making a casting mistake somewhere in your code, please double check the castings. The overload that daelmo point out should work.

edit: try instantiate the field as IEntityField2 instead.

jimw
User
Posts: 5
Joined: 13-Jul-2006
# Posted on: 21-Jan-2008 20:16:13   

goose wrote:

it seems you are making a casting mistake somewhere in your code, please double check the castings. The overload that daelmo point out should work.

edit: try instantiate the field as IEntityField2 instead.

Ah Ha! I found the problem when I looked in the mirror! Apparently, there was a loose nut behind the keyboard.

Actually, you were both absolutely right, of course!!!

The direction parameter for the AddSortItem routine was cast as a SortDirection instead of a SortOperator.

Thanks for the help guys!

IMHO: This is without a doubt the best support forum of any I've been on. Response time is great! Answers are right on! Thanks to everyone who helps support a great product. (And I'm not even trying to get a discount on my next upgrade.) simple_smile