public static UpdateDefinition<T> SetIfValue<T>(this UpdateDefinition<T> builder, Expression<Func<T, string>> setter, string value)
{
if (!string.IsNullOrEmpty(value))
builder.Set(setter, value);
return builder;
}
[Test] public void TestExtension() { var subject = CreateSubject<Person>(); Assert(subject.SetIf(x => x.FirstName, "Jack"), "{$set: {fn: 'Jack'}}"); Assert(subject.SetIf(x => x.FirstName, null), "{ }"); Assert(subject.Set(x => x.Age, 10).SetIf(x => x.FirstName, "Jack"), "{$set: {age: 10, fn: 'Jack'}}"); Assert(subject.Set(x => x.Age, 10).SetIf(x => x.FirstName, null), "{$set: {age: 10}}"); } public static class UpdateDefExtensions { public static UpdateDefinition<TDocument> SetIf<TDocument>(this UpdateDefinition<TDocument> builder, Expression<Func<TDocument, string>> field, string value) { if (!string.IsNullOrEmpty(value)) { return builder.Set(field, value); } return builder; } public static UpdateDefinition<TDocument> SetIf<TDocument>(this UpdateDefinitionBuilder<TDocument> builder, Expression<Func<TDocument, string>> field, string value) { if (!string.IsNullOrEmpty(value)) { return builder.Set(field, value); } return NoopUpdateDefinition<TDocument>.Instance; } public sealed class NoopUpdateDefinition<TDocument> : UpdateDefinition<TDocument> { private static readonly NoopUpdateDefinition<TDocument> __instance = new NoopUpdateDefinition<TDocument>(); private NoopUpdateDefinition() { } public static NoopUpdateDefinition<TDocument> Instance { get { return __instance; } } public override BsonDocument Render(IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry) { return new BsonDocument(); } } }
The only different between your implementation of the Noop and mine was that I wasn’t using a singleton, instead returning a new instance each time.
I’ll give this a try and let you know how it goes J
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see:
http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/mongodb-user/d5seLlEzHaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
mongodb-user...@googlegroups.com.
To post to this group, send email to
mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit
https://groups.google.com/d/msgid/mongodb-user/2e6b5a04-4f1a-49aa-9962-f6a30093660a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Actually, looking at the bug, I think that’s what I was hitting to corrupt my documents, they ended up only having their `_id` left.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/SIXPR01MB05413C47745B5F457147AFA6EED50%40SIXPR01MB0541.apcprd01.prod.exchangelabs.com.
To unsubscribe from this group and all its topics, send an email to mongodb-use...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/2e6b5a04-4f1a-49aa-9962-f6a30093660a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/d5seLlEzHaI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mongodb-use...@googlegroups.com.
To post to this group, send email to mongo...@googlegroups.com.