shenku
shenku

Reputation: 12420

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.

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 i.e. 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?

Thanks for the input/

Upvotes: 1

Views: 74

Answers (1)

Chad Ruppert
Chad Ruppert

Reputation: 3680

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.

Upvotes: 1

Related Questions