【问题标题】:MVC design pattern for multiplatform lib多平台库的 MVC 设计模式
【发布时间】:2016-04-02 05:08:18
【问题描述】:

我目前在个人项目中遇到设计问题,我正在开发一个 Android 应用程序(使用 xamarin)和一个 C# wpf windows 应用程序。

桌面应用程序帮助用户设计一些练习,学生可以在安卓应用程序上练习。但目前,我希望有一个良好的代码结构,主要是为两个应用程序提供一个公共库。问题是,遵循 MVC 设计模式,我的模型中的所有内容都应该独立于视图。或者,在 Android 上,我需要跟踪锻炼的运行时间以及学生按下的按钮。或者这种信息在练习完成后没有保存,从windows app的角度来看是无用的信息。所以在这种情况下,我应该为我的模型编写一种包装器,以便我可以保存这些数据吗?这样做似乎违反了 MVC 模式,但我找不到解决方案。为了更好地说明我的问题,这里是我的模型:

public class File
{
    public string Name { get; set; } = "file";
    public List<Module> Modules { get; set; } = new List<Module>();
}

public class Module
{
    public string Title { get; set; } = string.Empty;
    public string Color { get; set; } = "0xFF0000";
    public List<Exercice> Exercices { get; set; } = new List<Exercice>();
}

public class Exercice
{
    public string Name { get; set; } = string.Empty;
    public List<string> Sounds { get; set; } = new List<string>();
    public List<Unit> Units { get; set; } = new List<Unit>();
    public bool Randomize { get; set; } = false;
}

public class Unit
{
    public string Text { get; set; } = string.Empty;
    public List<string> Sounds { get; set; } = new List<string>();
    public EType Type { get; set; }
    public bool Solution { get; set; } = true;
    public List<UnitChild> Units { get; set; } = new List<UnitChild>();
}

public class UnitChild
{
    public string Text { get; set; } = string.Empty;
    public List<string> Sounds { get; set; } = new List<string>();
    public EType Type { get; set; }
    public bool Solution { get; set; } = true;
}

public enum EType
{
    Touch,
    Display,
    Voice
}

所有内容都是从 YAML 文件加载和解析的,因为我需要将其真正结构化。

在每个此类中,我不想保留(在 Unit 和 UnitChild 类中)已按下哪个,因为它是多余的,可能需要保存在控制器中(我目前没有写,因为我想在实际编码之前弄清楚这一点)。基本上,所有字段都是练习的核心,是 Windows 应用程序所需的唯一数据。

感谢您的帮助!

我希望这不会看起来很乱

【问题讨论】:

  • 请再次解释公共库的动机。它的目的是什么?
  • 好吧,我想尽可能多地重用代码,所以公共库正在处理与打开练习文件、创建练习等相关的所有事情。所以我只能从两者中调用函数应用程序,并通过直接修改 lib 轻松更改行为。
  • 打开和管理练习是您应用的业务逻辑,如果我没记错的话。您认为这两个平台共有哪些功能?听起来您正在尝试将 UI 功能导出到公共库,但 UI 依赖于平台,根本不常见。

标签: c# design-patterns


【解决方案1】:

这是我自己想出来的,

同时阅读有关 MVC 设计模式的文档,以及有关 C# WPF 编程的更多具体文章。

如果您处于我的情况,您需要做的是保持模型干净并仅在两个应用程序中保留共享变量,并在控制器中保留应用程序特定变量,因为他是在 View 之间建立链接的人和模型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-05
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多