Lewis Cianci
Lewis Cianci

Reputation: 1065

How do I get dependency injection working on Prism for Xamarin?

I have an app that I am porting over to Prism (from XamVvm). I'm using DryIoC.

My interface looks like this

[assembly: Dependency(typeof(IJoinCongregation))]
namespace MapManPrism.Services
{
public interface IJoinCongregation
{
    Task<bool> JoinCongregation(int Identifier, int PIN);
    Task<bool> CheckForMapUpdates();
    Task<Models.API.Publisher> PostPublisher(string name, string phone, int congregationid);

}

The class that implements this interface looks like this

public class JoinCongregation : IJoinCongregation
{
    private readonly string JoinCongregationAction = $"{EnvironmentConfiguration.WebEndPoint}Congregations/AuthoriseCongregation";
    private readonly string RegisterPublisherAction = $"{EnvironmentConfiguration.WebEndPoint}Publisher/EnrolPublisher";


    private readonly IFileStorage _fileStorage;
    private readonly IDatabaseService _database;


    public JoinCongregation(IFileStorage fileStorage, IDatabaseService database)
    {
        //var container = new Container();
        //container.resolve
        _fileStorage = fileStorage;
        _database = database;
    }

However, when my constructor code runs in my page viewmodel (which is called WelcomeWizardPageViewModel), it can't resolve the concrete implementation of IJoinCongregation. This makes DryIoC throw an error with timeout exceeded.

The constructor code looks like this...

public WelcomeWizardPageViewModel(IJoinCongregation congregation)
    {
        //var container = new Container();
        //new DatabaseService(); // runs constructor code which we need
        _congregation = congregation;
 ...

What am I doing wrong? These are my guesses.

  1. I'm registering the services incorrectly
  2. I'm pulling these services into other services in the wrong way
  3. By using [assembly: Dependency(typeof(IJoinCongregation))] in my services I am somehow mixing Xamarin's DI and DryIoC's DI (I tried taking this out to no effect)

Any help would be so much appreciated. Thank you.

EDIT: These are how the services get registered in the first place:

 protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.RegisterForNavigation<NavigationPage>();
            containerRegistry.RegisterForNavigation<MainPage>();
            containerRegistry.RegisterForNavigation<WelcomeWizardPage>();
             containerRegistry.RegisterForNavigation<SettingsPage>();
            //containerRegistry.RegisterForNavigation<WelcomeWizard>();
            //containerRegistry.register
            //containerRegistry.RegisterType<IFileStorage, FileStorage>();
            containerRegistry.Register<ICartService, CartService>();
            containerRegistry.Register<IDatabaseService, DatabaseService>();
            containerRegistry.Register<IJoinCongregation, JoinCongregation>();
            containerRegistry.Register<IMapService, MapService>();



        }

Upvotes: 0

Views: 925

Answers (1)

Lewis Cianci
Lewis Cianci

Reputation: 1065

In the end, it was that I had simply forgotten to register a dependent service. This is how I fixed it.

Work out what dependencies the Page View Model had, and then set them to null in the page view model to verify that the page view model is working, and the auto view model resolution. This was working so it had to be something else.

Take both services out of the constructor for the Page View Model. This also worked.

Add one service back into the page view model. This still worked.

Add second service to page view model, the wheels came off it and it broke with the DryIoC error.

Navigated to the service that was making it break and checked its dependencies.

One of its dependencies was not registered with DryIoC. Added it and the thing came to life sigh.

Upvotes: 2

Related Questions