[CSHARP-396] BsonClassMap.AutoMap() does not incorporate inheritance. Created: 17/Feb/12  Updated: 05/Apr/19  Resolved: 17/Feb/12

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.3.1
Fix Version/s: 1.4

Type: Task Priority: Major - P3
Reporter: Robert Poskevich Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: bson, driver, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Mono 2.10.8


Issue Links:
Related
related to CSHARP-400 Improve error message in SetIdMember ... Closed

 Description   

Would you please explain why BsonClassMap.AutoMap() does not map inherited members? What might the possible side effects be of changing this in the source, if any.

The changes I am considering are as follows:

  • Removing any instances of BindingFlags.DeclaredOnly, namely in Bson => Serialization => Conventions => MemberFinderConventions and Bson => Serialization => BsonClassMap
  • Removing check "if (memberInfo.DeclaringType != _classType)" and ensuing Exception in Bson => Serialization => BsonClassMap

Thanks for any help you can provide.



 Comments   
Comment by Robert Stam [ 17/Feb/12 ]

When registering class maps for an inheritance hierarchy you must register each class in the hierarchy separately. Each class map only maps the properties for that class; any inherited properties are handled by the class map for the class in which it is defined.

The correct way to register the class maps for these two classes is:

BsonClassMap.RegisterClassMap<A>(cm =>
{
    cm.AutoMap();
    cm.SetIdMember(cm.GetMemberMap(a => a.Id));
});
BsonClassMap.RegisterClassMap<AB>();

See also CSHARP-400 which I have created for making the error message more descriptive.

Comment by Robert Poskevich [ 17/Feb/12 ]

No, I am getting a runtime exception "Invalid memberMap" at Serialization => BsonClassMap => SetIdMember(BsonMemberMap memberMap). Because in my example, _declaredMemberMaps only contains "b" (and is looking for "Id").

You're right, that code wouldn't compile, and I apologize for being misleading. In reality, I am passing the Expression "ab => ab.Id" to this method. I want to manually specify the primary key, which 9/10 will be the Bson ObjectId. The problem arises in that I generally store many Bson ObjectIds in my classes, and so I do not want to rely on the AutoMap to pick my primary key nor do I want to be rely on Bson Attributes (abstraction, in my case, is critical).

Thanks for all your help thus far. Maybe this is another Mono issue.

Comment by Robert Stam [ 17/Feb/12 ]

You are getting a compiler error, right? (since the compiler doesn't know if the generic type argument TType has an Id property or not).

If you are not doing anything more than what you have shown I think you can just let the classes automap by themselves (which the driver does on demand if needed).

Comment by Robert Poskevich [ 17/Feb/12 ]

So I am using the following code to map my class and to specify the primary key. My class structure is also provided below. This code fails for class type AB (TType = AB), as it fails to find member "Id". Is there a better approach I should be using?

if(!BsonClassMap.IsClassMapRegistered(typeof(TType)))
{
BsonClassMap.RegisterClassMap<TType>(cm =>

{ cm.AutoMap(); cm.SetIdMember(cm.GetMemberMap(ab => ab.Id)); }

);
}

public class A
{
public ObjectId Id

{ get; set; }

public int a { get; set; }

}

public class AB : A
{
public int b

{ get; set; }

}

Comment by Robert Stam [ 17/Feb/12 ]

The fields and properties of the base class will be mapped when the base class is mapped. We don't want to map things twice.

Each class in the class hierarchy is responsible for mapping only its own fields and properties.

So class maps do incorporate inheritance, although perhaps in a slightly different way than you expected.

Comment by Robert Poskevich [ 17/Feb/12 ]

"Removing check ""if (memberInfo.DeclaringType != _classType)"" and ensuing Exception in Bson => Serialization => BsonClassMap" should read as follows:

Removing check "if (memberInfo.DeclaringType != _classType)" and ensuing Exception in Bson => Serialization => BsonClassMap => MapMember(MemberInfo memberInfo)

Generated at Wed Feb 07 21:36:42 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.