Client side Guid (string)

29 views
Skip to first unread message

Luke Eccleston

unread,
Sep 3, 2020, 3:18:05 PM9/3/20
to RavenDB - 2nd generation document database
Hi


but is it ok to generate the Guids client side and then tostring them? like so 

public abstract class Entity
{
     public string Id { get; set; }

     protected Entity()
     {
        Id = Guid.NewGuid().ToString();
     }
}


Cheers
Luke

Ivan Montilla

unread,
Sep 4, 2020, 4:16:24 AM9/4/20
to RavenDB - 2nd generation document database
I think this is a anti-pattern. I have in all my entities Id property with private setter, and I never set it directly from constructor or from entity, I let the session to assign the ID.

I check if an instance of entity is stored in DB checking if Id property is null. If you want to store with GUID, you can create an extension method to session called StoreWithGuid that create a new Guid and set it to the entity.

Luke Eccleston

unread,
Sep 4, 2020, 7:42:13 AM9/4/20
to RavenDB - 2nd generation document database
Hi

That was just an example. I want the Id to be generated before session store and to be a Guid.ToString().

Nik

unread,
Sep 4, 2020, 11:03:31 AM9/4/20
to RavenDB - 2nd generation document database
It's ok to have it in the constructor. During the conversion from document to an entity the value will be overwritten (you need a private setter at least).

Whether you'll set a random GUID in the constructor, pass it as a constructor parameter or let the session handle it, that depends on your app architecture requirements.

Oren Eini (Ayende Rahien)

unread,
Sep 6, 2020, 5:39:58 AM9/6/20
to ravendb
There is no issue here, aside from my dislike of guids in general.
Note that you can get a human readable id without going to the server. 
See also:

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/842c834c-93f5-43e5-9e25-8c3ca0d2d54fn%40googlegroups.com.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net
  
Reply all
Reply to author
Forward
0 new messages