title: Dependency injection between two ASP.NET Core projects
body: <p>I'm currently developing a web application with ASP.NET Core and handling the database with Entity Framework Core. I have two projects in my VS Solution; WebApp (the main application) and DatabaseHandler (the EF Core handler). I have installed Entity Framework Core with the Pomelo package, since I'm using a MySQL database.</p>
  <p>I've been following the Microsoft documentation to setup EF Core, connection strings and all that, and it works fine. I'm able to make migrations, make updates and do stuff with the database. I'm however not sure if I'm doing it correctly, since the latest EF Core tutorials use dependency injection and I'm not familiar with it.</p>
  <p>Right now I'm passing the DbContext object as an argument from WebApp to DatabaseHandler, since I want all database-related stuff to only exist in DatabaseHandler. This works, but is it possible to call functions from another project and also share the DbContext object without passing it as an argument? I'm probably not explaining it well, I hope my code explains it better.</p>
  <p><strong>WebApp/Startup.cs:</strong><br>
  This is where I load the connection string from appsettings.json.</p>
  <pre><code>public Startup(IConfiguration configuration)
  {
      Configuration = configuration;
  }
  
  public IConfiguration Configuration { get; }
  
  public void ConfigureServices(IServiceCollection services)
  {
      services.AddDbContextPool&lt;DataContext&gt;(
          options =&gt; options.UseMySql(Configuration.GetConnectionString(&quot;DefaultConnection&quot;)
      ));
  
      services.AddRouting(options =&gt; options.LowercaseUrls = true);
      services.AddControllersWithViews();
  }
  </code></pre>
  <p><strong>WebApp/HomeController.cs:</strong><br>
  This is where I call the GetAllChallenges() function from the DatabaseHandler project, and I also pass the DataContext object as an argument. This is what I'm trying to avoid!</p>
  <pre><code>public class HomeController : Controller
  {
      private readonly ILogger&lt;HomeController&gt; _logger;
      private readonly DataContext db;
  
      public HomeController(ILogger&lt;HomeController&gt; logger, DataContext _db)
      {
          _logger = logger;
          db = _db;
      }
  
      public IActionResult Challenges()
      {
          List&lt;Challenge&gt; ChallengesList = DatabaseHandler.HandleChallenges.GetAllChallenges(db);
          return View(ChallengesList);
      }
  }
  </code></pre>
  <p><strong>DatabaseHandler/DataContext.cs:</strong><br>
  This is where I initialize the entity classes and so on.</p>
  <pre><code>public class DataContext : DbContext
  {
      public DataContext(DbContextOptions&lt;DataContext&gt; options) : base(options) { }
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { }
  
      // Tables
      public DbSet&lt;User&gt; Users { get; set; }
      public DbSet&lt;Challenge&gt; Challenges { get; set; }
  
      // Data seeding
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Seed();
      }
  }
  </code></pre>
  <p><strong>DatabaseHandler/HandleChallenges.cs:</strong><br>
  This is where I have all my database functions. The results are returned back to the controller within the WebApp project.</p>
  <pre><code>public class HandleChallenges
  {
      public static List&lt;Challenge&gt; GetAllChallenges(DataContext db)
      {
          var Data = db.Challenges;
          List&lt;Challenge&gt; ChallengesList = Data.ToList();
          return ChallengesList;
      }
  }
  </code></pre>
  <p>I have looked into dependency injection, but I'm not sure how I can use this between two projects. Is there a less complicated way of achieving this, perhaps without using DI at all? I'm satisfied as long as I don't need to pass the DataContext object as an argument every time I need to call a function from DatabaseHandler.</p>
  <p>Can someone help me understand? Thanks a lot in advance!</p>