Search code examples
xamarinprismdryioc

DryIoC register configuration


I am working on a Xamarin project with Prism and DryIoC.

Currently I am setting up some custom environment-specific configuration, however I am struggling with the IoC syntax for this.

I have the following code as part of my App.xaml.cs:

private void SetConfiguration(IContainerRegistry containerRegistry)
        {
            // Get and deserialize config.json file from Configuration folder.
            var embeddedResourceStream = Assembly.GetAssembly(typeof(IConfiguration)).GetManifestResourceStream("MyVismaMobile.Configurations.Configuration.config.json");
            if (embeddedResourceStream == null)
                return;

            using (var streamReader = new StreamReader(embeddedResourceStream))
            {
                var jsonString = streamReader.ReadToEnd();
                var configuration = JsonConvert.DeserializeObject<Configuration.Configuration>(jsonString);

                What to do with configuration, in order to DI it?
            }

What should I do with the configuration variable to inject it? I have tried the following:

containerRegistry.RegisterSingleton<IConfiguration, Configuration>(c => configuration);
containerRegistry.Register<IConfiguration, Configuration>(c => configuration));

But the syntax is wrong with dryIoC.


Solution

  • RegisterSingleton and Register are meant for registering types where the container will then create the instances. You have your instance already, so you use

    containerRegistry.RegisterInstance<IConfiguration>( configuration );
    

    Instances are always singleton, obviously, so there's only no separate RegisterInstanceSingleton...