如何在ASP.NET MVC应用程序中限制控制器中的操作?

时间:2022-05-07 06:14:37

I writing an MVC app and I'm really struggling to keep my controllers lean and limit the number of actions.

我写了一个MVC应用程序,我真的很难保持我的控制器精益,并限制行动的数量。

For example, here is a look at my ReportController actions: OpenCall
ClosedCall
ServiceLevelAgreement
Barrier
Resolution
Repair
Failure
Inventory
CustomerLocation

例如,下面是我的ReportController操作:OpenCall ClosedCall ServiceLevelAgreement阻隔解决方案修复失败库存CustomerLocation

These are all my different reports. Should I be making a controller for each one?

这些都是我不同的报道。我应该为每个人制作一个控制器吗?

Here is a look at my ServiceCallController actions: New
Create
Reopen
UpdateETA
UpdateOnsite
UpdateServiceTimes
UpdateEnroute
Close
Cancel
Reassign
Show
ModifyAfterClose

下面是我的ServiceCallController操作:New Create重新打开UpdateETA UpdateOnsite UpdateServiceTimes UpdateEnroute关闭取消重新分配显示ModifyAfterClose

This are all different actions I need to take based on what the user wants to do. Can anyone help me out here with how to clean this up?

这是我需要根据用户想要做的所有不同的操作。任何人都可以帮我解决这个问题吗?

2 个解决方案

#1


I think it's clean already. If an object of yours is capable of doing 7-8 actions, then your structure is the way to go. If you had one controller that is responsible for the actions of multiple business objects, then I'd have suggested you change it into the way your current design is in.

我觉得它已经干净了。如果你的一个对象能够做7-8个动作,那么你的结构就是你要走的路。如果您有一个负责多个业务对象操作的控制器,那么我建议您将其更改为当前设计的方式。

One thing though about your current design, it'd be a good idea to just have one update action on ServiceCallController and that action could take what to update as a parameter. After all, all those update actions do one thing : update your business object.

关于您当前的设计,有一点是,在ServiceCallController上只有一个更新操作是个好主意,该操作可以将更新内容作为参数。毕竟,所有这些更新操作都做了一件事:更新您的业务对象。

But if you are still not satisfied, then you can just have one action that's named "do" which takes the action as parameter and passes that to a service layer which returns what the request wanted. But IMO, that wouldn't be a good design, so I definitely don't suggest it.

但是如果你仍然不满意,那么你可以只有一个名为“do”的动作,它将动作作为参数并将其传递给服务层,该服务层返回请求所需的内容。但IMO,这不是一个好的设计,所以我绝对不建议。

#2


If the file it self is getting to large to work with you could either use partial classes, Or back the logic off in to a separate class and leave your controller think and lean.

如果它自己变大的文件可以使用部分类,或者将逻辑关闭到一个单独的类中,让控制器思考和倾斜。

#1


I think it's clean already. If an object of yours is capable of doing 7-8 actions, then your structure is the way to go. If you had one controller that is responsible for the actions of multiple business objects, then I'd have suggested you change it into the way your current design is in.

我觉得它已经干净了。如果你的一个对象能够做7-8个动作,那么你的结构就是你要走的路。如果您有一个负责多个业务对象操作的控制器,那么我建议您将其更改为当前设计的方式。

One thing though about your current design, it'd be a good idea to just have one update action on ServiceCallController and that action could take what to update as a parameter. After all, all those update actions do one thing : update your business object.

关于您当前的设计,有一点是,在ServiceCallController上只有一个更新操作是个好主意,该操作可以将更新内容作为参数。毕竟,所有这些更新操作都做了一件事:更新您的业务对象。

But if you are still not satisfied, then you can just have one action that's named "do" which takes the action as parameter and passes that to a service layer which returns what the request wanted. But IMO, that wouldn't be a good design, so I definitely don't suggest it.

但是如果你仍然不满意,那么你可以只有一个名为“do”的动作,它将动作作为参数并将其传递给服务层,该服务层返回请求所需的内容。但IMO,这不是一个好的设计,所以我绝对不建议。

#2


If the file it self is getting to large to work with you could either use partial classes, Or back the logic off in to a separate class and leave your controller think and lean.

如果它自己变大的文件可以使用部分类,或者将逻辑关闭到一个单独的类中,让控制器思考和倾斜。