Code Monkey home page Code Monkey logo

Comments (5)

sliekens avatar sliekens commented on July 17, 2024 1

Apparently, providing an object that implements IDbContextFactory is completely optional.

public DbContextScopeFactory(IDbContextFactory dbContextFactory = null)
{
    _dbContextFactory = dbContextFactory;
}

But as you discovered, it seems that SimpleInjector does not support constructors with defaulted parameters (in this case the default is null).

I tried explicitly registering null for service type IDbContextFactory, but that is also not supported by SimpleInjector.

You could write your own implementation and register it in your container. The default implementation can be found here:
https://github.com/mehdime/DbContextScope/blob/master/Mehdime.Entity/Implementations/DbContextCollection.cs#L64-L68

public class DefaultDbContextFactory : IDbContextFactory
{
    public TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext
    {
        return Activator.CreateInstance<TDbContext>();
    }
}

container.Register<IDbContextFactory, DefaultDbContextFactory>();

@mehdime you could improve support for dependency injection by splitting the DbContextCollection constructor into overloads with and without parameters.

// default ctor for improved support for dependency injection containers that don't support default parameters
public DbContextScopeFactory()
    : this(null)
{
}

public DbContextScopeFactory(IDbContextFactory dbContextFactory = null)
{
    _dbContextFactory = dbContextFactory;
}

from dbcontextscope.

simon-nguyen avatar simon-nguyen commented on July 17, 2024 1

@wheeeels, @StevenLiekens
I am using SimpleInjector too, and the library allows me to manage the lifetime of an instance (Scoped, Transient, Singleton) when registering in an Owin WebApi 2 application. In case of DbContextScope I am currently using as below

var container = new Container();

// To allow scoped instances to be resolved during an OWIN request,
// the following registration needs to be added to the IAppBuilder instance:
app.Use(async (context, next) =>
{
    using (container.BeginExecutionContextScope())
    {
          await next();
     }
});

/**
 * There will be only one instance of a given service type within a certain (explicitly defined) scope and
 * that instance will be disposed when the scope ends(unless specified otherwise).
 * This scope will automatically flow with the logical flow of control of asynchronous methods.
 **/
container.Options.DefaultScopedLifestyle = new ExecutionContextScopeLifestyle();

container.Register<IDbContextFactory, DefaultDbContextFactory>(Lifestyle.Scoped);
container.Register<IDbContextScopeFactory, DbContextScopeFactory>(Lifestyle.Scoped);
container.Register<IAmbientDbContextLocator, AmbientDbContextLocator>(Lifestyle.Scoped);

container.Register<IRepositoryAsync<User>, Repository<User>>(Lifestyle.Scoped);

container.Register<IUserService, UserService>(Lifestyle.Scoped);

container.Verify();

My question is "Does Lifestyle.Scoped suit for Owin WebApi 2 which is using DbContextScope?".

I've read that LifeStyle.Scoped will ensure the container its disposal if the type of cached instance implements IDisposable.

There's already a thread that said Singleton would make sense. But I see that you two have been using SimpleInjector so please give me some advise. Thank you.

from dbcontextscope.

wheeeels avatar wheeeels commented on July 17, 2024

With that default implementation, I can now inject without any problems.
Thank you for your help!

from dbcontextscope.

sliekens avatar sliekens commented on July 17, 2024

I don't know, sorry.

from dbcontextscope.

rlightner avatar rlightner commented on July 17, 2024

@simon-nguyen What does your Repository class look like?

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.