Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6561157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:09+00:00 2026-05-25T13:33:09+00:00

Currently in my application and using the unit of work pattern and generic repository,

  • 0

Currently in my application and using the unit of work pattern and generic repository, all my controllers contain all of the business logic. I’m in the process of putting everything over to use ViewModels instead of the straight Model.

While this is a good idea, now comes a question that can significantly separate my business logic in the controllers. For controllers and ViewModels, which should contain most of the business logic?

I’ve tried a few ways to get my ViewModels to practically contain all the business logic. However, I do have to have an argument in my ViewModel’s constructor that takes a Unit of Work. Is this a good idea?

My code smell tells me it is. However, I am just a little worried how this will be in consistency with controllers that perform actions that do not need ViewModels. Simply put, actions that do not require to pass a Model/ViewModel to a View; this case happens on actions that do redirects to other actions. Which means, my business logic can either stay in that action or I could separate that business logic into a function.

What is the best practice here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:33:10+00:00Added an answer on May 25, 2026 at 1:33 pm

    For controllers and ViewModels, which should contain most of the business logic?

    None of those.

    I’ve tried a few ways to get my ViewModels to practically contain all the business logic. However, I do have to have an argument in my ViewModel’s constructor that takes a Unit of Work. Is this a good idea?

    imho It’s a very bad idea. First of all you are breaking several of the SOLID principles. Bundling all code into the view model makes it hard to test. What if you want to use some of the business logic in another view? Do you duplicate that code?

    What is the best practice here?

    Let’s go back to the MVC pattern first. It’s a quite wide definition but knowing it should give you a feeling of what you should place where.

    • The “Model” in MVC is really everything that is used to pull the data together. It can be webservices, a business layer, repositories etc.

    • The view is all code that generates the HTML (since we are talking about web).

    • The controller should be considered to be a glue between the model and the view. Hence it should take the information from the Model and transform it into something usable by the view.

    The problem with that structure is that it’s quite easy to “leak” layer specific information into the other parts of the pattern. Hence Microsoft introduced ViewModels into their implementation of MVC.

    In this way we can remove all rendering logic from the views and put it into the ViewModel. Instead of doing this in your view:

    <span>@(model.Age == 0 ? "n/a" : model.Age)</span>
    

    you put that code inside the ViewModel instead and simply call @model.Age. In this way you don’t have to duplicate that code in all views that are using your view model.

    The answer to your question about the ViewModel is that it should only contain logic which is used to render the information from the “Model” properly.

    As for the controller, I would not put any business logic into it either. First of all, it makes it very hard to test your logic. Then you add more responsibilities to it (and by doing so breaking SRP). The only logic that is valid in the controller is to take the information from the ViewModel and transform it into something usable by the “Model” and vice versa.

    Hope that answers your question.

    Update

    I would create a separate project and add classes to it. Then just add a reference from your webproject and call those classes in the controllers.

    I would also start using an inversion of control container to get those dependencies created for me automagically.

    Autofac can both discover your services for you (zero-configuration) and inject itself into MVC.

    To follow the separated interface pattern create the following projects:

    • YourProject.BusinessLayer <– Add your classes here
    • YourProject.BusinessLayer.Specification <– Add you interfaces that defines your business layer here.
    • YourProject.Mvc <– The MVC project.

    The “Specification” project can be used to make it easier to test things and to make it easier to switch implementation (might be only a few classes and not necessarily the entire business layer). Read up on “Seperated Interface Pattern”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an application using MVC3, Razor view engine, Repository Pattern with Unit
I'm currently writing a special client application to allow our unit tests to work
Currently developing an application using the newest version of symfony, obtained through PEAR. This
I am currently building an application using ASP.NET MVC. The data entry pages are
I am currently building an application using Tomcat, Spring and JAVA. I am using
Hello my friend is currently development an application using SOA Architecture, He sent me
Currently I'm building an application using latest Prism for Silverlight 4. I've a module
We currently have developed an application using WCF. Our clients make connections to different
I am currently developing a Rails application using a database that was designed before
I'm currently working on a small web application using Visual Studio 2008 Express. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.