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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:21:52+00:00 2026-05-20T17:21:52+00:00

I want to refactor my basic CRUD operations as they are very repetitive but

  • 0

I want to refactor my basic CRUD operations as they are very repetitive but I’m not sure the best way to go about it. All of my controllers inherit BaseController which looks like so:

public class BaseController<T> : Controller where T : EntityObject
{
    protected Repository<T> Repository;

    public BaseController()
    {
        Repository = new Repository<T>(new Models.DatabaseContextContainer());
    }

    public virtual ActionResult Index()
    {
        return View(Repository.Get());
    }
}

I create new controllers like so:

public class ForumController : BaseController<Forum> { }

Nice and easy and as you can see my BaseController contains an Index() method so that means my controllers all have an Index method and will load their respective views and data from the repository – this works perfectly. I’m struggling on Edit/Add/Delete methods, my Add method in my repository looks like this:

public T Add(T Entity)
{
    Table.AddObject(Entity);
    SaveChanges();

    return Entity;
}

Again, nice and easy but in my BaseController I obviously can’t do:

public ActionResult Create(Category Category)
{
    Repository.Add(Category);
    return RedirectToAction("View", "Category", new { id = Category.Id });
}

as I usually would so: any ideas? My brain can’t seem to get pass this.. ;-/

  • 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-20T17:21:53+00:00Added an answer on May 20, 2026 at 5:21 pm

    You could add an interface shared by all entities:

    public interface IEntity
    {
        long ID { get; set; }
    }
    

    And make your base controller require this:

    public class BaseController<T> : Controller where T : class, IEntity
    

    Which would allow you to:

    public ActionResult Create(T entity)
    {
        Repository.Add(entity);
        return RedirectToAction("View", typeof(T).Name, new { ID = entity.ID });
    }
    

    You should also consider using dependency injection to instantiate your controllers, so that your repositories are injected rather than instantiated manually, but that is a separate topic.

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

Sidebar

Related Questions

I want to refactor some code using the Windsor IOC/DI framework, but my issue
Data binding in ActionScript is really cool. But what if I want to refactor
I want to refactor an emun in two new enums, but I don't like
I've got following JavaScript functions but want to refactor the $(document).ready() as I've got
I'm doing some work with stats, and want to refactor the following method :
actually i refactor some portion of code. what i want to do is to
Want the function to sort the table by HP but if duplicate HPs then
I have a spring mvc app that I want to refactor out, speficially removing
I've a old C/C++ class that i want to refactor and access from .net
i want to develop a pretty basic client-server program. one software reads xml (or

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.