[CSHARP-657] Generic Query<T> cannot serialise interface. Created: 08/Jan/13  Updated: 20/Mar/14  Resolved: 16/May/13

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

Type: Improvement Priority: Major - P3
Reporter: Sleeper Smith Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Production.



 Description   

When constructing typed query, it has problem serialising an interface.
e.g.
Query<IDocument>.EQ(a => a.Url, url)

This doesn't seem like something complicated to achieve.



 Comments   
Comment by Craig Wilson [ 16/May/13 ]

I actually is relatively difficult to achieve. Imagine the following scenario:

interface IDocument
{
  string Url { get; }
}
 
class DocumentA : IDocument
{
  [BsonElement("url")]
  string Url { get; set; }
}
 
class DocumentB: IDocument
{
  [BsonElement("address")]
  string Url { get; set; }
}

The problem with the above is that based solely on the provided interface IDocument, we don't know whether to use the "url" element name or "address" element name. More information is needed, specifically which implementation is getting used.

We do support abstract classes though, and that implies that we could do the same thing for interfaces we do for them. However, the distinct difference between an abstract class and an interface is that a class cannot inherit from multiple classes, while it could implement multiple interfaces.

So, imagine the below scenario:

 
interface IDocument
{
  [BsonElement("address")]
  string Url { get; }
}
 
interface IUrlSource
{
  [BsonElement("url")]
  string Url { get; }
}
 
class DocumentC : IDocument, IUrlSource
{
  string Url { get; set; }
}

How would we know which element name applied to Url? Even worse, if you used an interface in your query builder (Query<IDocument>.EQ...) we might choose the wrong one...

I have another scenario I'd be happy to go through where interfaces cause problems, so let me know if you'd like to see it. The sum of the situation is that using interfaces for your entities and querying on them is very difficult to get right and puts a lot of onus on you to make sure everything is kosher and prevents us from doing the right thing. So, this is not going to get fixed.

However, if you have any suggestions on how to handle this type of situation, please let us know something we may not have thought about and we'll definitely consider it.

Thanks,
Craig

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