Search code examples
c#xamarin.formsprismdryioc

Equivalent of this Xamarin MVVMLight using SimpleIoc container to Xamarin Prism DryIoc


Below are code from Xamarin MVVMLight using SimpleIoc container and now i am porting it to Xamarin Prism using DryIoccontainer :

this.container.Register<IAsciiCommander>(() =>  {
      var commander = new AsciiCommander();
      commander.AddSynchronousResponder();
      commander.AddResponder(
          this.container.GetInstance<TranspondersMonitor>());
      commander.AddResponder(
          this.container.GetInstance<BarcodeMonitor>()); 
      return commander;
  });

The following is some in ViewModelLocator and I need to do this in App.xaml.cs

 this.container.Register<InventoryConfiguration>(true);
 this.container.Register<IInventoryConfigurator>(
     () => new InventoryConfigurator(
              this.container.GetInstance<IAsciiCommander>(),
              this.container.GetInstance<InventoryConfiguration>())
 );// This is done in ViewModelLocator

Solution

  • Not sure about Prism, but the respected registrations in DryIoc would be:

    this.container.RegisterDelegate<IAsciiCommander>(r => { 
        var commander = new AsciiCommander();
        commander.AddSynchronousResponder();
        commander.AddResponder(r.Resolve<TranspondersMonitor>());
        commander.AddResponder(r.Resolve<BarcodeMonitor>());
        return commander; });
    

    2nd snippet:

    this.container.Register<InventoryConfiguration>();
    this.container.RegisterDelegate<IInventoryConfigurator>(r => 
        new InventoryConfigurator(
            r.Resolve<IAsciiCommander>(), 
            r.Resolve<InventoryConfiguration>())
     );// This is done in ViewModelLocator
    

    Regarding true parameter there is no such in DryIoc. You may immediately Resolve the service after registration, but I think that would make sense only if service is singleton (Reuse.Singleton parameter in DryIoc).

    By immediately resolving I mean just call a Resolve ignoring the result, so that singleton will be instantiated and stored in container singletons scope.