Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8997461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:53:33+00:00 2026-06-15T23:53:33+00:00

Is it a good practice to use your domain objects as keys in a

  • 0

Is it a good practice to use your domain objects as keys in a Dictionary ?

I have a scenario where I populate my domain object using NHibernate.

For performing business logic I need to look-up Dictionary. I can make use of
Either

IDictionary<int, ValueFortheObject> 

or

Dictionary<DomainObject, ValueFortheObject>

The second options seems better to me as

  1. I can write easier test cases and can use real domain objects in test cases as well rather than using Mock<DomainObject> (if I go with the first option) since setter on the Id is private on ALL domain objects.

  2. The code is more readable as Dictionary<Hobbit,List<Adventures>> is more readable to me than Dictionary<int,List<Adventures>> with comments suggesting int is the hobbitId especially when passing as parameters


My questions are :

  1. What are advantages of using the first option over the second (which I might be blindly missing) ?

  2. Will be any performance issues using the second approach?

Update 01:

My domain models implement those and they DO NOT get mutated while performing the operations.

Will there be issues with performance using them as keys ? or am I completely missing the point here and performance / memory are not related to what keys are being used?

Update 02:

My question is

  1. Will there be any issues with performance or memory if I use Objects as keys instead of primitive types and WHY / HOW ?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:53:34+00:00Added an answer on June 15, 2026 at 11:53 pm

    @ScottChamberlain gives you the overall issue, but your use cases could argue either way. Some questions you should ask yourself are: what does it mean for two business objects to be equal? Is this the same or different if they are being used as a key in a dictionary or if I’m comparing them elsewhere? If I change an object, should it’s value as a key change or remain the same? If you use overrides for GetHashCode() and Equals(), what is the cost of computing those functions?

    Generally I am in favor of using simple types for keys as there is a lot of room for misunderstanding with respect to object equality. You could always create a custom dictionary (wrapper around Dictionary<Key,Value>) with appropriate method if readability is your highest concern. You could then write the methods in terms of the objects, then use whatever (appropriate) property you want as the key internally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that it is good practice to use a using block when getting
is it acceptable/good-practice to use the method chaining pattern on value objects (like, returning
Is it a good practice to use Fragment.setRetainInstance() for all of your Fragments in
In domain driven design, it appears to be a good practice to use Factories
Is it good practice to use custom error levels on trigger_error() ? For example,
Is it a good practice to use Assert for function parameters to enforce their
When writing a Perl module, is it a good practice to use croak/die inside
I've read that good programming practice claims that one should use a function if
Is it a good practice to have multiple XXX : DbContext classes for each
I know it is good practice to keep your website entirely 'http://www' or http://,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.