简体   繁体   English

UWP MVVM Template10:跨应用程序访问外部API的单个实例

[英]UWP MVVM Template10: Access single instance of external API across application

I've been tasked with taking over a partially developed sizeable and complex UWP application using MVVM via Template 10. The app needs to use an in-house developed webservices API and this needs to be used for practically every single function, starting with the initial login page. 我的任务是通过模板10使用MVVM接管部分开发的大型和复杂的UWP应用程序。该应用程序需要使用内部开发的Web服务API,这需要用于几乎所有单个功能,从初始开始登录页面。

So given I need to access a single instance of the API everywhere how do I go about doing that correctly? 因此,我需要在任何地方访问API的单个实例,我该如何正确地执行此操作? I've used MVVM a fair bit but never used Template10 and never had to share an instance of an object across an entire MVVM UWP app before. 我已经使用了MVVM但从未使用过Template10,并且之前从未在整个MVVM UWP应用程序中共享对象的实例。

So far I can think of three ways: 到目前为止,我可以想到三种方式:

  1. Declare and instantiate API instance in Appl.xaml.cs and use it globally 在Appl.xaml.cs中声明并实例化API实例并在全局范围内使用它
  2. Create a public Globals class and have the instance as a public static property: c# public class Globals { private static OurAPI _ourAPI; public static OurAPI API { get { return _ourAPI; } set { _ourAPI = value; } } } 创建一个公共Globals类并将该实例作为公共静态属性: c# public class Globals { private static OurAPI _ourAPI; public static OurAPI API { get { return _ourAPI; } set { _ourAPI = value; } } } c# public class Globals { private static OurAPI _ourAPI; public static OurAPI API { get { return _ourAPI; } set { _ourAPI = value; } } }

  3. Instantiate the API in the login page and then pass it as a parameter between ViewModels, presumably using the Navigation service. 在登录页面中实例化API,然后将其作为ViewModel之间的参数传递,可能是使用导航服务。

I'm thinking 1 or 2 are most likely not MVVM compliant and could cause unit testing issues so maybe 3 is the best option? 我认为1或2很可能不符合MVVM并且可能导致单元测试问题所以3可能是最好的选择吗? Or is there another, more correct way to do this to adhere to Template10/MVVM concepts and also be able to unit test it? 或者是否有另一种更正确的方法来遵守Template10 / MVVM概念并且能够对其进行单元测试?

EDIT: Sorry about the code not formatting, the edit box formats it Ok but when I save it it goes back to one long sentence :-( 编辑:抱歉代码没有格式化,编辑框格式化它确定但是当我保存它它回到一个长句:-(

The best solution consists of a singleton service and inversion of control (IoC) / Dependency injection . 最佳解决方案包括单件服务和控制反转(IoC) / 依赖注入 This is a pretty complex topic so I definitely encourage to read about it from several sources. 这是一个非常复杂的主题,所以我绝对鼓励从几个来源阅读它。

In summary, you first create an interface for your service, where you declare all the public members and methods. 总之,您首先要为您的服务创建一个接口,您可以在其中声明所有公共成员和方法。 Then you create an implementation of the interface. 然后,您创建一个接口的实现。 Then you use a IoC container and register your service as a singleton (single-instance) and then integrate the IoC so that it creates instances of your view models. 然后使用IoC容器并将服务注册为单例(单实例),然后集成IoC,以便创建视图模型的实例。 Then you can put the interface as a constructor parameter of your view model and the IoC container will make sure to provide the singleton instance you registered. 然后,您可以将接口作为视图模型的构造函数参数,IoC容器将确保提供您注册的单例实例。

In your case, you are using Template 10 which can be integrated with different IoC containers as seen in the documentation . 在您的情况下,您使用的是模板10,它可以与不同的IoC容器集成,如文档中所示 Check out AutoFac as an example of an IoC container. 查看AutoFac作为IoC容器的示例。 You can see some examples of registering and resolving a service in the documentation. 您可以在文档中看到一些注册和解析服务的示例。

For a general solution, check this SO question which demonstrates using AutoFac in UWP. 对于一般解决方案,请检查此SO问题 ,该问题演示了如何在UWP中使用AutoFac。

You can also see some code examples in this SO question and this one specifically for Template 10. 您还可以看到在一些代码示例这太问题这一个专门为模板10。

This solution is better than using static and global instances because you never actually deal with any hardcoded reference and actually always work just against the interface. 这个解决方案比使用static和全局实例更好,因为你从来没有真正处理任何硬编码的引用,实际上总是只对接口工作。 You put the interface as the parameter of your constructor and IoC will take care of providing the instance for you. 您将接口作为构造函数的参数,IoC将负责为您提供实例。 In addition - you can anytime swap the interface implementation for a different class and you just have to update it in one single location - the IoC registration. 此外 - 您可以随时将接口实现交换为其他类,您只需在一个位置更新它 - IoC注册。

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

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