question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange bheaviuor with derived class

See original GitHub issue

As stated in the title, i see a strange behaviour when try to save a derived type. Not clear if it’s a bug or a “feature” inherited from MongoDB driver, but it’s at least misleading in some situation, and accordingly with what is written in documentation.

Here the example :

 Person model = Person.BuildModel_Simple();

            Random rng = new Random();
            model.Name += rng.Next(10000, 99999).ToString();

            var task = MongoDB.Entities.DB.SaveAsync<Entity>(model);
            task.Wait();

            var result = task.Result;

            Assert.IsTrue(result?.UpsertedId != null);

And here the definition of Person

[Name("Person")]
public class Person : Entity, ICreatedOn, IModifiedOn
{
    public string Name { get; set; }
    public string Surname { get; set; }
    public DateTime DateOfBirth { get; set; }
    public DateTime CreatedOn { get; set; }
    public DateTime ModifiedOn { get; set; }
        
    public override string GenerateNewID() 
        => $"{(this.Name ?? "").Trim()}{(this.Surname ?? "").Trim()}".ToSubSHA256();
}

Now accordingly to the configuration i have set the entity name ( named “Person”) so when i save it, a collection named “Person” should created regardeless of the type.

But if my saving command is : var task = MongoDB.Entities.DB.SaveAsync<Entity>(model);

Then the collection is named Entity : image

If i change the save command to : var task = MongoDB.Entities.DB.SaveAsync<Person>(model); everything working fine

Mistery in the mistery i see a new property (in the mongo document) named _t that refer the correct type/name. A bit confused on what’s going on.

My goal is to save derived type in different collection.

Ps : i have now noticed that when i save thorugh the “Entity” (the faulty one) version of the command the CreatedOn and ModificedOn property are not considered. That part is handled in that library (MongoDB.Entities) so this let me suppose that somewhere in the library code, the generic type of SaveAsync method is checked instead of the type of the actual instance provided.

PPs : after a bit of digging in the library code here my suspected line of code image image

in yellow the problem about date in azure the problem about the attribute

It’s not clear to me (but i have to admit that i have not completley clear the architecture) why cache work on the generic type and not over instance type

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
paolobrionescommented, Jan 15, 2021

I agree with @dj-nitehawk

my opinion is that the exact type should be used in the api explicitly (or via inference). By allowing DB.SaveAsync< Entity >() would introduce messy usage patterns and would require reflection that can be avoided by just specifying the correct type.

0reactions
ItSkarycommented, Jan 16, 2021

Ok thanks for tips, i’ll keep in mind for future post

Read more comments on GitHub >

github_iconTop Results From Across the Web

Weird pointer behavior C++ / inheritance
For some background, I have created a base class and a derived class. This derived class inherits some member variables and some get/set ......
Read more >
Inheritance — What your mother never told you, C++ FAQ
Should a derived class redefine (“override”) a member function that is non- virtual in a base class? What's the meaning of, Warning: Derived::f(char)...
Read more >
Strange behavior of virtual methods in inherited classes
When you defined ProcessKey() as a virtual function, you said that any class that derived from fmMenuItem would implement that method.
Read more >
Bug: Accessing private statics in a class via its derived class is ...
Yeah, that behavior don't make much sense. I think it's quite strange that derived classes clone static fields from the base class, regardless...
Read more >
Strange behavior with dynamic_cast
If an object belongs to a class that uses multiple inheritance, it will have more than one vptr , and they will appear...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found