Reputation: 169
I'm looking for an example of how to use a custom class as an identifier with RavenDB .NET client. I have something like this:
class Product
{
public ProductId Id { get; }
public Product()
{
Id = new ProductId(Guid.NewGuid());
}
// ...
}
where ProductId looks like this:
class ProductId
{
public Guid Value { get; }
public ProductId(Guid value)
{
// validation
Value = value
}
}
I haven't found any information on that topic in the documentation. Does anyone know to do that?
Upvotes: 2
Views: 129
Reputation: 3839
A document identifier (document ID) is always a string that is associated with the document.
See: https://ravendb.net/docs/article-page/5.0/csharp/server/kb/document-identifier-generation
You can have the server generate the Guid for you:.
See: https://ravendb.net/docs/article-page/5.0/csharp/server/kb/document-identifier-generation#guid
To do that you assign: Id = string.Empty
See: https://ravendb.net/docs/article-page/5.0/csharp/client-api/document-identifiers/working-with-document-identifiers#autogenerated-ids
Else, maybe:
Refactor the current Id property to:
public ProductId MyId { get; }
and init with
MyId = new ProductId(Guid.NewGuid());
Add another property on the Product
class
public string Id { get; set; }
Use OnBeforeStore to assign:
Id = MyId.Value
Upvotes: 2