[CSHARP-1383] Unable to specify member map for generic base class. Created: 21/Aug/15 Updated: 09/Nov/22 Resolved: 09/Nov/22 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | BSON, Serialization |
| Affects Version/s: | 2.2 |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Benjamin Randall | Assignee: | James Kovacs |
| Resolution: | Won't Do | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
If you have a generic base class in a contracts library that doesn't have a dependency on Mongo.
Using the current BsonClassMap there does not appear to be a way to indicate that Properties should be used as the ExtraElements attribute without checking that the type has been mapped on each call and creating a mapping then. |
| Comments |
| Comment by James Kovacs [ 09/Nov/22 ] |
|
This feature can be implemented using a custom convention as discussed in the comments and demonstrated in the attached pull request. The PR was declined as this is not a common idiom and would be better suited for a blog post. |
| Comment by Benjamin Randall [ 09/Dec/15 ] |
|
No change on my pull request. Any idea when it will get looked at? |
| Comment by Benjamin Randall [ 24/Aug/15 ] |
|
Here's the pull request: https://github.com/mongodb/mongo-csharp-driver/pull/214/commits |
| Comment by Benjamin Randall [ 24/Aug/15 ] |
|
You are correct. It looks like conventions would be the best option for this. I've got a fork out and I'm writing up a simple example of how to write a generic convention. I'll send a pull request when it's done. Is there any way to assign this bug to me? Or should I just ping you when I'm done. |
| Comment by Craig Wilson [ 21/Aug/15 ] |
|
Ok, actually, for now, I think you may need to use conventions to handle this. You'll need to write a custom convention (http://mongodb.github.io/mongo-csharp-driver/2.0/reference/bson/mapping/conventions/). Basically, check to see if the type is generic and, if so, get its generic type definition and compare it to typeof(Thing<>). When this is true, find the Properties member and set it to be the extra elements, I think this type of thing is certainly worthy of an example in the documentation. In addition, we might should figure out how to make this easier, although this is the first question we've had related to this topic. |
| Comment by Craig Wilson [ 21/Aug/15 ] |
|
Oh, I think I see what you are talking about. The fact that your class is generic means you aren't able to use code because you don't necessarily know all the types that will be used. Hmm... Let me think on this for a little. We might need to add a feature to allow this sort of thing. |
| Comment by Craig Wilson [ 21/Aug/15 ] |
|
Hi Benjamin, I don't know what the version of the driver you are using is. I"m going to assume the latest, although this particular feature is supported back for a long time. Everything that can be done using attributes can be done via code. This is demonstrated on the docs page. Craig |