dimanche 21 juin 2015

Using a random id and designing API on them, an anti-pattern?

I had a discussion recently on how to model some data. The context is we have to model an entity that has a name and other information.

Entity:
    Long id
    String name
    String something
    int something-else

The initial approach was to have an Id (random digits) to uniquely identify an entity and the rest of the information in the entity are mutable. We would then design an API on the lines of

Entity getEntity( long id );

This would mean clients who are interested in using the API would have to either hard-code the id or have an ENUM to map id to a "textual" reference and use it.

The other opinion was to use the name itself as a unique identifier and use it instead. It would be more client friendly as it doesn't require them to hard code some random number, which by the literal value doesn't mean anything to the reader. I kind of agree with this point of view as a text based id is more meaningful than a bunch of digits. But by this it would mean the name it self would be immutable and any edits to the name would mean a delete & create.

I would like to know what others think about the argument and the real world experiences.

Aucun commentaire:

Enregistrer un commentaire