2

My app uses a singleton pattern for a handler class instance. It is responsible for handling some events:

public class MyHandler
{
    public void HandlerEvent(object sender, EventArgs e)
    {
        //want to update DB here
    }
}

It is possibly MyHandler will need to start interaction with DB. So can I do it? There is my vision:

  1. Just tough attach DbContext as singleton at MyHandler. Obviously it's a bad idea.
  2. Use ASP.Net Core DI features and send DbContext to MyHandler, but as an instance "one per request". I think in my case (MyHandler is singleton) this is similar to 1
  3. Do it through using operator, i.e. as atomic transaction, for e.g. using(var context = new XDbContext()) {...} As for me it's a good approach, but DbContext of Entity Framework Core implementation needs DbContextOptions as argument of its constructor. If I declare parameterless constructor for XDbContext then it throws an exception.

Any ideas?

3
  • Why don't you renew the instance in every request? Like for example have a DbContext property in your MyHandler class and in it's get renew your instance of XDbContext you don't need to worry about previous instances since they'll be collected via GC.
    – Emad
    Dec 25, 2016 at 6:01
  • 1
    @Emad in my case HandlerEvent processes messages which arrives from azure service bus, i.e. it does not depend on http-requests Dec 25, 2016 at 10:08
  • My point is still valid though the answer of Egorikas does the same thing using the factory pattern. You can have the entire code of the factory create in get method of my DbContext property.
    – Emad
    Dec 25, 2016 at 10:12

1 Answer 1

1

I've seen an ef core tutorial (https://learn.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext) and as I understood, DbContextOptions - is just an object with configuration parameters inside it. If I had same problem, I would use the third way (with using), but would create a helper for injecting parameters or using a factory for this. I found an example of factory in the tutorial

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace MyProject
{
   public class BloggingContextFactory : IDbContextFactory<BloggingContext>
   {
       public BloggingContext Create()
       {
          var optionsBuilder = new DbContextOptionsBuilder<BloggingContext>();
          optionsBuilder.UseSqlite("Filename=./blog.db");

          return new BloggingContext(optionsBuilder.Options);
       }
   }
}

Example of using:

public class MyHandler
{
   public void HandlerEvent(object sender, EventArgs e)
   {
       // Or make 'Create' method static
       using(var context = new BloggingContextFactory().Create())
       {
                 . . . 
       }
   }
}
2
  • Can such approach undermine main DB-configurations which were implemented in OnModelCreating method? Or this constructor is not matter for OnModelCreating-configs? Dec 25, 2016 at 11:52
  • @Mergasov As I know, new EF core is being written in the way like ASP.NET core. Which means that we don't have web.config and store our properties in another file (appsettings.json for example), so I suppose that this approach can undermine main DB-configurations, but I'm not sure. Watch learn.microsoft.com/en-us/ef/core/miscellaneous/… and ASP.NET Core section
    – Egorikas
    Dec 25, 2016 at 12:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.