Skip to content

Conversation

@BorisDog
Copy link
Contributor

No description provided.

@BorisDog BorisDog requested a review from a team as a code owner August 15, 2024 00:17
@BorisDog BorisDog requested review from JamesKovacs and rstam and removed request for a team and JamesKovacs August 15, 2024 00:17
false); // renderForElemMatch

var renderMethod = filterDefinitionType.GetMethod(nameof(FilterDefinition<BsonDocument>.Render), new[] { renderArgsType });
var renderedFilter = (BsonDocument)renderMethod.Invoke(filterDefinition, new object[] { renderArgs });
Copy link
Contributor Author

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.

Copy link
Contributor

@rstam rstam Aug 15, 2024

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! Done, thanks.

Copy link
Contributor

@rstam rstam left a 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 });
Copy link
Contributor

@rstam rstam Aug 15, 2024

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.

@BorisDog BorisDog requested a review from rstam August 15, 2024 18:14
Copy link
Contributor

@rstam rstam left a 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;
Copy link
Contributor

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.

Copy link
Contributor Author

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);
Copy link
Contributor

@rstam rstam Aug 15, 2024

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@BorisDog BorisDog requested a review from rstam August 15, 2024 18:26
Copy link
Contributor

@rstam rstam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@BorisDog BorisDog merged commit ae4adc4 into mongodb:main Aug 15, 2024
@BorisDog BorisDog deleted the csharp5226 branch August 15, 2024 19:25
@glen-84
Copy link

glen-84 commented Sep 26, 2024

@BorisDog This is another breaking change. Why was this released in a minor version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants