All of my classes use private setters, simplified example:
public class Person {
public string PersonId { get; private set:}
public string FirstName { get; private set;}
public string LastName { get; private set;}
public Person(string id, string firstName, string lastName) {
Id= id;
FirstName = firstName;
LastName = lastName;
}
}
Everything persists perfectly fine, but when I retrieve my document the constructor on my object is not being called to hydrate my object:
public async Task<Person> GetByIdAsync(string id)
{
using var result = await _collection.GetAsync(id);
var person = result.ContentAs<Person>();
return person;
}
I know I am getting my document back properly, I tested:
var doc = result.ContentAs<string>();
And it contained the proper document and the correct data but result.ContentAs<Person>();
just returns an object with empty strings for values.
I tried adding [JsonConstructor]
annotation on my class I saw in another post along with the following configuration for my cluster:
var serializerSettings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore
};
serializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
var options = new ClusterOptions
{
UserName = "username",
Password = "password",
};
options.ApplyProfile("wan-development");
options.WithSerializer(
new DefaultSerializer(serializerSettings, serializerSettings)
);
var cluster = await Cluster.ConnectAsync(
"couchbases://cb.mycluster.cloud.couchbase.com",
options
);
No luck, anyone solve this issue?
I am coming from MongoDB after they killed Atlas yesterday and I have a TON of things to migrate and I would love to avoid writing a ton of mappers or calling constructors manually everywhere if possible.