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 355757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:02:51+00:00 2026-05-12T12:02:51+00:00

I’m finding that the code that I write in my controllers is largely repetitive

  • 0

I’m finding that the code that I write in my controllers is largely repetitive , and more importantly that the tests I write for my controllers are also very repetitive. The general pattern for a save method is as follows…

Take an entity, and a number of primitive parameters. Write the values from the primitive parameters onto the same named parameters on my entity. Validate the entity. Save the entity if it is valid.

I already have a model binder that will bind anything that inherits from IDataEntity (an interface that all our database persisted entities implement). This model binder finds the appropriate repository and looks up the entity with the passed id.

I have been considering adding an extension to this model binder that will also pull the values in from the value provider and write them over the existing values. This will save me some repetitive code in my actions assigning values to my entities. And will also allow me to just pass entities to my controller tests rather than a large number of values.

for example a method that currently looks like this:

    public ActionResult SaveCompanyAddress(Address address,
        string address1,
        string address2,
        string address3,
        string city,
        string county,
        Country country,
        string postcode)
    {
        address.Address1 = address1;
        address.Address2 = address2;
        address.Address3 = address3;
        address.City = city;
        address.County = county;
        address.Country = country;
        address.Postcode = postcode;

        Validate(address);

        if (ModelState.IsValid)
        {
            using (var tran = TransactionFactory.CreateTransaction())
            {
                addressRepository.Save(entity);
                tran.Commit();
            }

            return this.RedirectToAction(x => x.List(address.Company));
        }
        else
        {
            return View("Edit", address);
        }
    }

Would be halved in length and have nearly all its arguments removed. This certainly sounds appealing as the amount of code we write is reduced, and a test to test validation can pass a mock validation provider and default address and not worry about setting all those parameters. but I am worried that there might be a little too much behind the scenes magic to make it a good idea.

Thoughts? Opnions? Ideas?

  • 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-12T12:02:51+00:00Added an answer on May 12, 2026 at 12:02 pm

    You might also compromise and use the default model binder to bind your simple properties and get the complex properties from the value provider and fill them in “manually.” For a model with only simple properties the default model binder would be sufficient. For those with one or two complex properties, those which aren’t handled by the default model binder, you could still save significant amounts of code without having to write the custom model binder. The complex binding code could be written using shared methods on a class. Once the binding code in your controller needs to cross controller boundaries or becomes significant, refactor it into your custom model binder.

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

Sidebar

Ask A Question

Stats

  • Questions 312k
  • Answers 312k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use ViewModel: class ViewModelClass { [DateValidationAttribute] public property… May 13, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer You need to make the second part optional: (r'^jobs/(?:(?P<job_id>\d+)/)?$', job_handler) May 13, 2026 at 10:41 pm
  • Editorial Team
    Editorial Team added an answer The author has oversimplified things. He's saying that everything under… May 13, 2026 at 10:41 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want use html5's new tag to play a wav file (currently only supported
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I've got a string that has curly quotes in it. I'd like to replace
In order to apply a triggered animation to all ToolTip s in my app,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.