This article is a mirror article of machine translation, please click here to jump to the original article.

View: 27156|Reply: 2

[Source] EF DbContext guarantees that the context is unique

[Copy link]
Posted on 8/22/2020 10:21:31 AM | | |
Using EF to manipulate the database, extract the DbContext (database operation context class) in the same HTTP request to ensure uniqueness, the article uses two schemes to ensure the uniqueness of the Entity Framework context, and the solution on the .NET Core platform is attached.

Option 1

If an object is guaranteed to be globally unique, you will definitely think of a classic design pattern: singleton mode, if the object to be used must be unique in the thread?

Data slot: CallContext, ok see msdn's explanation of callcontent.

CallContext is a dedicated collection object similar to a thread-local store for method calls, and provides a data slot that is unique to each logical execution thread. Data slots are not shared between call contexts on other logical threads. Objects can be added to the CallContext when it propagates back and forth along the path of the execution code and is inspected by individual objects in that path.

That is, the current thread stores the object to the thread's local storage, and the object is destroyed as the thread is destroyed.

Use code:



Usage scenarios: I personally think it is used when the object needs to be used globally within the thread, and the other thread package extension threads cannot access it. For example, in EF's data context, a thread will be generated for each request, and at this time, a data context object will be created for different functions to use, and finally commit together to avoid transaction problems. Of course, some people may ask me if I can create a variable to use, which can also achieve the same purpose, of course, but this object can also interact with other thread data, which goes against the concept of being unique in the thread.

Option 2

The scenario is based on web http requests, because every request is a new thread, and the code is as follows:



Using "CallContext" in .NET Core

We know that CallContext is no longer available for . NETStandard or .NET Core.

But you can use Asynclocal<T> to mimic a CallContext, just create the following static class:



Namespaces that need to be referenced:



It's also easy to use (here I've put CallContext in the Common library):





Previous:ASP.NET MVC5 is released using precompiled mode, improving page load speed and performance
Next:[Turn]. NET exports PPT as a picture
Posted on 8/25/2020 11:38:22 PM |
Isn't it fragrant with infusion?
 Landlord| Posted on 8/26/2020 9:37:39 PM |
Cucumber Posted on 2020-8-25 23:38
Isn't it fragrant with infusion?

asp.net core's default injection lifecycle doesn't meet my needs

The service instances created by the Singleton: IServiceProvider object are stored on the IServiceProvider object as the root container, so multiple IServiceProvider objects with the same root for the same type of service instances are all on the same object.
The service instances created by the Scoped:IServiceProvider object are saved by themselves, so the service instances of the same type provided by the same IServiceProvider object are all the same object.
Transient: For each service provision request, the IServiceProvider object always creates a new service instance.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com