dotnetjunkie / blog

Files for my weblog
0 stars 1 forks source link

Comments for "The Ambient Composition Model" #4

Open dotnetjunkie opened 5 years ago

dotnetjunkie commented 5 years ago

In the section below, you can leave your comments for this blog post: https://blogs.cuttingedge.it/steven/posts/2019/ambient-composition-model/.

Please be aware that a comment posted here will be shown in the comment section of the blog post, using JavaScript. This means that you should consider this a comment directly on my weblog—not as a post on GitHub.

This means that I might

vancodocton commented 1 year ago

I visited this blog while researching the flexibility of using IHttpContextAccessor due to its performance:

The IHttpContextAccessor interface should be used with caution. As always, the HttpContext must not be captured outside of the request flow. IHttpContextAccessor: Relies on AsyncLocal which can have a negative performance impact on asynchronous calls. Creates a dependency on "ambient state" which can make testing more difficult.

I intend to design a user-contextual interface. If I want to focus on performance, which composition model should I choose?

dotnetjunkie commented 1 year ago

Creates a dependency on "ambient state" which can make testing more difficult.

This statement is true by itself, but doesn't invalidate the Ambient Composition Model (ACM) because it promotes the encapsulation of ambient data inside the Composition Root, which means hiding its use behind an abstraction. When hidden behind an abstraction (which basically means adhering to the Dependency Inversion Principle) testability won't be impacted.

About the performance aspect of ambient state the only advice I can give is: measure. You should measure whether the use of ambient state has a noticeable and significant difference in the performance of your application. For the applications I've built over the years, this has never been any issue. But if you do run an application where these kinds of performance tweaks are crucial and those perf optimizations safe more money than the development investments cost, also considering the benefits the ACM brings to the table, you should certainly run performance benchmarks to measure whether the removal of AsyncLocal makes any significant difference for your specific use case.