c# - 我最终采用了什么设计模式?

标签 c# user-interface silverlight design-patterns xamarin.forms

我们有一个 Silverlight 应用程序。它有几个页面位于我们用户界面的选项卡内。过去,我们将它们称为 SavePage 和 PanelPage。保存页面仅具有编辑记录详细信息、创建新记录以及删除屏幕上现有记录的基本功能。 PanelPage 继承自 SavePage。 PanelPage 稍微复杂一些,面板根据您在屏幕上所做的选择而变得可见/不可见。

Silverlight 应用程序中的代码非常困惑。但是,最近,我采取了将此代码移植到 Xamarin Forms 中的步骤。我为此进行了两次失败的尝试,在第三次尝试中,我获得了可在所有目标平台上运行的代码:Silverlight、iOS、Android 和 Windows UWP。我对目前的类(class)设计相当满意。它可以更简单,但这将在一段时间内发挥作用。

这种设计的要点是 UI 逻辑是从物理 UI 控件本身中抽象出来的。我从跨两个平台(SavePage 和 PanelPage)的共享代码中删除了 System.Windows 使用。这些页面更像是 MVC 或 MVVC 模式中的“ Controller ”。但是,我不认为我所创建的完全是这两种模式中的任何一种。但是,我必须将这些类分为两部分:一部分用于抽象 UI 调用(如 SaveAsync()),另一部分用于特定于平台的 UI 调用(如 ReportError)。我正在努力正确命名类,因为我什至不知道我正在使用什么设计模式。

这是一个类图: Class Diagram 以下是一些相关接口(interface)的代码:

public interface IPage : IRecordSelector
{
    /// <summary>
    /// This event should be raised when the busy state of a tab changes
    /// </summary>
    event EventHandler<BusyStateChangedEventArgs> BusyStateChanged;
    object PageElement { get; }
}

public interface IButtonDrivenPage : IPage
{
    Task SaveClickAsync();
    Task DuplicateClickAsync();
    Task DeleteClickAsync();
    Task NewClickAsync();
    event EventHandler<ButtonVisibilityChangedEventArgs> ButtonVisibilityChanged;
    IRecord GetRecord();
}

public interface ISavePage : IButtonDrivenPage, IRequestClose
{
    string DataContextXmlSnapshot { get; }
    bool PromptForChangeCancel { get; }
    IRecord SelectedItem { get; }
    Task SetSelectedItemAsync(IRecord selectedItem);
    event EventHandler SelectedItemChanged;
    void Close();
    void SetAutomationObject(object automationObject);
    ISavePageUIController SavePageUIController { get; }
}

public interface ISavePageUIController: IDisposable
{
    /// <summary>
    /// The UI controller is notifying the page that the UI content has been loaded
    /// </summary>
    event EventHandler ContentLoaded;

    /// <summary>
    /// Prompt the user for a yet or a no
    /// </summary>
    Task<bool> GetYesNoFromPrompt(string message, string title);

    /// <summary>
    /// Report an error to the user
    /// </summary>
    void ReportError(string title, string message, Exception exception);

    /// <summary>
    /// Notifies the UI that the DataContext/Binding context has changed
    /// </summary>
    void SetSelectedItem(IRecord selectedItem);

    /// <summary>
    /// The actual UI object that is displayed on screen as the content of the page
    /// </summary>
    object PageElement { get;  }

    /// <summary>
    /// Clears residual errors from the screen if they exist
    /// </summary>
    void ClearErrors();

    /// <summary>
    /// The record was saved. The selectedItem parameter will be the saved record from the server.
    /// </summary>
    void CurrentRecordSaved(IRecord selectedItem);

    /// <summary>
    /// This event occurs when the UI wants to notify the controller that a Save button has been clicked in the UI somewhere
    /// </summary>
    event EventHandler SaveClicked;
}


public interface IPanelUIController : ISavePageUIController
{
    void CreateAndAddPanelFromContent(PagePanel pagePanel, double? panelHeight);
    IEnumerable<IPagePanel> GetIPagePanelControls();
    void SetHeader(IPanelHeader pageHeader);
    void SetVisiblePanels(IList<bool> visiblePanels);
    void HideAllPanels();
    event EventHandler<RecordsSelectedRoutedEventArgs> PanelPageRecordsSelected;
}

这些接口(interface)已在 Silverlight 和 Xamarin Forms 中成功实现。那么,这是否类似于另一种 UI 设计模式?有人可以建议改进吗?或者,告诉我需要做什么才能将其转换为更标准的 UI 设计模式?命名怎么样?我应该在这里为我的类和接口(interface)命名什么?

最佳答案

说实话,我不会太执着于成为其中之一(MVC、MVVM 或 MVP),它们几乎相同,重点是保持“大 split ”。也就是说,现在,恕我直言,您似乎最接近 MVP(模型 View 演示者)

问题是你有很多混合的逻辑:IPage实际上应该只是一个View,但你让它事情 Controller 通常会这样做。与它的子项相同:ISavePage有一个名为SetAutomation对象的方法,我通常希望在Controller中看到它(至少如果我是正确猜测其功能)。

Adam Freeman 出色地讲述了如何在 ASP.NET MVC 5 中分解这些内容:http://enos.itcollege.ee/~ijogi/Nooks/Pro%20ASP.NET%20MVC%205/Pro%20ASP.NET%20MVC%205.9781430265290.pdf查看第 51 页,他在其中详细说明了每个项目在概念上的含义,这可能有帮助吗?

鉴于我们已经确定您的 IPage 确实是一个 Controller ,我会尝试这样做。

public class PageElement
{
    IPageController _controller;

    // these are your models - your controller will simply allow them to be shown with your other methods
    private PageData _data; 
    private PageData _otherData;

    public PageElement(IPageController ctrl)
    {
         _controller = ctrl;
    }
}

public class PageController : IPageController
{
     IPageService _service;

     public PageController(IPageService service)
     {
          _service = service;
     }

     // this is what your button calls when clicked
     public void SaveAsync(object sender, SomeEventArgs args)
     {
           // the service does the actual work
          _service.SaveAsync()
     }
}

public class PageService : IPageService
{
     public void SaveAsync(){ // what it does}

}

关于c# - 我最终采用了什么设计模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079851/

相关文章:

javascript - 如何隐藏其他标签的内容,只显示选中标签的内容

performance - 了解用户 - 性能特朗普看起来像吗?

Java 和 Silverlight 永远在一起

c# - 有没有办法将方法标记为确保 T 不为空?

c# - 如何处理自定义 Powershell Cmdlet 的依赖项

c# - 从 Java 运行 C# 代码,反之亦然

c# - 调用 Sitefinity WCF Web 服务时出现错误 401

java - SWT 线程 : GUI not responding even after calling thread. stop()

银光 4 : Chart Toolkit Color Set

c# - 页眉和页脚布局