Skip to content
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

Fix driver results initialization method usage with dynamic caching #16644

Merged
merged 7 commits into from
Aug 31, 2024

Conversation

sebastienros
Copy link
Member

Mike was reporting that when cache the delegate passed in the Initialize method of a driver was invoked every time. At some point this has been broken and the delegate was used directly instead of being provided as the ShapeMetadata.OnProcessing value.

This fixes it and adds unit tests. There is no breaking change, just a few refactorings to make the methods dependencies more logical.

Copy link
Member

@MikeAlhayek MikeAlhayek left a comment

Choose a reason for hiding this comment

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

Some formatting feedback.

If with this fix, we no longer calls the initializer of a model when the shape is cached, I am wondering if you would notice a performance improvement?

From the looks of it, we should see reduction in allocations.

return shape;
});
initialize?.Invoke(shape);
return Task.FromResult(shape);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return Task.FromResult(shape);
return Task.FromResult(shape);

initialize((TModel)shape);
return new ValueTask<IShape>(shape);
initialize?.Invoke(model);
return ValueTask.CompletedTask;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return ValueTask.CompletedTask;
return ValueTask.CompletedTask;

var contentShape = await factory.CreateAsync("Content");
await shapeResult.ApplyAsync(new BuildDisplayContext(contentShape, "Detail", "", factory, null, null));

// Shape is created and initialized
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Shape is created and initialized
// Shape is created and initialized.

Comment on lines 220 to 221


Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

var buildDisplayContext = new BuildDisplayContext(contentShape, "Detail", "", factory, null, null);
var driverResult = new MyDisplayDriver().Display(contentItem, buildDisplayContext);
await driverResult.ApplyAsync(buildDisplayContext);
return driverResult;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return driverResult;
return driverResult;

public int MyProperty { get; set; } = 3;
}

public class MyDisplayDriver : ContentDisplayDriver
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public class MyDisplayDriver : ContentDisplayDriver
public sealed class MyDisplayDriver : ContentDisplayDriver

{
public override IDisplayResult Display(ContentItem model, BuildDisplayContext context)
{
return Initialize<MyModel>(model => model.MyProperty++).Location("Content").Cache("id", ctx => ctx.WithExpiryAfter(TimeSpan.FromSeconds(1)));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return Initialize<MyModel>(model => model.MyProperty++).Location("Content").Cache("id", ctx => ctx.WithExpiryAfter(TimeSpan.FromSeconds(1)));
return Initialize<MyModel>(model => model.MyProperty++)
.Location("Content")
.Cache("id", ctx => ctx.WithExpiryAfter(TimeSpan.FromSeconds(1)));

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.

2 participants