简体   繁体   English

如果不在ActionFilter中,应将逻辑放在哪里?

[英]Where to put logic if not in an ActionFilter?

So I have been reading some comments on StackOverlow, saying that you shouldn't put business logic in a custom ActionFilterAttribute because it is considered meta data. 因此,我一直在阅读有关StackOverlow的评论,说您不应将业务逻辑放入自定义ActionFilterAttribute因为它被视为元数据。

For example accessing a repository and doing some checks, and processing accordingly within your custom attribute (bad). 例如,访问存储库并进行一些检查,然后在您的自定义属性中进行处理(错误)。

Firstly I am not sure how much I agree with this because alot of the framework Filters (Authenticate) etc, do business logic - the main difference it is mostly within the scope of the controller only ie only accessing controller values, route data etc. 首先,我不确定我对此有多同意,因为很多框架的过滤器(身份验证)等都进行业务逻辑处理-主要区别在于它仅在控制器范围内,即仅访问控制器值,路由数据等。

So my question is, when you do need to use an ActionFilter to flag individual Actions for additional logic/processing, where do you put the logic? 所以我的问题是,当您确实需要使用ActionFilter来标记各个动作以进行其他逻辑/处理时,将逻辑放在哪里?

Thanks for the input/ 感谢您的输入/

There is nothing wrong with using actionfilters to do some business logic. 使用动作过滤器执行某些业务逻辑没有错。 Typically this is how you implement those cross cutting concerns like logging, security (as you pointed out). 通常,这就是您如何实现诸如日志记录,安全性(如您所指出)之类的交叉问题。

Their purpose is all in their name. 他们的目的全都以他们的名义。 Filters for your Actions. 过滤您的操作。 Either incoming actions, or outgoing modifications to your data to be output . 传入操作或对要输出的数据的传出修改。

What you need to beware is putting behaviors that modify business data and persist the changes in actionfilters. 您需要提防的是将修改业务数据并将其更改保留在动作过滤器中的行为。 These kinds of things are counter intuitive to the next developer to see your code. 这些事情对于下一个开发人员查看您的代码而言是直观的。 They don't filter anything, they change the state of the overall system, which is what Actions(Commands) are for. 它们不进行任何过滤,而是更改整个系统的状态,这就是Actions(Commands)的作用。

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

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