Generics in Listing and ListItems

Generics in Listing and ListItems

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


Tim FOrd posted on Tuesday, August 08, 2006

Afternoon All,

Just wondering if someone can point me in the right direction.

I have a list which needs to be generic but the listitems also need to be generic as well. I have attached a diagram of what I’m trying to achieve and wondered if this might be possible.

Text Version.

I have a need for a ContactList and a ContactListItem and ideally would like to use generic to achieve multiple classes

I will need

CustomerContactList to Return a Collection of CustomerContactListItem
LocationContactList to Return a Collection of LocationContactListItem
MeetingContactList to Return a Collection of MeetingContactListItem

The Customer, Location Meeting Contact List Item needs to inherit from ContactListItem and when i call the top level Class CustomerContacts it needs to call the BusinessObjectLoad under the ContactList and return a list dependant for the Customer, Location, Meeting ListItems.

Hope this makes sense and is possible?

Thanks, Tim.

RRorije replied on Tuesday, August 08, 2006

Hi

The first idea that pops into my head is that this closely resembles the abstract factory pattern (see Gamma et al, "Design Patterns"), but this pattern can of course also be found on the web.

I do not know how this must be implemented in this case, yet it might be worth looking into.


RockfordLhotka replied on Tuesday, August 08, 2006

Generics are not polymorphic. To create a strongly typed list of child generic types, those child generic types need to have some non-generic type in common. Typically this is a non-generic interface that all the generic child classes can implement, so the list can hold items of that non-generic interface type.

Tim FOrd replied on Wednesday, August 09, 2006

Hi Rocky,

I think i might have acehived what i was looking to do, please see the code below, i would be greateful if you could have a look and see if i'm barking up the wrong tree.

Thanks, Tim.

    Public Class ContactList(Of T As ContactListItem(Of T))
        Inherits ReadOnlyListBase(Of ContactList(Of T), ContactListItem(Of T))

        Public Shared Function GetCustomerContactList(ByVal ID As System.Guid) As ContactList(Of CustomerContactListItem)
            Return DataPortal.Fetch(Of ContactList(Of CustomerContactListItem))(New CustomerCriteria(ID))
        End Function

        Public Shared Function GetLocationContactList(ByVal ID As System.Guid) As ContactList(Of LocationContactlistItem)
            Return DataPortal.Fetch(Of ContactList(Of LocationContactlistItem))(New LocationCriteria(ID))
        End Function

        Public Shared Function GetMeetingContactList(ByVal ID As System.Guid) As ContactList(Of MeetingContactListItem)
            Return DataPortal.Fetch(Of ContactList(Of MeetingContactListItem))(New MeetingCriteria(ID))
        End Function

    End Class

    Public MustInherit Class ContactListItem(Of T As ContactListItem(Of T))
        Inherits ReadOnlyBase(Of ContactListItem(Of T))

    End Class

    Public Class CustomerContactListItem : Inherits ContactListItem(Of CustomerContactListItem)

    Public Class LocationContactListItem : Inherits ContactListItem(Of LocationContactListItem)

    Public Class MeetingContactListItem : Inherits ContactListItem(Of MeetingContactListItem)


RockfordLhotka replied on Wednesday, August 09, 2006

Oh, I see. You didn't want the list to contain both types at the same time, just one type or the other? Then that should work I would think. The only drawback to this is that your top-level list is a generic, which could cause you some grief in the UI, since your two lists won't be polymorphic at that level. If that doesn't cause you issues then I don't see a problem.
 
Rocky


From: Tim FOrd [mailto:cslanet@lhotka.net]
Sent: Wednesday, August 09, 2006 3:11 AM
To: rocky@lhotka.net
Subject: Re: [CSLA .NET] Generics in Listing and ListItems

Hi Rocky,

I think i might have acehived what i was looking to do, please see the code below, i would be greateful if you could have a look and see if i'm barking up the wrong tree.

Thanks, Tim.

Copyright (c) Marimer LLC