简体   繁体   中英

Xamarin PCL library and Sqlite data storage

I'm developing a cross platform application with Xamarin for Android, iOS and Windows Phone. I'm creating a PCL library that contains all my business logic including calls to my 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.

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.

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? 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.

How do I abstract my data layer in a PCL library with Xamarin for Android, iOS and Windows Phone?

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

I recommend using SQLite.Net.Async . You can then use DI to inject the platform implementation ( ISQLitePlatform ) into PCL side. Another option is to use SQLite-Net PCL if you don't want to use DI.

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. At startup of the application the correct platform specific object is initiated and then passed into the PCL compatible library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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