c# - 使用依赖注入(inject)访问 DbContext

标签 c# asp.net-core dependency-injection asp.net-core-mvc entity-framework-core

看不懂官方documentation ,在有关依赖注入(inject)的段落中。

他们说我可以使用 Controller (但来自 here 我知道我不需要它,因为我正在使用 Razor 页面)或者我可以直接访问 ServiceProvider:

using (var context = serviceProvider.GetService<BloggingContext>())
{
  // do stuff
}

但是如何在我的项目的通用 C# 类中检索对 ServiceProvider 的引用?

我在 startup.cs 中设置服务:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<MyDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbContext")));
    services.AddHangfire(options => options.UseSqlServerStorage(Configuration.GetConnectionString("MyDbContext")));
    services.AddOptions();
    services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
    services.AddMvc().AddDataAnnotationsLocalization();
}

编辑

为了进一步澄清我的困惑,我正在尝试做的是从 Worker 类添加/获取数据。 Here我找到了一个如何做的例子:

using (var context = new BloggingContext())
{
    var blog = new Blog { Url = "http://sample.com" };
    context.Blogs.Add(blog);
    context.SaveChanges();

    Console.WriteLine(blog.BlogId + ": " +  blog.Url);
}

但是如果我要使用依赖注入(inject),我不能使用没有参数 DbContext 的构造函数。另一方面,如果我添加参数,我必须在调用构造函数时传递正确的值,如上例所示 - 这是最初的问题。

编辑2

我将发布一个“完整”示例。我很难理解,但我正在尝试:

程序.cs

using Hangfire;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Options;

namespace MyProject
{
    public class Program
    {

        public static void Main(string[] args)
        {
            IWebHost host = BuildWebHost(args);
            BackgroundJob.Enqueue<MyClass>(x => x.ImportOperatorList());
            host.Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();

    }
}

启动.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Hangfire;
using MyProject.Models;

namespace MyProject
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<MyProjectContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyProjectContext")));
            services.AddHangfire(options => options.UseSqlServerStorage(Configuration.GetConnectionString("MyProjectContext")));
            services.AddOptions();
            services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
            services.AddMvc().AddDataAnnotationsLocalization();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseHangfireDashboard();
            app.UseHangfireServer();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });
        }
    }
}

MyProjectContext.cs

using Microsoft.EntityFrameworkCore;

namespace MyProject.Models
{
    public class MyProjectContext : DbContext
    {
        public MyProjectContext(DbContextOptions<MyProjectContext> options) : base(options) { }

        public DbSet<Operator> Operators { get; set; }
    }

    public class Operator
    {
        public int Id { get; set; }
        [MaxLength(40)]
        public string Name { get; set; }
        public int Password { get; set; }
    }
}

我的类.cs

using MyProject.Models;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;

namespace MyProject
{
    public class MyClass
    {
        const string REGEX_OPERATORS = "^(?<Id>.{4})(?<Name>.{40})(?<Password>.{5})";
        private readonly Regex reOperators = new Regex(REGEX_OPERATORS, RegexOptions.Compiled);

        public void ImportOperatorList()
        {
            var path = @"F:\testdata.txt";
            string[] lines = File.ReadAllLines(path);

            foreach (var line in lines)
            {
                Match match = reOperators.Match(line);
                if (match.Success)
                {
                    string rawId = match.Groups["Id"].Value;
                    string rawName = match.Groups["Name"].Value;
                    string rawPassword = match.Groups["Password"].Value;

                    int Id;
                    try
                    {
                        Id = int.Parse(rawId, System.Globalization.NumberStyles.Integer);
                    }
                    catch (Exception)
                    {
                        throw;
                    }

                    string Name = rawName;

                    int Password;
                    try
                    {
                        Password = int.Parse(rawPassword, System.Globalization.NumberStyles.Integer);
                    }
                    catch (Exception)
                    {
                        throw;
                    }

                    using (var context = new MyProjectContext(/* ??? */))
                    {
                        var op = new Operator
                        {
                            Id = Id,
                            Name = Name,
                            Password = Password
                        };

                        context.Operators.Add(op);
                        Debug.WriteLine(context.SaveChanges());
                    }
                }
            }
        }
    }
}

