在我的控制器中重构 Switch 语句
我目前正在开发 MVC.NET 3 应用程序;我最近参加了“鲍勃叔叔”马丁的一门课程,这启发了我(让我感到羞耻?)认真审视我当前的开发实践,特别是我的重构习惯。
所以:我的许多路由符合:
{controller}/{action}/{type}
其中 type 通常决定要返回的 ActionResult 的类型,例如:
public class ExportController
{
public ActionResult Generate(String type, String parameters)
{
switch (type)
{
case "csv":
//do something
case "html":
//do something else
case "json":
//do yet another thing
}
}
}
是否有人成功地将“用多态性替换开关”重构应用于类似的代码这?这是个好主意吗?很高兴听到您在这种重构方面的经验。
提前致谢!
I'm currently working on a MVC.NET 3 application; I recently attended a course by "Uncle Bob" Martin which has inspired me (shamed me?) into taking a hard look at my current development practice, particularly my refactoring habits.
So: a number of my routes conform to:
{controller}/{action}/{type}
Where type typically determines the type of ActionResult to be returned, e.g:
public class ExportController
{
public ActionResult Generate(String type, String parameters)
{
switch (type)
{
case "csv":
//do something
case "html":
//do something else
case "json":
//do yet another thing
}
}
}
Has anyone successfully applied the "replace switch with polymorhism" refactoring to code like this? Is this even a good idea? Would be great to hear your experiences with this kind of refactoring.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这个控制器操作正在尖叫着自定义操作结果:
然后:
控制器不应该关心如何序列化数据。那不是他的责任。控制器不应该做这样的管道工作。他应该专注于获取领域模型、将它们映射到视图模型并将这些视图模型传递给视图结果。
The way I am looking at it, this controller action is screaming for a custom action result:
and then:
A controller should not care about how to serialize the data. That's not his responsibility. A controller shouldn't be doing any plumbing like this. He should focus on fetching domain models, mapping them to view models and passing those view models to view results.
如果您想在这种情况下“用多态性替换 switch”,您可以创建三个重载的Generate() ActionResult 方法。使用自定义模型绑定,设置 Type 参数一个名为 DataFormat (或其他名称)的强类型枚举。然后您将拥有:
一旦到达这一点,您可以进一步重构以从控制器中获取重复。
If you wanted to "replace switch with polymorphism" in this case, you could create three overloaded Generate() ActionResult methods. Using custom model binding, make the Type parameter a strongly-typed enum called DataFormat (or whatever.) Then you'd have:
Once you get to this point, you can refactor further to get the repetition out of your Controller.