正确的使用方法数据库在Windows 8和Windows Phone 8 [英] Correct way to use databases in Windows 8 and Windows Phone 8

查看:163
本文介绍了正确的使用方法数据库在Windows 8和Windows Phone 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个Windows 8的应用程序,它需要存储一些表。目前,我使用的XML文件,的XDocument类来解决的目的。它采用保存使用负载方法 GetFileAsync CreateFileAsync 等。此外,还有保存负荷方法被称为不同的事件。但是,只要有反复调用,抛出一个异常告诉我,文件访问被拒绝。预期行为 - <一个href=\"http://stackoverflow.com/questions/13045504/i-get-system-unauthorizedaccessexception-while-loading-xml-from-disk/13048669#comment17742695_13048669\">more这里细节!虽然有肮脏的方法来避免这种情况(如使用锁等等),我不是很满意的结果。我宁愿preFER数据库。此外,我计划写的Windows Phone 8其他应用程序(也可能是网络版),将利用数据。

I am currently working on a Windows 8 app which needs to store some tables. Currently, I am using XML files with XDocument classes to solve the purpose. It employs save and load methods using GetFileAsync and CreateFileAsync etc. Moreover, there save and load methods are called by different events. However, whenever there are repeated calls, an exception is thrown telling me that file access is denied. Expected behavior - more details here! While there are dirty methods to avoid this (like using locks and such) I am not very happy with the results. I'd rather prefer databases. Moreover, I am planning to write another app for Windows Phone 8 (and possibly a web version) which will make use of the data.

他们已经多次说,Windows 8的是基于云计算。现在的问题是:什么是存储我的数据正确的做法? XML似乎是正确的,但我有上面提到的问题。什么是涉及到Windows 8中的Windows Phone 8,并可能Azure的云计算的理想解决方案的基础?我想要的是存储表,并让那些访问。

They have been repeatedly saying that Windows 8 is cloud based. Now the question: What is correct way to store my data? XML seems right but is has problems I mentioned above. What would be ideal cloud based solution involving Windows 8, Windows Phone 8 and possibly Azure? All I want is to store tables and make those accessible.

如果这个问题,似乎都不清楚对不起。如果需要,我会提供的信息。

Sorry if the question seems unclear. I will provide information if required.

推荐答案

如果你想使用天青,进行最简单的方法是的 Windows Azure的移动服务。它可以让你设置你的数据库和web服务在几​​分钟内使用Web界面。

If you want to use Azure, the easiest way to proceed is Windows Azure Mobile services. It allows you to setup your database and webservices using a web interface in a few minutes.

这是相当冷静,允许你添加自定义JavaScript到您的Web API的逻辑,并生成JSON网络的API。有适用于Windows 8,Windows Phone的和iOS客户端库。你可以很容易地推出自己的任何HTTP启用前端。

It's quite cool, allows you to add custom javascript to your web api logic, and generates json web apis. There are client Libraries for Windows 8, Windows Phone and iOS. You could easily roll your own for any http enabled frontends.

但要知道,走的是云计算的路线意味着你的应用程序不会脱机工作,(如果你没有code缓存系统,以及A缓存将需要一个本地数据库。)

However be aware that taking the cloud route means that your app won't work offline, (if you don't code a cache system that is. And a cache will requires a local DB.)

关于本地DB
你真的要准备:
1)一个真正的DB在你的应用程序,如SQLite的。它可以作为一个的NuGet包但现在ARM的支持不能开箱,也不由担保球队。如果你不需要的手臂,去试试吧:)

About the local DB You really have to possibilities: 1) A real DB in your app, like SQLite. It's available as a Nuget package but right now ARM support isn't available out of the box, nor guaranteed by the team. If you don't need arm, Go try it :)

2)普通的旧文件存储,就像你以前那样。我个人经常做我自己。来自不同线程访问它时(拒绝访问错误),你会得到然而问题。

