How to use a database context in a singleton service?

asp.net-core entity-framework-core singleton

Question

I would like to store completed chess games in the database to support users watching replays.

So far I have a singleton GameManager that stores all ongoing games. So within startup.cs I have the following line of code:

services.AddSingleton<IBattleManager, BattleManager>();

Now I would like to have BattleManager access the DbContext to save completed games.

public class BattleManager : IBattleManager
{
    //...
    private void EndGame(ulong gameId)
    {
        var DbContext = WhatDoIPutHere?
        lock(gameDictionary[gameId])
        {
            DbContext.Replays.Add(new ReplayModel(gameDictionary[gameId]));
            gameDictionary.Remove(gameId)
        }
    }
}

Is it possible to anyhow achieve this? How?

Failed attempts:

public class BattleManager : IBattleManager
{
    Data.ApplicationDbContext _context;
    public BattleManager(Data.ApplicationDbContext context)
    {
        _context = context;
    }
}

This will clearly fail since one cannot inject EF Core DbContext into a Singleton service like that.

I have a vague feeling that I should do something of this kind:

using (var scope = WhatDoIPutHere.CreateScope())
{
    var DbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
    DbContext.Replays.Add(new ReplayModel(...));
}

Is this the right direction?

1
3
8/21/2018 12:31:14 AM

Popular Answer

You're on the right track. The IServiceScopeFactory can do that.

public class BattleManager : IBattleManager {

    private readonly IServiceScopeFactory scopeFactory;

    public BattleManager(IServiceScopeFactory scopeFactory)
    {
        this.scopeFactory = scopeFactory;
    }

    public void MyMethod() {
        using(var scope = scopeFactory.CreateScope()) 
        {
            var db = scope.ServiceProvider.GetRequiredService<DbContext>();

            // when we exit the using block,
            // the IServiceScope will dispose itself 
            // and dispose all of the services that it resolved.
        }
    }
}

The DbContext will behave like it has a Transient scope within that using statement.

7
8/21/2018 12:27:20 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow