Style question: key before entity or entity before key?
I am putting together a type-safe version of the Repository Pattern by passing both the entity type and its corresponding key type. The style question is should the key or the entity go first in the definition?
e.g.
VB.net Code:
''' <summary>
''' Interface to support reading entities from the backing store
''' </summary>
''' <typeparam name="TEntity">
''' The key-identified type of entity we are reading
''' </typeparam>
''' <typeparam name="TKey">
''' The type of the key
''' </typeparam>
''' <remarks>
''' In this architecture there is a seperate read and write interface but often this
''' pattern has just the one interface for both functions
''' </remarks>
Public Interface IRepositoryRead(Of TEntity As IKeyedEntity(Of TKey), In TKey)
or
VB.net Code:
''' <summary>
''' Interface to support reading entities from the backing store
''' </summary>
''' <typeparam name="TEntity">
''' The key-identified type of entity we are reading
''' </typeparam>
''' <typeparam name="TKey">
''' The type of the key
''' </typeparam>
''' <remarks>
''' In this architecture there is a seperate read and write interface but often this
''' pattern has just the one interface for both functions
''' </remarks>
Public Interface IRepositoryRead(Of In TKey, TEntity As IKeyedEntity(Of TKey))
Thoughts?
Re: Style question: key before entity or entity before key?
Now THAT'S an esoteric question. I can see a reason to prefer it both ways, but I think that the second is slightly more clear despite being slightly less aesthetically appealing. It kind of comes down to how you feel like asking the question when looking for something in a repository. I tend to say Get All X By Y, in which case the key would appear to make more sense second, but in a function...heck, I just talked myself out of it.
I think that there is no best answer. No wonder you asked the question. I abstain from voting.
Re: Style question: key before entity or entity before key?
Quote:
Originally Posted by
Shaggy Hiker
Now THAT'S an esoteric question. I can see a reason to prefer it both ways, but I think that the second is slightly more clear despite being slightly less aesthetically appealing. It kind of comes down to how you feel like asking the question when looking for something in a repository. I tend to say Get All X By Y, in which case the key would appear to make more sense second, but in a function...heck, I just talked myself out of it.
I think that there is no best answer. No wonder you asked the question. I abstain from voting.
I agree. It feels like TEntity should come first because TKey has no meaning without TEntity but the first code snippet seems to read better, maybe because we're already used the Dictionary(Of TKey, TValue).
Re: Style question: key before entity or entity before key?
In the absence of voter guidance I shall fall back on instinct - TEntity before TKey as per above...e.g.
vb.net Code:
Public Class Bookshelf
Implements IRepositoryRead(Of Book, ISBNNumber)
End Class
Re: Style question: key before entity or entity before key?
Key first. Definitely. You're going to be giving it a TKey and getting back a TEntity, so put the type parameters in that order.
Re: Style question: key before entity or entity before key?
Quote:
Originally Posted by
Merrion
In the absence of voter guidance I shall fall back on instinct - TEntity before TKey as per above...e.g.
vb.net Code:
Public Class Bookshelf(Of Book, ISBNNumber)
Implements IRepositoryRead(Of Book, ISBNNumber)
End Class
Um, I don't think you really want the type parameters on the Bookshelf class - that will turn Book and ISBNNumber into type parameters, not references to the types.
Re: Style question: key before entity or entity before key?
Yup - correct - this was air coded...
Re: Style question: key before entity or entity before key?
I imagine there's no way to get the TEntity to provide the TKey class?
e.g.
vb.net Code:
''' <summary>
''' Interface defining any item we can store in a repository and can identify by
''' an unique key
''' </summary>
''' <remarks>
''' This interface is typed so we can make type-safe code for retrieving the entity
''' (don't pass in an integer if the entity is keyed by string etc.)
''' </remarks>
Public Interface IKeyedEntity(Of TKeyType)
''' <summary>
''' Get the key to find the entity by
''' </summary>
Property Key As TKeyType
End Interface
If my TEntity knows what type provides its key I still have to provide that again for IRepositoryRead... no harm as it is still type safe, but just in case I'm missing something other than sleep?