当然不是完整的也不是可编译的,因为项目中还有很多其他文件(即使没有我自己的特定应用程序)。

最佳答案

建立在你自己的答案之上。

重构 MyClass 以依赖于抽象而不是与具体紧密耦合。

这里是重构后的MyClass

public class MyClass {
    const string REGEX_OPERATORS = "^(?<Id>.{4})(?<Name>.{40})(?<Password>.{5})";
    private readonly Regex reOperators = new Regex(REGEX_OPERATORS, RegexOptions.Compiled);
    private readonly IFileSystem File;
    private readonly IProjectContext context;

    public MyClass(IFileSystem File, IProjectContext context) {
        this.File = File;
        this.context = context;
    }

    public void ImportOperatorList() {
        var path = @"F:\testdata.txt";
        var lines = File.ReadAllLines(path);
        foreach (var line in lines) {
            var match = reOperators.Match(line);
            if (match.Success) {
                string rawId = match.Groups["Id"].Value;
                string rawName = match.Groups["Name"].Value;
                string rawPassword = match.Groups["Password"].Value;
                var op = new Operator {
                    Id = int.Parse(rawId, System.Globalization.NumberStyles.Integer),
                    Name = rawName,
                    Password = int.Parse(rawPassword, System.Globalization.NumberStyles.Integer)
                };
                context.Operators.Add(op);
            }
        }
        if (lines.Length > 0)
            Debug.WriteLine(context.SaveChanges());
    }
}

经过以下修改

public interface IFileSystem {
    string[] ReadAllLines(string path);
}

public class FileWrapper : IFileSystem {
    public string[] ReadAllLines(string path) {
        var lines = File.ReadAllLines(path);
        return lines;
    }
}

public interface IProjectContext : IDisposable {
    DbSet<Operator> Operators { get; set; }
    int SaveChanges();
    //...add other functionality that needs to be exposed as needed
    //eg: Database Database { get; }
    //...
}

public class MyProjectContext : DbContext, IProjectContext {
    public MyProjectContext(DbContextOptions<MyProjectContext> options) : base(options) { }

    public DbSet<Operator> Operators { get; set; }
}

您将确保所有抽象都在组合根的服务容器中注册。

public void ConfigureServices(IServiceCollection services) {
    services.AddDbContext<MyProjectContext>(options => options.UseSqlServer(Configuration.GetConnectionString("MyProjectContext")));
    services.AddHangfire(options => options.UseSqlServerStorage(Configuration.GetConnectionString("MyProjectContext")));
    services.AddOptions();
    services.Configure<MySettings>(options => Configuration.GetSection("MySettings").Bind(options));
    services.AddMvc().AddDataAnnotationsLocalization();

    //...adding additional services
    services.AddScoped<IProjectContext, MyProjectContext>();
    services.AddTransient<IFileSystem, FileWrapper>();
    services.AddTransient<MyClass, MyClass>();
}

现在,当使用作用域服务提供者时,您可以请求您的类,并且在解析 MyClass 时将注入(inject)所有依赖项

using (var scope = host.Services.CreateScope()) {
    var services = scope.ServiceProvider;
    var myClass = services.GetRequiredService<MyClass>();
    myClass.ImportOperatorList();
}

由于上面的范围,当容器超出范围时,容器将管理容器创建的任何服务的处置。

关于c# - 使用依赖注入(inject)访问 DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47440794/

相关文章:

c# - P/Invoke Interop Assistant - 是否像导入 dll 的 header 一样简单?

javascript - 在 AngularJS 的类方法中无法访问注入(inject)的依赖项

nginx - nginx 背后的 ASP.NET 5

c# - 是否可以在 ASP.NET Core 中更新 session ?

dependency-injection - OAuthAuthorizationServerProvider 实现中的 Autofac 依赖注入(inject)

ios - 数据访问器对象单例或其他模式? ( objective-c )

c# - 如何在 C# 中重新创建调用 TREND() 的 Excel 公式?

c# - 为什么我的模型在发布后丢失了下拉列表项?

c# - 使用反射获取baseclass属性值-与this.Property相反的base.Property

c# - ASP .NET Core 2.1-preview2 HttpClient 死锁