-1

We have a 3-tier architecture: Web, Business, Models using the MVC pattern. Models are Code-First using EF6. Currently we access the dbContext directly in our Controllers to query and save changes. We define all the relationship constraints on the Models. However, we have many additional constraints on the models that are spread about in the controllers, or the Business layer as part of other processing.

Is there a cleaner way of accomplishing this whereby all the business constraints on the Models can be enforced? We have used the repository layer in the past on other projects, but it becomes inflexible and tedious to go through it for everything. Is there a best of both worlds approach whereby we can use the dbContext directly to query things, but when saving to db, we can be ensured that all the business constraints will be enforced with any violations passed back through to the caller?

Edit: this explains succintly why I don't want to use Repository pattern again.

0

We have used the repository layer in the past on other projects, but it becomes inflexible and tedious to go through it for everything

The repository layer is keeping you honest.

If you can access the DB in controllers there is nothing stopping you running whatever SQL you like and ignoring the business constraints.

Except maybe a few old fashioned not recommended options:

  1. Use SPROCs for all operations and put the business constraints in them
  2. Encode the business constraints into the DB with FK Constraints and the like.
  3. Active record pattern. Put the Save() methods on the object and enforce the constraints there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.