Code Monkey home page Code Monkey logo

Comments (9)

jrt324 avatar jrt324 commented on August 16, 2024

I had the same question, how to registering IDbContextScope with autofac?

from dbcontextscope.

 avatar commented on August 16, 2024

I too am wondering about this.

from dbcontextscope.

tomboon avatar tomboon commented on August 16, 2024

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.

mitsbits avatar mitsbits commented on August 16, 2024

Singleton makes sense

from dbcontextscope.

raysuelzer avatar raysuelzer commented on August 16, 2024

Singleton probably doesn't make sense in AutoFac for web requests because I believe it would be shared across requests.

from dbcontextscope.

VictorioBerra avatar VictorioBerra commented on August 16, 2024

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.

rock-walker avatar rock-walker commented on August 16, 2024

Singletons, from my perspective.

from dbcontextscope.

VictorioBerra avatar VictorioBerra commented on August 16, 2024

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.

jzabroski avatar jzabroski commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.