ISmellYu
ISmellYu

Reputation: 119

Best approach for adding another DbContext

So I was wondering what's the best approach to attach another DbContext to my ASP.NET app. I'm trying to achieve DbContext that I can use anywhere in my code not only in controllers. I tried to add it as service but failed. In Configure i'm creating a new class with IHubContext to connect to clients, but i also need to retrieve data from database so i was thinking maybe about editing constructor Baloon and adding DbContext as required. I wanna hear from you guys what's gonna be the best.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddDbContext<DiscordContext>();
    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    StartBalloon(app.ApplicationServices.GetRequiredService<IHubContext<BalloonHub>>());
    ...
}

private static void StartBalloon(IHubContext<BalloonHub> context)
{
    Globals.Bln = new Balloon(context); // Starting balloon
}

DiscordContext.cs

public class DiscordContext : DbContext
{
    public DbSet<DbUser> Users { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseMySQL("---connection_string----");
    }
}

Baloon.cs

public class Baloon
{
    private IHubContext<BalloonHub> _context;
    private DbContext _db;
    private List<BalloonPlayer> _currentPlayerList = new();
    public int? WhenToPop;
    public int? CurrentLevel;
    public int? BotValue;
    public int? CurrentValue;
    public bool Pooped;
    public bool Running;
    public Balloon(IHubContext<BalloonHub> context)
    {
        _context = context;
        Task.Run(Main);    // Let's imagine that there is need for data from database(DbContext)
    }
}

Upvotes: 0

Views: 301

Answers (3)

Thomson Mixab
Thomson Mixab

Reputation: 657

For dispose best practice, you can look at the example by Panagiotis Kanavos. Good luck

Destructor never gets called

Upvotes: 1

ISmellYu
ISmellYu

Reputation: 119

So I managed to do something like this

DiscordControl.cs

public class DiscordControll : IDisposable
    {
        public bool IsDead { get; set; }
        public DiscordControll()
        {
        }

        private DiscordContext _database;

        protected DiscordContext Database
        {
            get
            {
                return _database ??= new DiscordContext();
            }
        }

        public void Dispose()
        {
            IsDead = true;
            Database.Dispose();
        }
    }

Baloon.cs

public class Baloon : DiscordControll
{
    private IHubContext<BalloonHub> _context;
    private List<BalloonPlayer> _currentPlayerList = new();
    public int? WhenToPop;
    public int? CurrentLevel;
    public int? BotValue;
    public int? CurrentValue;
    public bool Pooped;
    public bool Running;
    public Balloon(IHubContext<BalloonHub> context)
    {
        _context = context;
        using (Database)
        {
            Console.WriteLine(
            Database.Users.GetUserByUlong(discordId).Points)
        }
        Console.WriteLine(IsDead)
        Task.Run(Main);    
    }
}

It works, but it's not disposing, IsDead is always false so I'm assuming that it's active.

Upvotes: 1

Thomson Mixab
Thomson Mixab

Reputation: 657

You can create a base class for the controller which expose DbContext to your deride class. Similar concept can also be apply to other normal business class.

Try a look at the URL below.

When should I create a new DbContext()

Upvotes: 1

Related Questions