Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

obj.id seems fairly common and also seems to fall within the range of something an object could know about itself. I find myself asking why should my object know its own id?

It doesn't seem to have a reason to have it? One of the main reason for it existing is retrieve it, and so my repositories need to know it, and thus use it for database interaction.

I also once encountered a problem where I wanted to serialize an object to JSON for a RESTful API where the id did not seem to fit in the payload, but only the URI and including it in the object made that more difficult.

Should an object know it's own id? why or why not?

update: Terms

  1. id: Identifier attribute on an object. in database terms, a surrogate key not a natural key.
  2. Object: an Entity, or otherwise uniquely identifiable object within the system.
share|improve this question
1  
To remain abstract, if there is no reason to store information, don't store it. It just creates headaches. –  delnan Sep 29 '12 at 20:52
3  
If you don't know the unique key of an object how would you update the data in a database or let the user select an occurrence from a list? –  Emmad Kareem Sep 29 '12 at 21:40
    
@EmmadKareem what I'm saying is that the collection (repository) knows the id, so why does the object itself need to. If I was rendering a list I'd probably be rendering the collection, which knows the id's. –  xenoterracide Sep 29 '12 at 21:43
    
If you dont use the id thats stored with the object, and never intend or want to use it, its clearly not needed. –  GrandmasterB Sep 29 '12 at 22:48
    
@GrandmasterB well of course you use the id, the question is whether you store the id in memory with the object, and why. –  xenoterracide Sep 30 '12 at 0:03
show 3 more comments

6 Answers

Short Answer: Including object identifier within the object is a must if it is going to be referred.

For a scenario where you are planning to use a collection of objects and have plans to refer an individual object/entity then identifier should be included. However, there might be cases where natural key/identifier like DateTime can artificially fulfill that need.

In addition, you may have your DTO's as a lightweight container of your object, that may skip/include object identifier in itself. Really all this choices are really depend on your business use cases and needs.

Edit: if you want to identify your object you need to have an identifier. That identifier can be a surrogate Id, date-time, guid, etc... basically, anything that identifies your object from others in a unique way.

share|improve this answer
2  
why should they be included in the object, rather than just the collection (assuming the collection is some kind of dictionary)? –  xenoterracide Sep 29 '12 at 21:40
    
my point was that, if you want to identify your object you need to have an identifier. it can be Id, date-time, etc... anything that identifies your object from others. –  Yusubov Sep 29 '12 at 21:52
1  
well of course, my question is more of why the object itself needs to be aware of that identifier. –  xenoterracide Sep 29 '12 at 22:09
    
to clarify a bit I mean a surrogate id, usually things like datetime's, or vin numbers, are natural and thus part of the data, and not named id (or at least I wouldn't) –  xenoterracide Sep 29 '12 at 22:19
add comment

In an attempt to leave my answer as abstract as your question, I think you've actually answered your own question.

An object should know its own ID if it needs to.

An object shouldn't know its ID (or that it even has one) if it doesn't need to.

As you pointed out, there are cases where it is either inconvenient or unnecessary for the object to retain or know if its ID. But there are other cases where it is more convenient for the object to be aware of its ID. Code your objects as appropriate to their usage.

share|improve this answer
    
what cases is it convenient for it to be aware of its id? I've been pondering it and thinking that most of them could just be based on that's the way it was coded, perhaps a for loop used to render obj.id as opposed to rendering the collection itself. –  xenoterracide Sep 29 '12 at 21:46
1  
@xenoterracide - asynchronous viewing and updating of DB records is one example. In some cases, I won't have sufficient information to uniquely identify a record except for its ID so it makes sense to keep the ID with the record object. –  GlenH7 Sep 29 '12 at 21:54
    
but is that a cause or an effect? is it a requirement? if your repository/collection/datamapper (or some chain thereof ) is responsible for persisting the updates, and it knows the id and is able to pass it on... I'm trying to understand if there's a reason for it following or if it's there because many people use and active record pattern. –  xenoterracide Sep 29 '12 at 21:58
    
