-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5226: Remove non-RenderArgs Render overloads #1417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| false); // renderForElemMatch | ||
|
|
||
| var renderMethod = filterDefinitionType.GetMethod(nameof(FilterDefinition<BsonDocument>.Render), new[] { renderArgsType }); | ||
| var renderedFilter = (BsonDocument)renderMethod.Invoke(filterDefinition, new object[] { renderArgs }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rstam: Just discovered this dynamic rendering due to failing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose adding a new private static helper method:
// private static methods
private static BsonDocument RenderFilter<TDocument>(FilterDefinition<TDocument> filterDefinition, IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
{
return filterDefinition.Render(new(documentSerializer, serializerRegistry));
}
and replacing lines 44-55 with:
var renderFilterMethod = typeof(InjectMethodToFilterTranslator).GetMethod(nameof(RenderFilter), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(documentType);
var renderedFilter = (BsonDocument)renderFilterMethod.Invoke(null, new object[] { filterDefinition, documentSerializer, serializerRegistry });
This localizes the use of reflection to this file only and isolates us from (or at least turns into compile time errors) changes to the RenderArgs constructor or the signature of the Render method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea! Done, thanks.
rstam
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small change requested.
| false); // renderForElemMatch | ||
|
|
||
| var renderMethod = filterDefinitionType.GetMethod(nameof(FilterDefinition<BsonDocument>.Render), new[] { renderArgsType }); | ||
| var renderedFilter = (BsonDocument)renderMethod.Invoke(filterDefinition, new object[] { renderArgs }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I propose adding a new private static helper method:
// private static methods
private static BsonDocument RenderFilter<TDocument>(FilterDefinition<TDocument> filterDefinition, IBsonSerializer<TDocument> documentSerializer, IBsonSerializerRegistry serializerRegistry)
{
return filterDefinition.Render(new(documentSerializer, serializerRegistry));
}
and replacing lines 44-55 with:
var renderFilterMethod = typeof(InjectMethodToFilterTranslator).GetMethod(nameof(RenderFilter), BindingFlags.Static | BindingFlags.NonPublic).MakeGenericMethod(documentType);
var renderedFilter = (BsonDocument)renderFilterMethod.Invoke(null, new object[] { filterDefinition, documentSerializer, serializerRegistry });
This localizes the use of reflection to this file only and isolates us from (or at least turns into compile time errors) changes to the RenderArgs constructor or the signature of the Render method.
rstam
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some naming fell through the cracks.
| { | ||
| internal static class InjectMethodToFilterTranslator | ||
| { | ||
| private readonly static MethodInfo __renderMethodInfo; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this optimization of caching the MethodInfo in a static field.
But the field should be named __renderFilterMethodInfo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| var renderMethod = filterDefinitionType.GetMethod("Render", renderMethodArgumentTypes); | ||
| var renderedFilter = (BsonDocument)renderMethod.Invoke(filterDefinition, new object[] { documentSerializer, serializerRegistry }); | ||
|
|
||
| var renderMethod = __renderMethodInfo.MakeGenericMethod(documentType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
variable should be called renderFilterMethod
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
rstam
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
No description provided.