在可移植类库中使用 SQLite [英] using SQLite inside portable class library

查看:16
本文介绍了在可移植类库中使用 SQLite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我们开始着手一个新项目,其中包括 Windows 8 Metro、Windows Phone 和桌面应用程序的客户端.之所以决定使用 MVVM 模式作为主要架构,是因为在项目之间共享 ViewModel 对我们来说是更容易接受的解决方案.

recently we started to work on a new project which includes clients for Windows 8 Metro, Windows Phone and Desktop application. it was decided to use MVVM pattern as main Architecture because sharing ViewModels between projects is much more acceptable solution for us.

我们决定为此使用可移植类库,但问题是在从 Visualstudio 2012 扩展库下载并安装 Windows 运行时的 SQLite 后,当我们尝试添加对适当库的引用时,我们根本看不到这些库.这让我们想到,在 Portable 类库项目中使用 SQLite 是不可能的.

we decided to use portable class library for this purpose, but the problem is that after downloading and installing SQLite for windows runtime from Visualstudio 2012 extension gallery when we try to add reference to appropriate libraries we do not see those libraries at all. this makes us think, that it is not possible to use SQLite in Portable class library project.

也许你们中的一些人已经这样做了并且知道我们可以实现该功能的方式?请为我们提供正确的方法来开发这个任务,因为使用 SQLite 并且在开发的这个阶段拥有可重用的代码非常重要

Maybe some of u already done this and knows the way we could achieve that functionality? please provide us right way to develop this task as using SQLite and having reusable code is very important on this stage of the development

推荐答案

在 MvvmCross 中,我们通过不同的方法解决了这个问题.

In MvvmCross, we tackled this via a different approach.

我们想利用 SQLite 的本机端口,我们想使用来自 https://https://github.com/praeclarum/sqlite-net/的 SQLite-net ORM 包装器/github.com/praeclarum/sqlite-net/

We wanted to take advantage of the native ports of SQLite and we wanted to use the SQLite-net ORM wrapper from https://github.com/praeclarum/sqlite-net/

因此,我们所做的不仅仅是使用 PCL,而是:

So instead of using just a PCL, what we did was to:

为每个平台构建该插件 DLL 的扩展/实现

build an extension/realisation of that plugin DLL for each platform

使用通用的 DI 模式和库,以便 PCL 和非 PCL 数据库客户端都知道如何加载和实例化这些插件.

use a common DI pattern and library so that both PCL and non-PCL database clients know how to load and instantiate these plugins.

在代码级别,客户端应用程序可以使用以下插件:

At a code level, client apps can use the plugin like:

在业务逻辑库(PCL 或特定平台)中,代码可以定义模型对象:

In a business logic library (PCL or platform specific) the code can define a model object:

public class ListItem
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Name { get; set; }
    public string WhenCreated { get; set; }
}

在启动期间应用程序可以调用:

during startup the app can call:

  Cirrious.MvvmCross.Plugins.Sqlite.PluginLoader.Instance.EnsureLoaded();
  var factory = this.GetService<ISQLiteConnectionFactory>();
  var connection = factory.Create("SimpleList");
  connection.CreateTable<ListItem>();

然后在运行过程中,代码可以做这样的事情:

then during operation, the code can do things like:

  connection.Insert(new ListItem() { Name = TextToAdd, WhenCreated = DateTime.Now.ToString("HH:mm:ss ddd MMM yyyy") });

 public ListItem this[int index]
 {
     get { return _connection.Table<ListItem>().OrderBy(_sortOrder).Skip(index).FirstOrDefault(); }
 }

UI 特定代码必须引用插件的特定平台扩展并将该平台特定实现注入 IoC/DI 系统.在 Droid 上,这确实很简单(因为 MonoDroid 在运行时支持 Assembly.Load),但在其他平台上,这涉及一些样板"代码,例如:

While the UI specific code has to reference the platform-specific extension of the plugin and to inject that platform specific implementation into the IoC/DI system. On Droid this really is simple (because MonoDroid supports Assembly.Load at runtime), but on other platforms, this involves a little bit of 'boiler-plate' code like:

    protected override void AddPluginsLoaders(Cirrious.MvvmCross.Platform.MvxLoaderPluginRegistry loaders)
    {
        loaders.AddConventionalPlugin<Cirrious.MvvmCross.Plugins.Sqlite.WinRT.Plugin>();
        base.AddPluginsLoaders(loaders);
    }

<小时>

注意事项:


Notes:

  • 当前的 MvvmCross 存储库仅包含 WinRT 和 MonoDroid SQLite 包装器 - 但其他(WP* 和 MonoTouch)应该很容易构建(我知道其他人已经构建了它们,但尚未将它们贡献回来)

  • the current MvvmCross repo only includes the WinRT and MonoDroid SQLite wrappers - but others (WP* and MonoTouch) should be easy to build (and I know others have built them, but not yet contributed them back)

当前的 MvvmCross 存储库仅包含 WinRT 的同步(而非异步)接口 - 但我再次知道人们告诉我他们已经在他们的私人项目中扩展了这一点.

the current MvvmCross repo only includes the sync (not async) interfaces for WinRT - but again I know people have told me that they have extended this in their private projects.

我目前正在将这个插件结构拉到 MvvmCross 之外,以便可以更广泛地使用这些插件.希望能在圣诞节前就此发布公告.

I'm currently in the process of pulling this plugin structure outside of MvvmCross so that the plugins can be used more widely. Hopefully expect an announcement on this before Xmas.

有关 MvvmCross 中插件的更多信息,请参阅 https://speakerdeck.com/crillous/mvvmcross-going-portable

For more on plugins in MvvmCross see https://speakerdeck.com/cirrious/mvvmcross-going-portable

这篇关于在可移植类库中使用 SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