View Poll Results: Key or entity first?

Voters
2. You may not vote on this poll
  • Entity type then key type

    0 0%
  • Key type then entity type

    2 100.00%
Results 1 to 8 of 8

Thread: Style question: key before entity or entity before key?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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:
    1. ''' <summary>
    2. ''' Interface to support reading entities from the backing store
    3. ''' </summary>
    4. ''' <typeparam name="TEntity">
    5. ''' The key-identified type of entity we are reading
    6. ''' </typeparam>
    7. ''' <typeparam name="TKey">
    8. ''' The type of the key
    9. ''' </typeparam>
    10. ''' <remarks>
    11. ''' In this architecture there is a seperate read and write interface but often this
    12. ''' pattern has just the one interface for both functions
    13. ''' </remarks>
    14. Public Interface IRepositoryRead(Of TEntity As IKeyedEntity(Of TKey), In TKey)

    or

    VB.net Code:
    1. ''' <summary>
    2. ''' Interface to support reading entities from the backing store
    3. ''' </summary>
    4. ''' <typeparam name="TEntity">
    5. ''' The key-identified type of entity we are reading
    6. ''' </typeparam>
    7. ''' <typeparam name="TKey">
    8. ''' The type of the key
    9. ''' </typeparam>
    10. ''' <remarks>
    11. ''' In this architecture there is a seperate read and write interface but often this
    12. ''' pattern has just the one interface for both functions
    13. ''' </remarks>
    14. Public Interface IRepositoryRead(Of In TKey, TEntity As IKeyedEntity(Of TKey))

    Thoughts?
    Last edited by Merrion; Feb 3rd, 2014 at 05:28 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Style question: key before entity or entity before key?

    Quote Originally Posted by Shaggy Hiker View Post
    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).
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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:
    1. Public Class Bookshelf
    2.    Implements IRepositoryRead(Of Book, ISBNNumber)
    3.  
    4. End Class
    Last edited by Merrion; Feb 4th, 2014 at 07:02 AM. Reason: added show-and-tell and fixed class decl.

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Style question: key before entity or entity before key?

    Quote Originally Posted by Merrion View Post
    In the absence of voter guidance I shall fall back on instinct - TEntity before TKey as per above...e.g.

    vb.net Code:
    1. Public Class Bookshelf(Of Book, ISBNNumber)
    2.    Implements IRepositoryRead(Of Book, ISBNNumber)
    3.  
    4. 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.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Style question: key before entity or entity before key?

    Yup - correct - this was air coded...

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    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:
    1. ''' <summary>
    2. ''' Interface defining any item we can store in a repository and can identify by
    3. ''' an unique key
    4. ''' </summary>
    5. ''' <remarks>
    6. ''' This interface is typed so we can make type-safe code for retrieving the entity
    7. ''' (don't pass in an integer if the entity is keyed by string etc.)
    8. ''' </remarks>
    9. Public Interface IKeyedEntity(Of TKeyType)
    10.  
    11.     ''' <summary>
    12.     ''' Get the key to find the entity by
    13.     ''' </summary>
    14.     Property Key As TKeyType
    15.  
    16. 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
  •  



Click Here to Expand Forum to Full Width