0

我发现了这个使用 MVVM 和便携式库的好例子,发现它非常有用,我想开始在我的项目和解决方案中使用它。我开始通过这个例子移植我当前的 Windows Phone 应用程序,它会满足 MVVM 模式。我只是不确定如何抽象像 SQLCE 这样的本地数据库。

我的非 MVVM 解决方案是这样的:

数据上下文

public class ChallengeDataContext : DataContext
{
    public const string ConnectionString = "isostore:/challenges.sdf";

    public ChallengeDataContext(string connectionString)
        : base(connectionString)
    {
        this.Challenges = this.GetTable<Challenge>();
    }

    public Table<Challenge> Challenges { get; set; }
}

存储库

public class ChallengeRepository
{
    public static void Initialize()
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            if (dc.DatabaseExists() == false)
            {
                dc.CreateDatabase();
                dc.SubmitChanges();
            }
        }
    }

    public static void SeedSampleData()
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            if (dc.DatabaseExists() != false && dc.Challenges.Count() == 0)
            {
                var challenge = new Challenge("Testing", 3, ChallengeKind.Writting, false);
                dc.Challenges.InsertOnSubmit(challenge1);
                dc.SubmitChanges();
            }
        }
    }

    public Challenge GetChallengeRandomly(int difficulty, ChallengeKind kind)
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            var challenge = (from ch in dc.Challenges
                             where ch.Difficulty == difficulty && ch.Kind == kind && ch.Shown == false
                             select ch).FirstOrDefault();
            return challenge;
        }

    }

    public void SaveChallenge(Challenge challenge)
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            dc.Challenges.InsertOnSubmit(challenge);
            dc.SubmitChanges();
        }
    }

    public void UpdateChallengeShownAttribute(int id, bool shown)
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            var challenge = (from ch in dc.Challenges
                             where ch.Id == id
                             select ch).FirstOrDefault();

            if (challenge != null)
            {
                challenge.Shown = shown;
                dc.SubmitChanges();
            }
        }
    }

    public void UpdateChallengeShownAttribute(int difficulty, ChallengeKind kind, bool shown)
    {
        using (ChallengeDataContext dc = new ChallengeDataContext(ChallengeDataContext.ConnectionString))
        {
            var challenges = from ch in dc.Challenges
                             where ch.Kind == kind && ch.Difficulty == difficulty
                             select ch;

            foreach (var challenge in challenges)
            {
                challenge.Shown = shown;
            }
            dc.SubmitChanges();
        }
    }
}

挑战班

[Table(Name = "Challenges")]
public class Challenge
{
    [Column(IsPrimaryKey = true,IsDbGenerated=true)]
    public int Id { get; set; }
    [Column]
    public string Name { get; set; }
    [Column]
    public int Difficulty { get; set; }
    [Column]
    public ChallengeKind Kind { get; set; }
    [Column]
    public bool Shown { get; set; }

    public Challenge()
    {
    }

    public Challenge(string name, int difficulty, ChallengeKind kind, bool shown)
    {
        Name = name;
        Difficulty = difficulty;
        Kind = kind;
        Shown = shown;
    }
}
public enum ChallengeKind
{
    Writting,
    Telling,
    Pantomime
}

我知道基础知识,创建接口没问题,但我不确定如何更改可在所有项目中使用的模型类(我可以将属性更改为 DataContract 和 DataMember,但如何将其与 SQLCE 一起使用?)。那么我必须在哪个项目中实现存储库?代码在所有平台上都是相同的,但我认为 Windows 8 不使用 SQLCE。那么这个怎么算呢?感谢帮助

4

0 回答 0