MVC 项目之外的 DbContext 依赖注入

Dav*_*504 3 asp.net-mvc dependency-injection entity-framework-6 asp.net-core asp.net-core-2.0

我有一个 C# 解决方案,其中包含两个项目 ProductStore.Web 和 ProductStore.Data,它们都针对 .NET Core 2.0。

我有我的 HomeController 和 CustomerRepository 如下(我已经在 HomeController 中设置它以提高速度,客户创建将在客户控制器中,但还没有脚手架出来):

namespace ProductStore.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly DatabaseContext _context;

        public HomeController(DatabaseContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            ICustomerRepository<Customer> cr = new CustomerRepository(_context);
            Customer customer = new Customer
            {
                // customer details
            };
            //_context.Customers.Add(customer);
            int result = cr.Create(customer).Result;
            return View();
        }
    }
}

namespace ProductStore.Data
{
    public class CustomerRepository : ICustomerRepository<Customer>
    {
        DatabaseContext _context;
        public CustomerRepository(DatabaseContext context)
        {
            _context = context;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

依赖注入会在控制器内部自动解析 _context。然后我将上下文作为驻留在 ProductStore.Data 中的 CustomerRepository 的参数传递。

我的问题有两个:

  1. 这是最佳实践吗(将上下文从控制器传递到 CustomerRepository)
  2. 如果不是最佳实践,我是否可以通过IServiceCollection services与如何将 DatabaseContext 插入到我的应用程序 StartUp.cs 类中的服务类似的方式来访问上下文...

我觉得我不应该传递上下文,CustomerRepository 应该负责获取上下文。

仅供参考,MVC 相对较新,实体框架和依赖注入是全新的

谢谢

Rub*_*yan 5

您不需要传递contextcontroller就可以使用context存储库内注册的服务。我更喜欢这样做的方式如下。注射contextrepository,然后注入repository到控制器。在 .Net Core 中使用 Microsoft Dependency Injection Extension 它看起来像这样

// Service registrations in Startup class
public void ConfigureServices(IServiceCollection services)
{
    // Also other service registrations
    services.AddMvc();
    services.AddScoped<DatabaseContext, DatabaseContext>();
    services.AddScoped<ICustomerRepository<Customer>, CustomerRepository>();
}

// Controller
namespace ProductStore.Web.Controllers
{
    public class HomeController : Controller
    {
        private readonly ICustomerRepository _customerRepository;

        public HomeController(ICustomerRepository customerRepository)
        {
            _customerRepository = customerRepository;
        }
        public IActionResult Index()
        {
            Customer customer = new Customer
            {
                // customer details
            };
            //_context.Customers.Add(customer);
            int result = _customerRepository.Create(customer).Result;
            return View();
        }
    }
}

//Repository
namespace ProductStore.Data
{
    public class CustomerRepository : ICustomerRepository<Customer>
    {
        DatabaseContext _context;
        public CustomerRepository(DatabaseContext context)
        {
            _context = context;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此之后,当DependencyResolver尝试解析ICustomerRepository注入HomeController他看到的ICustomerRepository(在我们的例子中CustomerRepository)的注册实现有一个构造函数需要DatabaseContext作为参数并DependencyResolver试图获取注册服务DatabaseContext并将其注入CustomerRepository