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
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 theId
isprivate
on ALL domain objects.The code is more readable as
Dictionary<Hobbit,List<Adventures>>
is more readable to me thanDictionary<int,List<Adventures>>
with comments suggesting int is thehobbitId
especially when passing as parameters
My questions are :
What are advantages of using the first option over the second (which I might be blindly missing) ?
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
- Will there be any issues with performance or memory if I use Objects as keys instead of primitive types and WHY / HOW ?