MAUI Dependency Injection

m_kojima 1 Reputation point
2022-11-13T08:20:54.943+00:00
  1. We have two type communication class public class SocketService : ICommunicationService
    public class SerialPortService : ICommunicationService
    1. Regiter DI at stating application according to standard way as follows. public static class MauiProgram
      {
      public static MauiApp CreateMauiApp()
      {
      var builder = MauiApp.CreateBuilder();
      builder
      .UseMauiApp<App>()
      .ConfigureFonts(fonts =>
      {
      fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
      fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
      });
      ...
      builder.Services.AddSingleton<ICommunicationService, SocketService>();
      builder.Services.AddSingleton<ICommunicationService, SerialPortService>();
      ...
      return builder.Build();
      }
      }
    2. Use communication class as follows. public class CommunicationViewModel
      {
      private ICommunication _service; public CommunicationViewModel(ICommunicationService service)
      {
      _service = service;
      }
      }
    3. In this pattern CommunicationViewModel works using serialport.
      Becase SocketService was overwrote SerialPortService class,
      and ICommunication interface tied SerialPortService class.

<Question>
If user select socket interface, after coneccting socket ,
I would like to use CommunicationViewModel using socket.

If user select serial port interface, after connecting COM port,
I would like to use CommunicationViewModel using serialport.

Is there the way to implement the style as follows ?
public CommunicationViewModel(ICommunicationService service)

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,842 questions
{count} votes

1 answer

Sort by: Most helpful
  1. m_kojima 1 Reputation point
    2022-11-20T03:39:30.773+00:00

    LeonLu-MSFT san, thank you for your comments.

    I understood I could not register same interface for two services.
    I found other solution. My inpletations are as follows.

    1. separate two services as follows. public interface ISocketService : ICommunicationService
      {
      public ReactivePropertySlim<string> IPAddress { get; set; }
      public ReactivePropertySlim<string> Port { get; set; }
      } public interface ISerialPortService: ICommunicationService
      {
      public ReactivePropertySlim<string> PortName { get; set; }
      public ReactivePropertySlim<string> BaudRate { get; set; }
      }
    2. change DI registration in CreateMauiApp() ....
      builder.Services.AddSingleton<ISocketService, MySocketService>();
      builder.Services.AddSingleton<ISerialPortService, MySerialPortService>();
      ....
    3. change communication class after userselection as follows. public class CommunicationViewModel
      {
      private ICommunication _service; public CommunicationViewModel(ISocketService socket, ISerialPortService serialport)
      {
      if (socket.IsConnected.Value) _service = socket;
      if (serialport.IsConnected.Value) _service = serialport; ...
      }
      }

    Thank you.