简体   繁体   English

Xamarin PCL库和Sqlite数据存储

[英]Xamarin PCL library and Sqlite data storage

I'm developing a cross platform application with Xamarin for Android, iOS and Windows Phone. 我正在使用Xamarin开发适用于Android,iOS和Windows Phone的跨平台应用程序。 I'm creating a PCL library that contains all my business logic including calls to my REST API. 我正在创建一个PCL库,其中包含我的所有业务逻辑,包括对REST API的调用。 For now this seem to work fine, however, I need to store bits of information in a persistent space, such as API authentication keys etc, and I figured I might as well use Sqlite for this, it seems to be supported on all three platforms. 就目前而言,这似乎可以正常工作,但是,我需要在持久空间中存储一些信息,例如API身份验证密钥等,并且我认为我也可以为此使用Sqlite,这似乎在所有三个平台上都受支持。

The problem I'm running into is that System.Data and Mono.Data.Sqlite don't seem to be available within the context of a PCL project. Mono.Data.Sqlite的问题是System.DataMono.Data.Sqlite在PCL项目的上下文中似乎不可用。

Even if my PCL library can't, or doesn't, link with the required assemblies, I should at least be able to use the references (through using ) in my PCL library, right? 即使我的PCL库不能或不能链接到所需的程序集,我至少也应该能够使用PCL库中的引用(通过using ),对吗? I mean, I should be able to have the application be responsible for creating a database connection and then use dependency injection to inject that connection into my library. 我的意思是,我应该能够让应用程序负责创建数据库连接,然后使用依赖项注入将该连接注入到我的库中。 But seems I can't even make references in my code to any the System.Data and Mono.Data.Sqlite at all. 但是似乎我什至根本无法在我的代码中引用任何System.DataMono.Data.Sqlite

How do I abstract my data layer in a PCL library with Xamarin for Android, iOS and Windows Phone? 如何使用Xamarin for Android,iOS和Windows Phone在PCL库中抽象数据层?

[1] http://developer.xamarin.com/guides/ios/advanced_topics/system.data/ [1] http://developer.xamarin.com/guides/ios/advanced_topics/system.data/

I recommend using SQLite.Net.Async . 我建议使用SQLite.Net.Async You can then use DI to inject the platform implementation ( ISQLitePlatform ) into PCL side. 然后,您可以使用DI将平台实现( ISQLitePlatform )注入PCL端。 Another option is to use SQLite-Net PCL if you don't want to use DI. 如果您不想使用DI,则另一个选择是使用SQLite-Net PCL

I would be looking at using Inversion of Control technique so I have an interface or abstract class that is PCL compatible then a platform specific implementation of this class. 我将使用控制反转技术,因此我有一个与PCL兼容的接口或抽象类,然后是该类的特定于平台的实现。 At startup of the application the correct platform specific object is initiated and then passed into the PCL compatible library. 在应用程序启动时,将启动正确的平台特定对象,然后将其传递到PCL兼容库中。

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

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