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

  • SEARCH
  • Home
  • 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 3344834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:06:54+00:00 2026-05-18T01:06:54+00:00

My simplest ASP.NET MVC 2 controllers make calls to my service layer and map

  • 0

My simplest ASP.NET MVC 2 controllers make calls to my service layer and map view models to entities using AutoMapper. Everything looks fantastic and there is no repeated code.

However, when I get into scenarios where I have similar behavior I have trouble balancing Single Responsibility Principle (SRP) with Don’t Repeat Yourself (DRY). An example of this might be the need to add/edit vehicles where some properties/behaviors are shared while others are unique to a specific vehicle.

If I strive for really thin controllers (thus honoring Single Responsibility Principle), I end up having repeated code in both the views and controllers with minor variations (title, field labels, field visibility, dropdown values, selection criteria, etc.).

If I strive for non-repeated code I end up bundling too much logic into a single controller/view and it gets bloated.

What are some ways of addressing repeated code in controllers / views? I’m not talking about database code that can be factored out to a repository. Nor am I talking about business logic that can be factored out to a service layer. I’m looking for tools and/or rules of thumb that will help me produce the best solution in the scenario described above.

  • 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-18T01:06:55+00:00Added an answer on May 18, 2026 at 1:06 am

    You get:

    • partials
    • RenderAction
    • action filters
    • service layer and helper classes (not HtmlHelper)
    • model binders
    • base controllers
    • dependency injection

    So your views can invoke shared partials/actions for similar parts, common data can be prepared by action filters, database access code can be hidden in smart model binder, or you can have parent controller that child controllers override with specific tweaks. And, of course, good old service layeres, where you just extract common code into helper/static methods, or, better, inject specific implementations.

    That’s nothing new, same old tricks.

    Or, maybe, your controllers do too much works? This is where stuff above also helps. ASP.NET MVC has very good tools to hide infrastructure-layer code and move it away from controllers. And if it’s not infrastructure – it probably belongs to domain layer. There you can use inheritance, composition and other OOP tricks.

    Specific example. Suppose your controllers should set few properties in a different way.

    1. You can have your views to do this, if it’s mostly formatting or choosing what properties to show
    2. You can have your entities to have virtual methods – i.e. refactor code to move decisions to domain layer instead of controllers
    3. You can have helper ViewDetails classes that will take your entities and get the data based on for what you need it; this is a bit of a dirty trick but sometimes useful; you delegate decision to another “strategy” class
    4. You can use action filters to add this data to ViewData, or to tweak specific ViewData.Model types (look for some interface of it).
    5. You can have abstract controller, where children pass implementation details to the base constructor like (): base(repository => repository.GetSpecificData())

    And so on. I actually use all of them in appropriate places.

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

Sidebar

Related Questions

I am developing a site using Asp.net MVC 3 with Razor. In the _Layout.cshtml
I have an asp.net application which is using form authentication, asp.net membership provider (ASP.NET
I have an IIS7.5 web-site, on Windows Server 2008, with an ASP.NET MVC2 web-site
I'm developing an in-house ASP.Net application, due to some reason, I've to create dynamic
I am working on site something like job portal in ASP.NET C#. Here I
I need to run different consecutive calls to a web service with no particular
I need to run different consecutive calls to a web service with no particular
I need to run different consecutive calls to a web service with no particular
I need to run different consecutive calls to a web service with no particular
I need to run different consecutive calls to a web service with no particular

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.