-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom serializer and multiple models #106
Comments
Hi, So for now there's no option to change the serializer, only Newtonsoft.Json, even if I plan to make it more generic, but it's a ton of work. About the DB with multiple models, you can do it like that but It will try to deserialize everything anyway. What you could do is to have a boolean and call the Where method to filter things out. However, I am open to suggestions for proper implementation. |
Hey me again I was looking for this feature too I didn't notice it doesn't have it until I saw this but I came up with a simple workaround 😄. // This is dotnet 5.0 Top level statements
var context = new Context();
var empDb = context.Client.GetDatabase<Emperor>();
var rebDb = context.Client.GetDatabase<Rebel>();
for (int i = 0; i < 5; i++)
{
await empDb.AddAsync(new Emperor
{
Id = Path.GetRandomFileName(),
Underlings = i,
Alive = i % 2 == 0
});
await rebDb.AddAsync(new Rebel
{
Id = Path.GetRandomFileName(),
Name = "Anakin",
Surname = "Skywalker",
BattleCount = i.ToString(),
Enemy = "Obi Wan Kenobi",
Masters = new() { "Obi", "Sauron" }
});
}
var rebels = await context
.Query<Rebel>()
.ToListAsync();
var emperors = await context
.Query<Emperor>()
.ToListAsync();
var rebel = rebDb.FindAsync(rebels[0].Id);
var emperor = rebDb.FindAsync(rebels[0].Id);
WriteLine(rebel is not null);
WriteLine(emperor is not null);
WriteLine();
var jsonOptions = new JsonSerializerSettings { Formatting = Formatting.Indented };
WriteLine($"Rebels count: {rebels.Count}"); // Should be 5.
WriteLine($"Rebels json: \n{JsonConvert.SerializeObject(rebels, jsonOptions)}");
WriteLine();
WriteLine($"Emperors count: {emperors.Count}"); // Should be 5.
WriteLine($"Emperors json: \n{JsonConvert.SerializeObject(emperors, jsonOptions)}");
public class Context : CouchContext
{
public IQueryable<T> Query<T>() where T : CouchDocument<T>, new()
{
string discriminator = new T().Discriminator;
return Client.GetDatabase<T>().Where(d => d.Discriminator == discriminator);
}
protected override void OnConfiguring(CouchOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseEndpoint("http://localhost:5984/")
.EnsureDatabaseExists()
.UseBasicAuthentication(username: "admin", password: "password");
}
}
[JsonObject("multi_database")] // This is the common database name. It could be done on GetDatabase<T>("db_name")
public class CouchDocument<T> : CouchDocument
{
[DataMember]
[JsonProperty("discriminator")]
public virtual string Discriminator => typeof(T).Name;
}
public class Emperor : CouchDocument<Emperor>
{
public int Underlings { get; set; }
public bool Alive { get; set; } = true;
}
public class Rebel : CouchDocument<Rebel>
{
public string Name { get; set; }
public string Surname { get; set; }
public string Enemy { get; set; }
public string BattleCount { get; set; }
public List<string> Masters { get; set; } = new();
} |
@panoukos41 thanks for the input! As your solution is not generic, I opted to add the filter directly in the query builder. Next step is to update the context |
Yea it was basically a hack, later I discovered I could skip the context altogether 😀 From my understanding since Since an |
@panoukos41 doing a non-generic version of What I did instead is:
This way everything is automatic and safe. Everything is in the dev branch if you want to test and give me your feedback |
I see, yea I didn't really know how LINQ works :P but I am happy this feature now works out of the box 😄 |
V3.0.0 released |
Thank you!! |
Hi!
I use couch db database with multiple models from F#, I was wondering:
I assume I can use diff "databases" that are the same but with different TSource.:
The text was updated successfully, but these errors were encountered: