简体   繁体   English

在 WPF .NET 核心应用程序的不同项目中访问 appsettings.json 值

[英]Accessing appsettings.json values in different project in WPF .NET Core app

I am new to .NET core and currently playing around with a WPF .NET Core app.我是 .NET 核心的新手,目前正在使用 WPF .NET 核心应用程序。 I structured my solution with three different projects: View , ViewModel and Model .我用三个不同的项目构建了我的解决方案: ViewViewModelModel

My Model contains the business logic and in it I fetch invoice data from different sources.我的Model包含业务逻辑,并在其中从不同来源获取发票数据。 Now I need to exclude invoices with certain invoice numbers.现在我需要排除具有某些发票编号的发票。 I decided to define these numbers inside the appsettings.json file.我决定在 appsettings.json 文件中定义这些数字。 After some research I learned I must initialize the IConfigurationBuilder instance inside the OnStartup() method in the App.xaml.cs file in the View project.经过一番研究,我了解到我必须在View项目的App.xaml.cs文件中的OnStartup()方法中初始化 IConfigurationBuilder 实例。 I read the config classes are supposed to be defined in the target project (in my case the Model project) and used in the OnStartup() method of the View project, so Dependency Injection can be used to pass the config class to the target project, like described in this link .读到配置类应该在目标项目中定义(在我的例子中是Model项目)并在View项目的OnStartup()方法中使用,因此可以使用依赖注入将配置 class 传递给目标项目,就像这个链接中描述的那样。

However, for this method to work, I must add a dependency to my Model project in my View project, so the config class can be accessed there.但是,要使此方法起作用,我必须在我的View项目中向我的Model项目添加一个依赖项,以便可以在那里访问配置 class。 I believe this would violate the MVVM pattern - at least how I understand it.我相信这会违反 MVVM 模式——至少我是这么理解的。

How would you tackle this problem?你将如何解决这个问题? What are the best practices here?这里有哪些最佳实践? Or am I doing things completely wrong?还是我做的事情完全错了? I am also open for suggestions to use a completely different approach.我也愿意建议使用完全不同的方法。

You have to add a dependency to Model project in View project, because it also serves as composition root for project (contains OnStartup() method).您必须在View项目中添加对Model项目的依赖项,因为它还用作项目的组合根(包含OnStartup()方法)。 Dependency to Model is justified.Model的依赖是合理的。

You don't break MVVM as long as you don't have code in Views which performs operation directly with Model, skipping View Models.只要视图中没有直接使用 Model 执行操作的代码,跳过视图模型,就不会破坏 MVVM。 Views can be aware of Models (not vice versa), and it can be handy in some situations (eg reference some enum from Model project in xaml style trigger via {x:Static model:MyEnum.MyValue} ) Views can be aware of Models (not vice versa), and it can be handy in some situations (eg reference some enum from Model project in xaml style trigger via {x:Static model:MyEnum.MyValue} )

I decided to just pass the IConfiguration instance to the Model project and handle things from there so that the Configuration setup code (like configurationBuilder.Configure<MySettingsClas>() ) is no more in the View project but in th Model project.我决定将 IConfiguration 实例传递给Model项目并从那里处理事情,以便配置设置代码(如configurationBuilder.Configure<MySettingsClas>() )不再在View项目中,而是在Model项目中。 That way I don't need to add a dependency from the Model to the View.这样我就不需要将 Model 的依赖项添加到视图中。

My App class looks like this now:我的App class 现在看起来像这样:

public partial class App : Application
{
    public IConfiguration Configuration { get; private set; }

    protected override void OnStartup(StartupEventArgs e)
    {
        IConfigurationBuilder builder = new ConfigurationBuilder()
                                        .SetBasePath(Directory.GetCurrentDirectory())
                                        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

        Configuration = builder.Build();

        ServiceCollection serviceCollection = new ServiceCollection();
        ConfigureServices(serviceCollection);

        IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

        MainWindow mainWindow = new MainWindow(serviceProvider.GetRequiredService<IConfiguration>());
        mainWindow.Show();
    }

    private void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton(Configuration);
    }
}

Not sure if this is the right way to do it though.不确定这是否是正确的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker 中 .NET Core 应用程序的 appSettings.json? - appSettings.json for .NET Core app in Docker? 无法在 WPF 项目 .net core 3.0 中添加 appsettings.json - Cannot add appsettings.json inside WPF project .net core 3.0 ASP.NET Core 应用程序不会从输出目录中读取 appsettings.json,而是从项目一中读取 - ASP.NET Core app does not read appsettings.json from output directory, but from the project one 从 .net core 3.1 中的 appsettings.json 获取值 - Get values from appsettings.json in .net core 3.1 如何从 .NET 核心中的 appsettings.json 读取部分值 - How to read section values from appsettings.json in .NET Core 在服务 .net core 中获取 appsettings.json 值 - Get appsettings.json values in service .net core .NET Core 2和DI - 在构造函数中使用appsettings.json中的值? - .NET Core 2 & DI - use values from appsettings.json in the constructor? .NET Core获取配置appsettings.json值 - .NET Core getting Configuration appsettings.json values ASP.NET 核心 Web App appsettings.json 值替换为 Z05B6053C41A2130AFDZ6FC3B158B44B1 容器中的随机字符串 - ASP.NET Core Web App appsettings.json values replaced by random strings in docker container appsettings.json 中的哨兵配置与 .Net Core 3 控制台应用程序中的 Serilog - Sentry configuration in appsettings.json with Serilog in .Net Core 3 Console App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM