简体   繁体   English

如何解决Prism + DryIoC中的依赖性

[英]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. 我在我的Xamarin.Forms(4.0.0.497661)项目之一中使用Prism.DryIoc.Forms(7.1.0.431) 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. 我们有一个名为RestService的服务,它可以处理网络调用,并且还实现了另一个名为ProfileService的服务,在该服务中,我们可以获取用户信息以及与Profile的服务相关的其他内容。 I am thinking to resolve the RestService dependency in ProfileService to make the network calls. 我正在考虑解决ProfileService中的RestService依赖关系以进行网络调用。

I have registered both the services in App.xaml.cs under RegisterTypes() method. 我已经在App.xaml.cs RegisterTypes()方法下RegisterTypes()这两个服务。

If you want to access the underlying container used by Prism.Forms. 如果要访问Prism.Forms使用的基础容器。 In your case DryIoc, you can easily get inside RegisterTypes method. 就您的DryIoc而言,您可以轻松地进入RegisterTypes方法。

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. AppContainer是在App.xaml.cs类中声明的属性,如下所示。

Note:- GetContainer method is an extension method which is available in Prism.DryIoc namespace. 注意:-GetContainer方法是Prism.DryIoc命名空间中提供的扩展方法。 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. 现在,您可以使用如下所示的DryIoc容器。

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. 注意:-在您要在代码行上方调用的类中,导入DryIoc命名空间。

using DryIoc;

Happy Coding :) 快乐编码:)

Note:- The above approach is not recommended as this will make your class / code untestable. 注意:-不建议使用上述方法,因为这会使您的类/代码不可测试。

I am thinking to resolve the RestService dependency in ProfileService to make the network calls. 我正在考虑解决ProfileService中的RestService依赖关系以进行网络调用。

Never ever actively resolve anything anywhere, except once at the root of the application. 永远不要主动解决任何问题,除非在应用程序的根目录一次。 (*) (*)

What you want to do is to inject the dependency, ie you create a constructor parameter: 您要做的是注入依赖关系,即创建一个构造函数参数:

internal class ProfileService : IProfileService
{
    public ProfileService( IRestService restService )
    {
        // store restService for later, use it now... but have it injected, don't resolve it!
    }
}

(*) convention-over-registration stuff like the ViewModelLocator is a notable exception, but in fact, conceptionally, we inject a universal factory there more than resolving individual view models. (*)像ViewModelLocator这样的注册约定ViewModelLocator东西是一个明显的例外,但是实际上,从概念上讲,我们在这里注入通用工厂而不是解析单个视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM