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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:06:22+00:00 2026-06-17T17:06:22+00:00

So 99% of all DI examples using the Repository pattern with MVC (or Web

  • 0

So 99% of all DI examples using the Repository pattern with MVC (or Web API), show something similar to this below in a controller action (left out constructor injection code for _repository). The issue is there is the assumption that nothing needs to be processed 1st (like rules) and commencing persistence occurs immediately:

public ActionResult Create(Person person)
{
    if (ModelState.IsValid)
    {
      _repository.Add(person);
    }

    return View(person);
}

The problem is, what if I need to process rules in the domain layer before persisting the person object? Obviously I don’t do that logic in the Controller, but rather need it in the domain layer. Which option below is better:

Option 1: Relegate all persistence details on the repository to be called in the domain / business layer and pull out of the controller. The updated code would look like this:

if (ModelState.IsValid)
{
  using (busniessLogic = new MyApp.BusniessLogic(_repository))
  {
       busniessLogic.ProcessRulesAndSavePerson(person);
  }
}

The domain layer might have a method like this:

public void ProcessRulesAndSavePerson(Person person)
{
   //process some rules...

   if(rules = true)
   {
     //use injected repository to add now that rules have passed
     _repository.Add(person)
   }
}

Option 2: Keep the same as now in the Controller, but just make a call to process the rules prior to saving on the repository. Updated code is like below:

if (ModelState.IsValid)
{
  using (busniessLogic = new MyApp.BusniessLogic())
  {           
       busniessLogic.ProcessRulesAboutPerson(person);
       _repository.Add(person)
  }
}

I’m open to other ideas too, but I am leaning toward Option #1. I like to keep the Controller thin and just pass along the injected _repository to the layer requiring it.

Any help on this is appreciated, thanks!

  • 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-17T17:06:23+00:00Added an answer on June 17, 2026 at 5:06 pm

    Both options will create a dependency on the business logic processor, you always have to be careful when using the new keyword because when you try to unit test your method, you almost always will run into problems, and that defeats the purpose of dependency injection.

    You are better of creating a service that handles complicated business logic and injecting that into your controller. If most of your logic is complicated, I would just skip passing the repository to the controller all together and just depend on the service to handle everything for me.

    Controller:

    public MyController(IPersonService personService) 
    {
        _personService = personService;
    }
    
    public ActionResult Create(Person person)
    {
        if (ModelState.IsValid)
           _personService.Create(person);
        else
           ......
    }
    

    PersonService:

    public PersonService(IPersonRepository repo) 
    {
        _repo = repo;
    }
    
    public int Create(Person person)
    {
        //businesslogic
        return _repo.Add(person);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All the examples I've come across using Google Maps API seem to show a
I've read through the Google Spreadsheets API PHP documentation. All examples are using Zend,
I'm using opengl and trying to create a first person camera. All examples use
I am using saml2.0 for sinlgle sign on(SSO). In all the examples I have
All of the examples I've seen of using yield return x; inside a C#
All examples are taken from the SICP Book: http://sicpinclojure.com/?q=sicp/1-3-3-procedures-general-methods This was motivated from the
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am making a website on asp.net MVC and I am using a repository
I want to implement Repository Pattern in my ASP.NET MVC + EF 4.3 project.
I'm pretty new to the repository pattern and dependency injection. Almost all repository patterns

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.