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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:50:00+00:00 2026-06-03T21:50:00+00:00

In an attempt to DRY up my code today i’d like to do the

  • 0

In an attempt to DRY up my code today i’d like to do the following. (I don’t know if its the best way, but it seems better than to have an ever increasing code base where I continually need to update multiple methods if i want to change something across the whole site)

What i know about Inheritance is scary. As Iv’e never questioned any of the code/libraries that I use, and Iv’e never really attempted writing anything like this before, but I want to learn… Hoping this will be my day of enlightenment 😛

To my question:

Say Iv’e got an add method (in all my controllers) like this:

public ActionResult Add(VM_Down_Time_Capture viewModel)
        {
            using (Down_Time_CaptureRepository repository = new Down_Time_CaptureRepository())
            {
                if (!ModelState.IsValid)
                    return ReturnValidationFailure(ViewData.ModelState.Values);

                Down_Time_Capture model = new Down_Time_Capture();
                model.InjectFrom(viewModel);

                string mserMsg = repository.Add(model, User.Identity.Name);

                if (!string.IsNullOrEmpty(mserMsg))
                    return ReturnCustomValidationFailure(Server.HtmlEncode(mserMsg));

                repository.Save();

                return Json("Added successfully.", JsonRequestBehavior.AllowGet);
            }
        }

And at the moment I’ve got the following as well.

Generated by T4 Templates/EF.

    ViewModels, Repositories, (Standard) EF Models

I’m thinking I need a ModelSpecfic base controller for each page (can be done using T4), that inherits from a custom ControllerBase class that contains the basic CRUD functionality. That way i can have custom code per controller, and my code base will be cleaner & smaller & that wont get affected should i need to regenerate the base files

I don’t quite understand how to implement something in the lines of what i need. What i understand so far is that ill need to have my repositories, and view models inherit from a base as well and somehow specify in [B] which ones I’m using… but as to how to do that i don’t know

For example (and this is my best attempt at it, not my actual code, extremely hacky as I’m amazingly confused :S)

    public class Down_Time_CaptureController : Down_Time_CaptureBase
    {
     //[A]
    }

    //Generated by T4
    public class Down_Time_CaptureBase: ControllerBase
    {
      //[B]
       public override EntityObject CreateNewModel()
       {
          return new Down_Time_Capture();
       }

       public override Base_Repository CreateNewRepository()
       {
          return new Down_Time_CaptureRepository();
       }

       public override Base_ViewModel CreateNewViewModel()
       {
          return new VM_Down_Time_Capture();
       }

      //how would i go about specifying which repository & model & view model to use
      //although i expect it to be something to what i did here above
      //and how would i go about calling the new generic add method (but in context of this controller)?

    }

    //coded once
    public abstract class ControllerBase: Controller
    {
        //[C]
        //make abstract so i have to override it
        public abstract Base_Controller CreateNewModel();
        public abstract Base_Controller CreateNewRepository();
        public abstract Base_Controller CreateNewViewModel();

        //I'm assuming my generified add method would go in here  
        public virtual ActionResult Add(Base_ViewModel viewModel)
        {
           using (Base_Repository repository = CreateRepository())
           {
                   if (!ModelState.IsValid)
                       return ReturnValidationFailure(ViewData.ModelState.Values);

                   EntityObject model = CreateNewModel();
                   model.InjectFrom(viewModel);

                   string mserMsg = repository.Add(model, User.Identity.Name);

                   if (!string.IsNullOrEmpty(mserMsg))
                       return ReturnCustomValidationFailure(Server.HtmlEncode(mserMsg));

                   repository.Save();

                   return Json("Added successfully.", JsonRequestBehavior.AllowGet);
            }       
        }
    }
  • 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-06-03T21:50:02+00:00Added an answer on June 3, 2026 at 9:50 pm

    Here’s a simple generic interpretation of what you are asking for:

    // concrete controller implementation
    public class Down_Time_CaptureController: ControllerBase<Down_Time_Capture, VM_Down_Time_Capture, Down_Time_CaptureRepository>
    {
    }
    
    // generic controller base
    public abstract class ControllerBase<TModel, TViewModel, TRepository>: Controller
            where TModel : Base_Model, new()
            where TViewModel : Base_ViewModel, new()
            where TRepository : Base_Repository, new()
    {
        protected virtual TModel CreateNewModel()
        {
               return (TModel)Activator.CreateInstance<TModel>();
    
        }
    
        protected virtual TRepository CreateNewRepository()
        {
               return (TRepository)Activator.CreateInstance<TRepository>();
        }
    
        protected virtual TViewModel CreateNewViewModel()
        {
                return (TViewModel)Activator.CreateInstance<TViewModel>();
        }
    
        //I'm assuming my generified add method would go in here  
        public virtual ActionResult Add(TViewModel viewModel)
        {
           using (var repository = CreateRepository())
           {
                   if (!ModelState.IsValid)
                       return ReturnValidationFailure(ViewData.ModelState.Values);
    
                   var model = CreateNewModel();
                   model.InjectFrom(viewModel);
    
                   string mserMsg = repository.Add(model, User.Identity.Name);
    
                   if (!string.IsNullOrEmpty(mserMsg))
                       return ReturnCustomValidationFailure(Server.HtmlEncode(mserMsg));
    
                   repository.Save();
    
                   return Json("Added successfully.", JsonRequestBehavior.AllowGet);
            }       
        }
    }
    

    A few notes:

    1. You will probably want to create interfaces for the three types (Model, ViewModel, Repository) and use those as the generic constraints.
    2. You will probably want a generic Repository interface and base implementation (so you don’t have to code each repository independently, and copy similar logic from one to the other).
    3. Consider using an Inversion of Control container and dependency injection. Rather than have the controller, for example, handle creating an instance of a repository, make it a property and set it from the constructor. You can then use an IoC of your choice (like Ninject or Autofac) and register concrete implementations, and it will manage creating and the lifetime of both the dependencies and the controller itself.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first attempt at reverse engineering, and really, I don't know how
I'm trying to attempt something that seems super simple, but right now I want
This is my first attempt at C++, following an example to calculate a tip
Is there any attempt to re-make something like Vimeo's couch mode http://vimeo.com/couchmode in jQuery?
In an attempt to figure out how to integrate code from one program into
Second attempt at this question (the initial code wasn't enough to highlight the issue)
Here's my best attempt to recreate the situation. public interface IFoo { } public
Following is my attempt to write BK-Tree , for 150000 word file it takes
I'll attempt to shorten this code example: public interface IThing { //... Stuff }
An attempt to explain this as best as possible: To begin, I have a

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.