Comments (9)
I had the same question, how to registering IDbContextScope with autofac?
from dbcontextscope.
I too am wondering about this.
from dbcontextscope.
We are also wondering what we need to register in Autofac to make this work.
We "think" we need to register in Autofac:
- IDbContextFactory and our own implementation for this
- IDbContextScopeFactory and DbContextScopeFactory
- IAmbientDbContextLocator and AmbientDbContextLocator
Also, we are using WCF, which is currently marking our transaction boundary (using autofachostfactory). How does this integrate with WCF and Autofac?
from dbcontextscope.
Singleton makes sense
from dbcontextscope.
Singleton probably doesn't make sense in AutoFac for web requests because I believe it would be shared across requests.
from dbcontextscope.
Since you are using IoC I think you use whatever makes sense for whatever is consuming your service like Mehdi argues. If you are using MVC, have your web app configure the container for web requests. If you build a CLI for your service, you can create the context when your app starts and dispose of it after your command completes and the app exits.
What Mehdi is saying at the core of his article is the service needs to be the one in charge of the lifecycle of the DbContext so you can use transactions. But who's in charge of the life-cycle of the service? Whatever app is making use of the service. A web app is unique because it's technically running 24/7. If you take IoC out of the equation, it might help you think about this better. Just like when Mehdi say's he stopped calling his library a UnitOfWork things became more clear.
Consider if you had to choose manually on when you would new up your service.
from dbcontextscope.
Singletons, from my perspective.
from dbcontextscope.
It might be helpful to look into ASP.NET Core and how people are doing this over there. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection DI is a first class citizen now. They might have the same issue as we are trying to solve here or they might not.
For what it's worth, there is nothing wrong with using a scoped lifetime. This library just makes it easy to let your services handle the transactions and also gives your repos easy access to the context without too much passing things around.
from dbcontextscope.
Invert the dependency!
Always invert! Don't locate your dependencies, ask for them:
public class Example {
public Func<IDbContext> DbContext { get; set; }
Example(Func<IDbContext> dbContext)
{
DbContext = dbContext;
}
}
Then you need a TransactionManager which will be responsible for creating scopes and threading the right DbContextScope dependency to Func, like so:
// open a logical transaction
using (TransactionManager.OpenScope())
{
DbContext().Save();
}
from dbcontextscope.
Related Issues (20)
- Compatibility with Effort
- Use with EntityFramework Reverse POCO Code First Generator? HOT 1
- nested IDbContextFactory.CreateWithTransaction HOT 1
- Need way to disable disposing of DbContext when root DbContextScope is disposed HOT 3
- .net core HOT 2
- Unable to use a non-interfaced DbContext with DbContextScope HOT 2
- TDD with DbContextScope HOT 1
- Connect to database with a different user/connection string HOT 5
- Moving to .Net Standard 2.0 & EF 2.0 HOT 8
- Best practice for taking advantage of EF first-level caching in Domain Service Layer. HOT 5
- Working with Store Procedure and Transaction
- DbContextScope with EF Core !! HOT 6
- Dead link
- Problems when attaching new dbcontexts while performing savechanges
- The current type, EntityFramework.DbContextScope.Interfaces.IDbContextFactory, is an interface and cannot be constructed. Are you missing a type mapping?
- Suggestions: how to retrieve ID generated by DB after saveChanges() HOT 2
- how to use DbContextScope with Parallel.ForEach
- Satellite class to use the same Factory/Locator across application modules
- What to pass to IDbContextFactory argument in DbContextScope constructor? HOT 1
- Support of ASP.NET Core dependency injection out of the box
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dbcontextscope.