Serializing and deserializing embedded classes (Firestore)
See original GitHub issueI have recently started using Firestore and your C# library and everything is working great, apart from one thing that is quite important to me, and hopefully other people too.
As you know it’s very easy to serialize and deserialize root properties of custom models by annotating them with the [FirestoreProperty]
attribute, however, there doesn’t seem to be a “native” way of doing the same for nested objects. Documentation says that we can embed Dictionary
instances in Firestore documents, however, that means we lose the type awareness if we’re resorted to using Dictionary<string, object>
.
I am current migrating a project from MongoDB to Firestore and I have quite a few models that are embedded 2-3 levels deep. ConvertTo<T>()
method unfortunately doesn’t serialize models containing other embedded models so I ended up writing custom extension methods which convert such models to dictionaries and back. That, however, means I don’t get to use the automatic (de)serialization that the library offers.
From what I can see, the library is using ValueDeserializer
by default and there is no way to override or modify it. Are there any plans to add support for nested models? My thinking is when you extract the property values you could check if the value is decorated with the [FirestoreData]
attribute and automatically extract it just like the root object. Maybe the same could be done when hydrating a model instance with snapshot data.
Here’s my ideal scenario:
[FirestoreData]
public class Parent {
[FirestoreProperty]
public string Name { get; set; }
[FirestoreProperty]
public Child Child { get; set; }
}
[FirestoreData]
public class Child {
[FirestoreProperty]
public int One { get; set; }
[FirestoreProperty]
public int Two { get; set; }
}
var child = new Child
{
One = 1,
Two = 2
};
var parent = new Parent
{
Name = "John",
Child = child
};
DocumentReference document = await collection.AddAsync(parent);
That would result in this document being stored in Firestore:
{
"Name": "John",
"Child": {
"One": 1,
"Two": 2
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:5
You are indeed correct. I’m not even sure how to explain my stupidity as I was convinced I tried doing it that way. Apparently I haven’t…
I updated the few models that I have at the moment and everything seems to work as expected. One thing I noticed though is that it’s not possible to pass an instance of a model to the
Update
method of theTransaction
class (not sure about other methods).I’m currently implementing a custom
IUserStore
version of ASP.NET Identity and the update method is expected to handle concurrency by fetching and persisting the user document in a single transaction. Here is a good example of what I mean.As you can see it’s not possible to do
transaction.Update(userRef, user);
as none of the overloads accepts an object. It’s obviously not an issue as it can be “fixed” by having a custom dictionary extractor but it would be nice to be able to just pass the model to the transaction methods without having to convert them manually.I’ve ended up with the following extension method, which doesn’t extract nested models, but as I only need it for the User model it’s not a problem at the moment as the class doesn’t have any nested classes:
Okay, I’ve tried it, and it worked as I expected to. Here’s my test code, using Json.NET just to show the structure of the object in a simple way. I’ve used your
Parent
andChild
classes without any modifications.Output:
That’s working as you intended, right? Could you clarify what behavior you want that isn’t covered?