3

I am using MVC3 and Enitty Framework 4 with the Database first approach. It created the context template with its own validation for the fields based on the database. My question is where should I put the other validation based on the buisness logic. Should I put it in the Context.tt file, should it go in the controller or should it go in a seperate model?

asked Dec 12, 2012 at 14:19

1 Answer 1

4

At a minimum, business logic validation should go within the Controller for the framework you're using. If the validation logic is portable, creating a separate validation module, called by the Controller, would also be okay.

In general, place the validation logic as close as possible to the layer it affects. Likewise, validate items at the most appropriate layer (appropriate defined by what's being affected). The concept is a related to encapsulation and the SOLID principles. The goal is to allow changing out the individual components as needed, as well as making the validation logic more portable.

To be a little more clear, if there's validation logic to protect the database from errant changes, then that logic should be placed within (or called from) the Model. Likewise, input validation belongs within the View.

Some validation logic can pull double (or triple) duty or would be common across multiple modules in the same layer. It's appropriate to create a separate module for that logic. At the risk / expense of duplicate checks, I would still have each layer validate for the aspects it is responsible for.

answered Dec 12, 2012 at 15:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.