@xenoterracide - in the cases I'm thinking of, it definitely was a causal need. For some cases I've been in, it was a lot more convenient to keep the ID associated with the object. Your question was good because it challenged a baseline assumption a lot of folk make. We tend to learn when we dig into assumptions like that. On the other hand, don't over analyze and go in the opposite direction. Otherwise you'll end up making the same time of sacrosanct declarations that you were breaking down in the first place. –  GlenH7 Sep 30 '12 at 0:25
add comment

It's fairly common to include the ID, but it's also common to use dictionaries, i.e. the data structures where each object is associated with its ID in a way that you can easily retrieve this object knowing the corresponding ID, but the object doesn't know it.

For example, if you're creating an API with two methods:

Then you don't need to resend the ID 452 in the JSON response of the second request. The user of the API already knows the ID.

share|improve this answer
add comment

I think that this is subjective and depends on your design.

Mostly though this appears to be a design that comes from an active record. In an active record your entity has methods to do database operations and therefore must also know it's database identifier.

When using other patterns such as a repository with a data mapper storing this data in the object becomes unnecessary and perhaps inappropriate.

Take for example a Person object. I am given a name which may or may not be unique within a family. As the population has grown larger names are no longer unique and so we have come up with surrogate identifiers for increasingly large system. Examples of these include: my drivers license, and social security number. I am not born assigned an id, all of these id's must be applied for.

Most of these do not make good primary keys/ids for software, as they are not universal. At least not outside of their specific system, obviously an SSN is unique, and consistent, to the Social Security Administration. Since we are not generally the providers of this information you would not call them an id but rather the data they represent, e.g. SSN. Sometimes even contain the full composed object such as a DriversLicense which could contain all of the information the driver's license does.

All general id's are thus surrogate keys in the system, and could be replaced with in memory references, only containing id's to make looking up and persisting records easier.

Since an id is not a piece of conceptual data, I doubt that it (generally) belongs within the object, as it does not come from the domain. Rather it should be retained to its purpose which is to identify an object which has no other way to represent a unique identity. This could be done in a repository/collection with easy.

In software if you need to represent the object as a list, or persist it, you can simply do so from the repository/collection object, or some other object associated with that. When passing on to the Data Mapper (if it's separate), you can simply pass .update( id, obj ).

Disclaimer: I have not yet tried to build a system that does not contain the id within the entity and thus may prove myself wrong.

share|improve this answer
add comment

As GlenH7 noted in a comment, it's a good question, because it challenges what many people take for granted. My take on it however is that even if leaving the id out might seem conceptually pure, the pragmatic thing to do would be to keep the id with the object across as many layers of the system as possible. It costs little, but can come in handy when things start to break down.

An entity might not need to know it's id, just like a person doesn't need to know his personal identification number, driver license number nor any other identifier that might be in use in your location. You could certainly do without it most of the time. It's wise however to carry some kind of identification on you, just in case.

The same can be said of an object. Regardless of the data access layer's intricacies, the object ultimately maps to a certain database entry. This entry might only be uniquely identifiable by object's id. If so, then I would prefer to have this information available to me at any time, regardless whether I'm debugging some UI code, number crunching in the business layer, the repository itself or a dependent system across a webservice. Or whether I'm browsing error logs for that matter.

In your case, if you're only fetching data from the db and pushing it through the webservice, leaving the id out might be be the right thing to do. I just don't believe that it makes more sense than keeping it in the general case.

share|improve this answer
add comment

You are right, you don't need to store the id in the object, as long as you only need to know it in your repository context.

The situation changes when you pass the object to code outside that context, code that did not request the object by id (and therefore doesn't know it yet), but needs the id for whatever purpose (eg. to place it on a list of ids that will be requested from the repository later, by yet another code).

In that situation, you have two options:

  • introduce a new function argument 'id' in the whole chain of functions between the repository context and the function where the id is needed

  • include the id in the object

In most of these cases, storing the id inside the object will be the better solution. It will also reduce code changes when the id is needed in unforeseen places, plus it might be helpful with debugging at times.

That's why I do it when I do it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.