简体   繁体   中英

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. I structured my solution with three different projects: View , ViewModel and Model .

My Model contains the business logic and in it I fetch invoice data from different sources. Now I need to exclude invoices with certain invoice numbers. I decided to define these numbers inside the appsettings.json file. 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. 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 .

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. I believe this would violate the MVVM pattern - at least how I understand it.

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). Dependency to Model is justified.

You don't break MVVM as long as you don't have code in Views which performs operation directly with Model, skipping View Models. 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. That way I don't need to add a dependency from the Model to the View.

My App class looks like this now:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM