I find myself trying to write reusable code alot. So far, what I really like about the adapter pattern is that I can create an instance of an object, set its primary key value, and pass it to a facade method that uses abstract objects and fetch the data, such as the code below:
Public Function FetchData(ByVal Entity As EntityBase2) As EntityBase2
Dim myDB As DataAccessAdapter = db(True)
If myDB.FetchEntity(Entity) Then
Return Entity
End If
End Function
When creating an instance of an entityCollection, you create the instance using a factory and then fetch data into it. This code typically looks something like this:
Public Function Seasons(ByVal PortalID as integer) as HelperClasses.EntityCollection
Dim mSeasons As New HelperClasses.EntityCollection(New FactoryClasses.SeasonEntityFactory)
'... predicate code omitted
myDB.FetchEntityCollection(mSeasons, pred, 0, sort)
If mSeasons.Count > 0 Then
TraceMessage("Located " & mSeasons.Count.ToString & " seasons")
Return mSeasons
End If
End Function
My question is, this: does an entity object know about what factory it is related to?
In the Seasons sample above, I create a new entityCollection using a factory class. If an entity was aware of it's factory, an entity could be passed to this method, and the method could use the entity's factory to create the collection. The goal of this exercise would be to have one method, that would fetch collections, and not care about the factory being used, because it could determine the factory from the data passed in.
Any thoughts on how I could make my code more extensible?