michaelscodingspot / WPF_MVVMC

A WPF framework for navigation between pages with MVC-like pattern
MIT License
64 stars 18 forks source link

Dependency Injection for json config file #16

Closed papillon2k2 closed 1 week ago

papillon2k2 commented 1 year ago

I am trying to use an appsettings.json file in my app. I have implemented it the usual way with dependency injection, registering the IConfiguration object and MainWindow in startup.

Then in MainWindow code behind I am able to access configuration values , by injecting the IConfiguration object into MainWindow constructor, like this:

    public partial class MainWindow : Window {
        private readonly IConfiguration _config;
        public MainWindow(IConfiguration config) {
            InitializeComponent();
            _config = config;
        }
        public void someMethod() {
            //get config value
            string someConfigValue = _config.GetValue<string>("something");
        }
    }

But How could I inject it into each Step View, so I can access it from each step view code-behind? is this possible? I guess I would need to inject it first into the Controller constructor, but have not been able to make it work (apparently the package code always calls the empty constructor, not the one with the IConfiguration object injected...)

michaelscodingspot commented 1 year ago

You're right, MVVMC doesn't support dependency injection right now. What dependency injection framework are you using btw?

In any case, here's a suggestion of a relatively easy implementation usage:

What do you think?

michaelscodingspot commented 11 months ago

Closing due to lack of interest. Reopen if the need arises.