便携式类库里面使用SQLite [英] using SQLite inside portable class library

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

问题描述

最近,我们开始一个新项目,其中包括为Windows 8地铁,Windows手机和桌面应用程序客户端。它决定使用MVVM模式为主要架构,因为项目之间共享的ViewModels更易被人接受了我们的解决方案。

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.

我们决定使用便携式类库用于此目的,但问题是,下载并安装的SQLite从VisualStudio的2012扩展程序库的窗口运行后,当我们尝试添加引用适当的库,我们没有看到这些库的。这让我们觉得,这是不可能的便携式类库项目中使用的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.

也许有些U已经做到了这一点,知道我们能实现这一功能的方法?请向我们提供开发这一任务,使用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的本地端口的优势,我们要使用来自SQLite的净ORM包装< A HREF =htt​​ps://github.com/praeclarum/sqlite-net/> https://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.

  • e.g. you can see some of these in the SimpleDroidSql sample in https://github.com/slodge/MvvmCross/tree/vnext/Sample%20-%20SimpleDialogBinding

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

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/cirrious/mvvmcross-going-portable

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

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

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