简体   繁体   English

在具有广泛MVC架构(C#)的非Web应用程序中控制程序流

[英]Controlling program flow in a non-web app with broad MVC architecture (c#)

I have a console app, the central process of which is to get the next line from a DB table, and based on a field value, run one of several bits of code which all return the same type o object. 我有一个控制台应用程序,其中心过程是从数据库表中获取下一行,并基于字段值运行全部返回相同类型o对象的几段代码之一。 I could have a switch or if statement type arrangement, which does something like (pseudo code): 我可能有一个开关或if语句类型安排,其作用类似于(伪代码):

if exhibitType == "Summary" then AddSummaryExhibit
elseif exhibitType == "Timeline" then AddTimelineExhibit

... etc. Almost like the routing setup in MVC websites. ...等。几乎就像MVC网站中的路由设置一样。 This program does follow an approximate MVC type structure, with each method getting data from a model, rendering HTML based on a .cshtml razor view and appending it to a collection of 'sections' that belong to a HTML document container. 该程序确实遵循近似的MVC类型结构,每种方法都从模型中获取数据,基于.cshtml剃刀视图呈现HTML,并将其附加到属于HTML文档容器的“节”的集合中。

Ideally it'd be nice just to add a new method of name x to my project and then allow x to be added as a new value in the db table without having this manually maintained set of conditionalities. 理想情况下,最好将一个名为x的新方法添加到我的项目中,然后允许x作为新值添加到db表中,而无需手动维护此条件集。 I was thinking maybe delegates or somesuch might apply here, but that's just a thought. 我在想也许代表或某些事情可能在这里适用,但这只是一个想法。 Any tips on best practice would be appreciated. 有关最佳做法的任何提示将不胜感激。

Thanks 谢谢

I believe you are looking for has already been answered here in the thread talking about long switch statements: How to avoid long switch ..need help refactoring 我相信您正在寻找的关于长切换语句的线程已经在这里回答了: 如何避免长切换..需要帮助重构

In effect, you are looking for a gang of four pattern. 实际上,您正在寻找具有四个模式的帮派。 The patterns that come to mind are the strategy pattern, state pattern and chain of responsibility pattern. 我想到的模式是策略模式,状态模式和责任链模式。

These patterns can be quite challenging to think about how they would work for your requirements, but its a huge step in a good direction by identifying a need for modularity and that you aren't getting it from your switch statement. 考虑这些模式如何满足您的需求可能会非常具有挑战性,但是通过确定对模块化的需求并且您没有从switch语句中获取模块化,这是朝着良好方向迈出的重要一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM