簡體   English   中英

使用依賴注入訪問DbContext

[英]Access to DbContext with dependency injection

關於依賴注入的段落,我不明白官方文檔

他們說我可以使用一個控制器(但從這里我知道我不需要它因為我正在使用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類添加/獲取數據。 在這里我找到了一個如何做到的例子:

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);
}

但是如果我要使用依賴注入,我不能使用沒有參數DbContext的構造函數。 另一方面,如果我添加參數,我必須在調用構造函數時傳遞正確的值,如上例所示 - 這是最初的問題。

EDIT2

我將發布一個“完整”的例子。 我很難理解,但無論如何我都在努力:

Program.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();

    }
}

startup.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; }
    }
}

MyClass.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時注入所有依賴項

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

由於上面是作用域,容器將管理容器超出范圍時創建的任何服務的處置。

您必須手動將context參數傳遞給函數,依賴注入不會為您執行此操作。 因此,在program.cs中你可以添加:

using (var scope = host.Services.CreateScope())
{
    var services = scope.ServiceProvider;
    var context = services.GetRequiredService<MyProjectContext>();

    // pass context to relevant Classes/Functions, i.e.
    MyClass myClass = new MyClass();
    myClass.ImportOperatorList(context);
}

在MyClass.cs中,您現在可以直接使用該變量:

public void ImportOperatorList(MyProjectContext context)
{
    // ...
    context.Operators.Add(op);
    context.SaveChanges();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM