[英]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
.我用三个不同的项目构建了我的解决方案:
View
、 ViewModel
和Model
。
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.