メソッド パラメーターの検証に感謝します。流暢な API に関する新しいアイデアを得ることができました。とにかく、前提条件チェックが嫌いでした...
開発中の新製品用に拡張システムを構築しました。このシステムでは、使用可能なコマンド、ユーザー インターフェイス要素などを流暢に説明できます。これは、優れた API である StructureMap と FluentNHibernate の上で実行されます。
MenuBarController mb;
// ...
mb.Add(Resources.FileMenu, x =>
{
x.Executes(CommandNames.File);
x.Menu
.AddButton(Resources.FileNewCommandImage, Resources.FileNew, Resources.FileNewTip, y => y.Executes(CommandNames.FileNew))
.AddButton(null, Resources.FileOpen, Resources.FileOpenTip, y =>
{
y.Executes(CommandNames.FileOpen);
y.Menu
.AddButton(Resources.FileOpenFileCommandImage, Resources.OpenFromFile, Resources.OpenFromFileTop, z => z.Executes(CommandNames.FileOpenFile))
.AddButton(Resources.FileOpenRecordCommandImage, Resources.OpenRecord, Resources.OpenRecordTip, z => z.Executes(CommandNames.FileOpenRecord));
})
.AddSeperator()
.AddButton(null, Resources.FileClose, Resources.FileCloseTip, y => y.Executes(CommandNames.FileClose))
.AddSeperator();
// ...
});
そして、次のように使用可能なすべてのコマンドを構成できます。
Command(CommandNames.File)
.Is<DummyCommand>()
.AlwaysEnabled();
Command(CommandNames.FileNew)
.Bind(Shortcut.CtrlN)
.Is<FileNewCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileSave)
.Bind(Shortcut.CtrlS)
.Enable(WorkspaceStatusProviderNames.DocumentOpen)
.Is<FileSaveCommand>();
Command(CommandNames.FileSaveAs)
.Bind(Shortcut.CtrlShiftS)
.Enable(WorkspaceStatusProviderNames.DocumentOpen)
.Is<FileSaveAsCommand>();
Command(CommandNames.FileOpen)
.Is<FileOpenCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileOpenFile)
.Bind(Shortcut.CtrlO)
.Is<FileOpenFileCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
Command(CommandNames.FileOpenRecord)
.Bind(Shortcut.CtrlShiftO)
.Is<FileOpenRecordCommand>()
.Enable(WorkspaceStatusProviderNames.DocumentFactoryRegistered);
私たちのビューは、ワークスペースによって与えられたサービスを使用して、標準の編集メニュー コマンドのコントロールを構成します。
Workspace
.Observe(control1)
.Observe(control2)
ユーザーがコントロールにタブで移動すると、ワークスペースは自動的にコントロール用の適切なアダプターを取得し、元に戻す/やり直しおよびクリップボード操作を提供します。
これにより、セットアップ コードが大幅に削減され、さらに読みやすくなりました。
ビューを検証するために WinForms MVP モデル プレゼンターで使用しているライブラリ、FluentValidationについて話すのを忘れていました。本当に簡単で、本当にテスト可能で、本当に素晴らしいです!