-
Type: New Feature
-
Resolution: Works as Designed
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.24.0
-
Component/s: Linq, LINQ3, Serialization
-
None
-
Dotnet Drivers
Summary
We are using an "Rich Domain Model" approach in our projects. This means, among other things, that we make use of "ValueObjects" to represent specific concepts in our Domain. These "ValueObjects" are often just wrapper around a string with some behavior, e.g. validation.
With LinqProvider.V2 a simple query like
var query = collection.Find(e => e.ValObj == "123x");
with the document type being
public class Entity { public string Id { get; private set; } public ValObj ValObj { get; private set; } public string Name { get; private set; } public Entity(ValObj valObj, string name) { ValObj = valObj; Name = name; } }
and the "ValObj" property looking like this (please see attached unit test project for full details)
public sealed class ValObj : StringValueObject { public ValObj(string value) : base(value) { if (!IsValid(value)) { throw new ArgumentException($"'{value}' is not valid", nameof(value)); } Value = value.TrimStart('0'); } private static bool IsValid(string val) { return !string.IsNullOrWhiteSpace(val); } }
would translate to the very sensible
find({"ValObj": "123x"})
With LinqProvider.V3 the same query translates to
find({ "$expr" : { "$eq" : [{ "$toString" : "$ValObj" }, "123x"] } })
The query produced by V3 works, but does not take advantage of indexes (apparently).
How to Reproduce
Please see attached unit test project.