[CSHARP-197] Proxy attribute on properties? Created: 10/Apr/11  Updated: 05/Apr/19  Resolved: 04/May/12

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.0
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Andrew Armstrong Assignee: Craig Wilson
Resolution: Won't Fix Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-232 Create DBRefs with conventions Closed

 Description   

It would be great if I could mark a class with an attribute so that the C# driver knows it needs to load/fetch (a join) additional documents without having to hand code this each time:

class Order
{
[BsonReference("db.users", "_id", "UserId)]
public User User

{get;set;}
...
}

- Or lazy loading -

class Order
{
[BsonReference("db.users", "_id", "UserId)]
public Reference<User> User {get;set;}

...
}

In both examples, I indicate that this Order class (that the driver knows about due to mappings) has a User property that I would like to be loaded each time an Order is loaded as well. Let the driver load this on my behalf.

The first version would always load the property, the second would lazy load for me - accessing the order.User property would lazy load the proxy in the background (the driver would fetch the instance for me).

I'd like to avoid having to use a 'DBRef' in this case because the relationship is obvious (its always going to be a User I want loaded, found in the db.users collection).

I also want to avoid using the 'FollowReference' syntax because it means I can't just use order.User I need to put a UserId on the Order instance (which is a bit unclean) and would rather be able to access the object directly (while being aware its going to cost in terms of performance).

Thoughts?

  • Andrew


 Comments   
Comment by Craig Wilson [ 04/May/12 ]

Andrew,
We have discussed this request and determined that supporting proxies and lazy loading is beyond the scope of the driver. The use case for this feature would get used so rarely that it is not worth the complexity of implementation. While it is a little inconvenient to "dereference" manuallly, it is supported and available.

However, if you truly want this feature, you can implement a custom IBsonSerializer that generates the necessary code yourself using Castle's DynamicProxy, LinFu, or another proxy generating library.

Craig

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