在代码中尝试使用Cockburn用例进行实验
我正在编写一些复杂的UI代码。我决定采用Cockburn用例(Martin Fowler在他的书“UML Distilled”中讨论了鱼、风筝和海平面)并将其封装在静态C#对象中,以便可以针对表示UI工作流程步骤的静态常量测试逻辑条件。这个想法是,您可以阅读代码并知道它在做什么,因为封装的对象及其公共常量通过名称空间给出了英语用例。
此外,我将使用反射来生成包括描述的用例的错误消息。这个想法是,堆栈跟踪可以包含一些英文用户界面用例步骤....结果证明这是一个有趣的方法来实现小型、伪轻量级域语言,但不必编写DSL编译器。所以我的问题是这样做是否是一个好方法?有没有其他人做过类似的事情?
c#示例片段如下
假设我们有一个aspx页面,其中有3个用户控件(具有大量可点击部分)。用户必须单击一个特定的用户控件上的内容(可能进行某种选择),然后,UI必须以视觉提示用户选择成功。现在,在该项被选择的同时,用户必须浏览网格视图以查找另一个用户控件中的项目,然后选择某些内容。这听起来很容易管理,但代码可能会变得混乱。
在我的情况下,所有的用户控件都发送了事件消息,这些消息被主页面捕获。这样,页面就像UI事件的中央处理器一样,可以跟踪用户点击时发生的情况。
因此,在主aspx页面中,我们捕获第一个用户控制的事件。
using MyCompany.MyApp.Web.UseCases;
protected void MyFirstUserControl_SomeUIWorkflowRequestCommingIn(object sender, EventArgs e)
{
// some code here to respond and make "state" changes or whatever
//
// blah blah blah
// finally we have this (how did we know to call fish level method?? because we knew when we wrote the code to send the event in the user control)
UpdateUserInterfaceOnFishLevelUseCaseGoalSuccess(FishLevel.SomeNamedUIWorkflow.SelectedItemForPurchase)
}
protected void UpdateUserInterfaceOnFishLevelGoalSuccess(FishLevel.SomeNamedUIWorkflow goal)
{
switch (goal)
{
case FishLevel.SomeNamedUIWorkflow.NewMasterItemSelected:
//call some UI related methods here including methods for the other user controls if necessary....
break;
case FishLevel.SomeNamedUIWorkFlow.DrillDownOnDetails:
//call some UI related methods here including methods for the other user controls if necessary....
break;
case FishLevel.SomeNamedUIWorkFlow.CancelMultiSelect:
//call some UI related methods here including methods for the other user controls if necessary....
break;
// more cases...
}
}
}
//also we have
protected void UpdateUserInterfaceOnSeaLevelGoalSuccess(SeaLevel.SomeNamedUIWorkflow goal)
{
switch (goal)
{
case SeaLevel.CheckOutWorkflow.ChangedCreditCard:
// do stuff
// more cases...
}
}
}
因此,在MyCompany.MyApp.Web.UseCases名称空间中,我们可能会有以下代码:
class SeaLevel...
class FishLevel...
class KiteLevel...
嵌入在类中的工作流用例可以是内部类、静态方法、枚举或任何使命名空间最清晰的东西。我不记得我最初做了什么,但你明白我的意思。