如何在WPF应用程序启动时依赖于用户选择设置依赖注入?

huangapple go评论67阅读模式
英文:

How to set the dependency injection when it depends of the selection of user at startup in a WPF application?

问题

在查看如何在WPF应用程序中使用依赖注入的示例时,我注意到它通常在app.xaml.cs文件中配置,并在显示任何窗口之前执行。但在我的情况下,某些依赖项依赖于用户在第一个窗口中的选择。

这是我的情况。我想创建一个应用程序,允许用户从两个不同的云中上传和下载文件。用户可以从下拉框中选择要使用的云。一旦选择,整个应用程序将使用所选的云。如果用户想使用另一种云,他必须关闭并重新运行应用程序(这有点傻,但这是为了简化,我认为这更好地表达了疑虑)。

由于用户需要选择云,我无法在app文件中配置依赖项。

我的代码如下:

interface ICloudService
{
    UploadFile(string pathFileToUpload);
    DownloadFile(string pathToSaveFile);
}

class CloudOneService : ICloudService
{
    //实现
}

class CloudTwoService : ICloudService
{
    //实现
}

在app.xaml.cs文件中,我应该配置依赖项,类似于以下方式:

public partial class App : Application
{
    public App()
    {
        host = new HostBuilder()
          .ConfigureServices((hostContext, services) =>
          {
              services.AddScoped<ICloudService, CloudOneService>();
          }).Build();
    }
}

但这段代码首先总是使用CloudOneService,其次,它在用户可以选择云之前运行。

因此,我不确定如何在依赖项选择依赖于用户选择时进行配置。

我该如何处理?

英文:

When I see some examples about how to use dependency injection in a WPF application, I have seen that this is configure in the app.xaml.cs file, that it is execute before any window is showed.

But in my case, some dependencies depends on the selection of the user in the first windows.

This is the case. I want to have an application that allow to upload and download files from two different clouds. The user selects from a dropbox which cloud he wants to use. Once it is selected, the whole application will use the selected cloud. If the user wants to use the other cloud, he has to close and run the application again (it is a bit silly behaviour, but it is to simplify and I think it expose the doubt better).

How the user need to select the cloud, I can't configure the dependency in the app file.

My code is this:

interface ICloudService
{
    UploadFile(string pathFileToUpload);
    DownloadFile(string pathToSaveFile);
}

class CloudOneService() : ICloudService
{
    //Implementation
}

class CloudTwoService() : ICloudService
{
    //Implementation
}

In the app.xaml.cs file, I should to configure the dependencies, something like that:

public partial class App : Application
{
    public App()
    {
        host = new HostBuilder()
          .ConfigureServices((hostContext, services) =&gt;
          {
              services.AddScoped&lt;ICloudService, CloudOneService&gt;();
 
          }).Build();
    }
}

But this code first it will use always CloudOneService and second, it is run before the user can select the cloud.

So I am not sure how could I configure the dependency injection when it depends on the selection of the user.

How could I do it?

Thanks.

答案1

得分: 1

So, as I see it, your application has two states:

  1. where the user did not yet select something and
  2. after the selection happened

Now, the question is: Do you need the interface to be available in state 1? If yes, then you should provide "something" there. If no, then you can easily resolve it when going into state 2, e.g. by using a factory class, like you suggested.

enum CloudServiceType
{
    One,
    Two
}

interface ICloudServiceFactory
{
    ICloudService GetService(CloudServiceType selectedCloud);
}

If you need to have an ICloudService ready before the selection, you could either inject a "default" one using regular DI or just let the factory provide a default one with a GetDefault() method.

英文:

So, as I see it, your application has two states:

  1. where the user did not yet select something and
  2. after the selection happened

Now, the question is: Do you need the interface to be available in state 1? If yes, then you should provide "something" there. If no, then you can easily resolve it when going into state 2, e.g. by using a factory class, like you suggested.

enum CloudServiceType
{
    One,
    Two
}

interface ICloudServiceFactory
{
    ICloudService GetService(CloudServiceType selectedCloud);
}

If you need to have an ICloudService ready before the selection, you could either inject a "default" one using regular DI or just let the factory provide a default one with a GetDefault() method.

huangapple
  • 本文由 发表于 2023年2月14日 01:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439128.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定