public class Id<TEntity> where TEntity : class{ public string Value { get; }
public Id(string value) { Value = value; }}public class Person{
public Id<Person> Id { get; set; }
public string Name { get; set; } public int Age { get; set; }
public bool IsInLegalAge => Age > 18; public Id<Person> FatherId { get; set; } public Id<Person> MotherId { get; set; }}ID: people/737-A { "Name": "Ivan", "Age": 23, "FatherId": "people/705-A", "MotherId": "people/673-A", "Id": "people/737-A", "@metadata": { "@collection": "People", "Raven-Clr-Type": "Tryouts.Raven.Entities.Person, Tryout.Raven" }}public class IdConverter : JsonConverter{ public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var idString = (string) value.GetType().GetProperty("Value").GetValue(value); writer.WriteValue(idString); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var idString = (string) reader.Value;
var ctor = objectType.GetConstructor(new[] { typeof(string) }); return ctor?.Invoke(new object[] {idString}); }
public override bool CanConvert(Type objectType) { return objectType.IsGenericType && objectType.GetGenericTypeDefinition() == typeof(Id<>); }}var defaultDocumentIdGenerator = documentStore.Conventions.AsyncDocumentIdGenerator;documentStore.Conventions.AsyncDocumentIdGenerator = async (dbName, entity) =>{ var entityIdentifierProperty = entity.GetType().GetProperty("Id"); var entityIdentifierPropertyType = entityIdentifierProperty?.PropertyType;
if (entityIdentifierProperty is null || entityIdentifierPropertyType != typeof(Id<>).MakeGenericType(entity.GetType())) return await defaultDocumentIdGenerator(dbName, entity);
// Notice here that we are creating a new HiLo generator per entity, so we are receiving a new rank per entity.
// The ideal is get the HiLo generator from the document store, but is private ;( var hiLoGenerator = new AsyncMultiDatabaseHiLoIdGenerator(documentStore, documentStore.Conventions); var generatedId = await hiLoGenerator.GenerateDocumentIdAsync(dbName, entity); entityIdentifierProperty.SetValue(entity, entityIdentifierPropertyType.GetConstructor(new []{typeof(string)})?.Invoke(new object[]{generatedId}));
return generatedId;};
var defaultIdentityProperty = documentStore.Conventions.FindIdentityProperty;documentStore.Conventions.FindIdentityProperty = info =>{ var entityType = info.DeclaringType; var entityIdentifierProperty = entityType?.GetProperty("Id"); var entityIdentifierPropertyType = entityIdentifierProperty?.PropertyType; if (entityIdentifierPropertyType is null || entityIdentifierPropertyType == typeof(Id<>).MakeGenericType(entityType)) return defaultIdentityProperty(info); return false;};var sophia = new Person{ Age = 50, Name = "SOPHIA",};
var john = new Person{ Age = 53, Name = "JOHN"};
// We store here the parents because we need that// the sophia and john instances have Id assigned.await session.StoreAsync(sophia);await session.StoreAsync(john);
var ivan = new Person{ Age = 23, Name = "IVAN", FatherId = john.Id, MotherId = sophia.Id};
await session.StoreAsync(ivan);await session.SaveChangesAsync();We can query to try it:
var ivan = await session.Query<Person>()
.Where(x => x.Id == new Id<Person>("people/193-A"), true) .Include(x => x.FatherId) .Include(x => x.MotherId) .FirstAsync();
var sophia = await session.LoadAsync<Person>(ivan.MotherId.Value);var john = await session.LoadAsync<Person>(ivan.FatherId.Value);
// Output: IVAN, Mother: SOPHIA, Father: JOHN
Console.WriteLine("{0}, Mother: {1}, Father: {2}", ivan.Name, sophia.Name, john.Name);Best to the RavenDB community ;)
--
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/6e70204d-8c09-4272-82e8-9b191e9bdd22%40googlegroups.com.
![]() | Oren Eini CEO / Hibernating Rhinos LTD
|
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/6e70204d-8c09-4272-82e8-9b191e9bdd22%40googlegroups.com.
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/9269c68b-0b5f-48bd-a08f-99437074256c%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/9269c68b-0b5f-48bd-a08f-99437074256c%40googlegroups.com.
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/f79b740e-d4e9-4f01-9745-75eef5962889%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/f79b740e-d4e9-4f01-9745-75eef5962889%40googlegroups.com.