Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve the dependencies in Prism + DryIoC

I am using Prism.DryIoc.Forms (7.1.0.431) in one of my Xamarin.Forms (4.0.0.497661) projects. I am facing issue while resolving the dependency in other service classes.

Use case:

We have a service called RestService which takes care of network calls and we have implemented one more service called ProfileService in which we get the user information and other stuff related to Profile's service. I am thinking to resolve the RestService dependency in ProfileService to make the network calls.

I have registered both the services in App.xaml.cs under RegisterTypes() method.

like image 252
vmakam Avatar asked Oct 28 '25 12:10

vmakam


1 Answers

If you want to access the underlying container used by Prism.Forms. In your case DryIoc, you can easily get inside RegisterTypes method.

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
   containerRegistry.RegisterForNavigation<NavigationPage>();
   AppContainer = containerRegistry.GetContainer(); //Assigning actual dryioc container
}

AppContainer is an property declared in App.xaml.cs class like below.

Note:- GetContainer method is an extension method which is available in Prism.DryIoc namespace. Import namespace

using Prism.DryIoc;

//Private and Public variables
public partial class App
{
    /// <summary>
    /// Actual Dry Ioc Container which can be called and used for manual registering and resolving dependencies.
    /// </summary>
    public static IContainer AppContainer { get; set; }
}

Now you can use DryIoc container like below.

var authService = App.AppContainer.Resolve<IAuthenticationService>();//you need to register IAuthenticationService inside RegisterType Method.

Note:- import DryIoc namespace in your class where you are calling above line of code.

using DryIoc;

Happy Coding :)

Note:- The above approach is not recommended as this will make your class / code untestable.

like image 83
Hamid Shaikh Avatar answered Oct 30 '25 01:10

Hamid Shaikh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!