1

I am currently learning DDD and having a hard time wrapping my head about how to implements URL friendly ids for a web application.

From my knowledge, in DDD it is common practice to use UUID as identifier for a domain entity that doesn't have any natural unique identifier. Since the domain doesn't have any knowledge about the database, this is gonna be the only unique identifier.

Usually, what I would do is query the database, retrieve my items and create a URL for my item like '/item/{id}' where {id} is the database id, so something like '/item/12345'.

What's annoying me currently with DDD is the fact that when I query my domain entity's repository it will give me the domain entity which has a uuid as unique identifier. Because of this, the url I will generate for the item is gonna be something like '/item/4020fd9e-8655-4c2b-93d0-c283210753d9', which looks ridiculous and is way too long.

Am I overthinking this?

What could be a way to deal with this ?

  • Yes, you are overthinking this. Just use UUIDs and go with that. – Andy Mar 24 at 17:20
  • 1
    Yes, there're other ways but they brings considerable complexity to the system. How important is for you to have friendly URLs? Are you looking for a way for the users to remember these URLs? Or it's just a mere aesthetic issue? – Laiv Mar 24 at 17:35
  • I might just use uuid in my url for now, but I would still be interested on how to implement this if you have any reading to recommand. Thanks ! – user3358768 Mar 24 at 17:53
  • An advantage of "way too long" UUIDs is that they are hard for an attacker to guess. If, instead, they see IDs like "JohnDoe" they might try "JenniferLawrence". – user949300 Apr 25 at 14:10
1

If you consider that a UUID is essentially just a 128 bits, usually displayed in hex, you can actually convert it quite easily from hex (base16) to a url friendly variant of base64 (e.g. this), making it about 1/4 of the size, but containing the same value. There's some good examples on here for converting regular base64 to a URL friendly variant, just be sure to decode the UUID hex first (or don't! it'll just end up being base64 encoded ascii instead).

0

Because of this, the url I will generate for the item is gonna be something like '/item/4020fd9e-8655-4c2b-93d0-c283210753d9', which looks ridiculous and is way too long.

  1. Looks ridiculous

    Assuming you are writing an api here, No-one will ever see the urls

  2. Way too long

    The max length of a url differs between browsers, but we are talking 2000 characters. Your url is not too long.

The major benefit with a UUID/GUID is that any computer can generate one without having to check to see if its already been used.

In practice this means that a client of your API can create a new Item with an ID, manipulate it, join it to other things etc etc and when its ready, POST it to your API to be saved without having to worry that the ID has already been used.

  • I see your point. Let's push the question further then. Let's say I want user friendly URL in a context of a web page. From my understanding, the domain entity shouldn't know anything about url formatting. So how should I go about generating a short identifier(such as a slug) for my domain entity so I can have a url like '/item/my-item-name' instead of having a uuid in the url? – user3358768 Mar 24 at 17:25
  • well it depends what your requirements are. I normally make the ID field a string, so I can put plain English names in for stuff which is static. but if you want seo urls you will need a mapping table to map multiple 'friendly' urls to your object – Ewan Mar 24 at 17:39
  • That's the sort of complexity I was refering to in my comment. On one side you need the capacity to generate unique/universal slugs, on the other side you have to map them somewhere because URLs are not (very likely) a domain concern. It will cause then 2 trips to different stores per requests: 1 to the indexer where friendly URIs are mapped against UUIDs and, 1 more to de data store. – Laiv Mar 25 at 6:39
0

DDD is about your business. Is a friendly URL a part of your business domain?

If the answer is "yes", then add an attribute to your entity to represent the friendly URL. Sometimes, the rules to generate a friendly URLs (or a slug) can be complicated. Honestly, I don't see the benefit of friendly URLs besides the SEO purpose. The requirement of friendly URLs is mostly from technical person, not from a domain expert.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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