Skip to content

Commit

Permalink
Error in ACM adapter example fixed based on feedback by @rexcfnghk. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunkie committed Dec 8, 2019
1 parent 44d4e17 commit 68f5284
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions content/posts/ambient-composition-model.md
@@ -1,12 +1,12 @@
---
title: "The Ambient Composition Model"
date: 2019-07-15
author: Steven van Deursen
reviewers: Peter Parker and Ric Slappendel
proofreaders: Katie Tennant
tags: [.NET General, Architecture, C#, Dependency Injection]
gitHubIssueId: 4
draft: false
title: "The Ambient Composition Model"
date: 2019-07-15
author: Steven van Deursen
reviewers: Peter Parker and Ric Slappendel
proofreaders: Katie Tennant
tags: [.NET General, Architecture, C#, Dependency Injection]
gitHubIssueId: 4
draft: false
aliases:
- /p/acm
---
Expand Down Expand Up @@ -169,15 +169,20 @@ In this example, `ShoppingBasketRepository` is injected with `AmbientShoppingBas
// This class will be part of your Composition Root
class AmbientShoppingBasketContextProvider : IShoppingBasketContextProvider
{
private readonly string connectionString;
private readonly AsyncLocal<ShoppingBasketDbContext> context;

public AmbientShoppingBasketContextProvider(string connectionString)
{
this.context = new AsyncLocal<ShoppingBasketDbContext>(
() => new ShoppingBasketDbContext(connectionString));
this.connectionString = connectionString;
this.context = new AsyncLocal<ShoppingBasketDbContext>();
}

public ShoppingBasketDbContext Context => this.context.Value;
public ShoppingBasketDbContext Context =>
this.context.Value ?? (this.context.Value = this.CreateNew());

private ShoppingBasketDbContext CreateNew() =>
new ShoppingBasketDbContext(this.connectionString);
}
{{< / highlightEx >}}

Expand Down
2 changes: 1 addition & 1 deletion content/posts/di-composition-models-comparison.md
Expand Up @@ -36,7 +36,7 @@ The ACM is perhaps less known, but not less interesting, and there are some down

I should start with the elephant in the room: there is little reason to try to compare the two composition models when one is technically infeasible in the environment you’re working in. I’ll give an example for each model.

With the introduction of the new asynchronous programming model in .NET 4.0, you can now write code that doesn’t block threads but instead uses the more-efficient I/O completion ports. With this model, it is no longer possible to link ambient data to a specific thread—a single request flows (sequentially) from thread to thread. Instead, different constructs should be used, such as [`AsyncLocal<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.asynclocal-1) and [`CallContext`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.remoting.messaging.callcontext). They represent “ambient data that is local to a given asynchronous control flow, such as an asynchronous method.” `AsyncLocal<T>`, however, was added to .NET 4.6, while `CallContext` is only available in the full .NET version, and its semantics changed in .NET 4.5 to make it usable in combination with async/await.
With the introduction of the new asynchronous programming model in .NET 4.0, you can now write code that doesn’t block threads but instead uses the more-efficient I/O completion ports. With this model it is no longer possible to link ambient data to a specific thread—a single request flows (sequentially) from thread to thread. Instead, different constructs should be used, such as [`AsyncLocal<T>`](https://docs.microsoft.com/en-us/dotnet/api/system.threading.asynclocal-1) and [`CallContext`](https://docs.microsoft.com/en-us/dotnet/api/system.runtime.remoting.messaging.callcontext). They represent “ambient data that is local to a given asynchronous control flow, such as an asynchronous method.” `AsyncLocal<T>`, however, was added to .NET 4.6, while `CallContext` is only available in the full .NET version, and its semantics changed in .NET 4.5 to make it usable in combination with async/await.

In the good old days, when working with the asynchronous programming model in old environments such as .NET 4.0 and Silverlight, there were issues regarding the use of ambient data.

Expand Down

0 comments on commit 68f5284

Please sign in to comment.