View Poll Results: Key or entity first?
- Voters
- 2. You may not vote on this poll
-
Feb 3rd, 2014, 05:20 PM
#1
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?
Last edited by Merrion; Feb 3rd, 2014 at 05:28 PM.
-
Feb 3rd, 2014, 05:50 PM
#2
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.
My usual boring signature: Nothing
 
-
Feb 3rd, 2014, 06:05 PM
#3
Re: Style question: key before entity or entity before key?
 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).
-
Feb 4th, 2014, 04:32 AM
#4
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
Last edited by Merrion; Feb 4th, 2014 at 07:02 AM.
Reason: added show-and-tell and fixed class decl.
-
Feb 4th, 2014, 05:00 AM
#5
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.
-
Feb 4th, 2014, 05:01 AM
#6
Re: Style question: key before entity or entity before key?
 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.
-
Feb 4th, 2014, 07:02 AM
#7
Re: Style question: key before entity or entity before key?
Yup - correct - this was air coded...
-
Feb 4th, 2014, 05:03 PM
#8
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|