2) plain old file storage, like you did before. I personally often do that myself. You will however get issues when accessing it from different threads (Access Denied errors).

当你存储的东西在本地文件,不要忘了(当您读取或写入文件,即),以prevent访问被拒绝的例外锁定关键部分。可以肯定,你Incapsulate读/写逻辑在服务类的实例您的应用程序中是唯一的。 (使用Singleton模式的实例,或任何等价物)。

When you store things in a local file, don't forget to lock the critical sections (ie when you read or write to the file) to prevent the access denied exceptions. To be sure, Incapsulate your write/read logic in a service class instance unique within your app. (Use the singleton pattern for instance, or anything equivalent).

锁本身,现在。我想象你正在使用异步等待。我喜欢这甜蜜的事情太多。但是,经典的C#锁(使用锁定关键字例如)不与异步工作等待。 (即使它的工作,阻断不会凉)。​​

The lock itself, now. I imagine that you are using async await. I like this sweet thing too. But classic C# locks (using the lock keyword for instance) don't work with async await. (And even if it worked, blocking wouldn't be cool).

这就是为什么奇妙AsyncLock进场。这是一个锁,但-approximately-不会阻止(你等待它)。

That's why the marvellous AsyncLock comes into play. It's a lock, but which -approximately- doesn't block (you await it).

public class AsyncLock
{
    private readonly AsyncSemaphore m_semaphore;
    private readonly Task<Releaser> m_releaser;

    public AsyncLock()
    {
        m_semaphore = new AsyncSemaphore(1);
        m_releaser = Task.FromResult(new Releaser(this));
    }

    public Task<Releaser> LockAsync()
    {
        var wait = m_semaphore.WaitAsync();
        return wait.IsCompleted ?
            m_releaser :
            wait.ContinueWith((_, state) => new Releaser((AsyncLock)state),
                this, CancellationToken.None,
                TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
    }

    public struct Releaser : IDisposable
    {
        private readonly AsyncLock m_toRelease;

        internal Releaser(AsyncLock toRelease) { m_toRelease = toRelease; }

        public void Dispose()
        {
            if (m_toRelease != null)
                m_toRelease.m_semaphore.Release();
        }
    }
}

public class AsyncSemaphore
{
    private readonly static Task s_completed = Task.FromResult(true);
    private readonly Queue<TaskCompletionSource<bool>> m_waiters = new Queue<TaskCompletionSource<bool>>();
    private int m_currentCount;

    public AsyncSemaphore(int initialCount)
    {
        if (initialCount < 0) throw new ArgumentOutOfRangeException("initialCount");
        m_currentCount = initialCount;

    }
    public Task WaitAsync()
    {
        lock (m_waiters)
        {
            if (m_currentCount > 0)
            {
                --m_currentCount;
                return s_completed;
            }
            else
            {
                var waiter = new TaskCompletionSource<bool>();
                m_waiters.Enqueue(waiter);
                return waiter.Task;
            }
        }

    }
    public void Release()
    {
        TaskCompletionSource<bool> toRelease = null;
        lock (m_waiters)
        {
            if (m_waiters.Count > 0)
                toRelease = m_waiters.Dequeue();
            else
                ++m_currentCount;
        }
        if (toRelease != null)
            toRelease.SetResult(true);

    }
}

您可以用这种方式(我假设你有一个名为blogLock的AsyncLock场(从我自己的项目之一)采取:

you can use it this way (I suppose that you have an AsyncLock field named blogLock (taken from one of my own projects):

            using (await blogLock.LockAsync())
            {
                using (var stream = await folder.OpenStreamForReadAsync(_blogFileName))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        var json = await reader.ReadToEndAsync();
                        var blog = await JsonConvert.DeserializeObjectAsync<Blog>(json);

                        return blog;
                    }
                }
            }

这篇关于正确的使用方法数据库在Windows 8和Windows Phone 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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