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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:24:20+00:00 2026-05-27T07:24:20+00:00

I’m building an e-commerce site using C#, MVC3, Entity Framework 4, my first stab

  • 0

I’m building an e-commerce site using C#, MVC3, Entity Framework 4, my first stab at MVC3 and Entity Framework so I want to ensure a sound architecture. In particular, I’m questioning my usage of Interfaces and Dependency Inversion as they pertain to the Services and Repository layers. I’ll focus on one area of the system, the Cart system, for brevity and clarity.

Here’s an Interface Inversion example PluralSite uses to explain the typical tendency to create interfaces without consideration of proper dependencies.

Lets say you have an IKangaroo interface that a “BoxingMatch” class depends on. When you add more “boxers” like IMikeTyson and IJoeBoxer, you now have three different Interfaces, one for each boxer, that “BoxingMatch” needs to know about. IKangaroo, IMikeTyson, and IJoeBoxer only have one concrete implementation each, which means you don’t even need those interfaces (you may as well make BoxingMatch depend directly on the concrete Kangaroo, MikeTyson, and JoeBoxer classes). Further, it doesn’t even make sense that there would be more than one implementation of IKangaroo, or IMikeTyson. So the interfaces are needless and don’t bring any value to the architecture.

Inverting the dependencies in this example would result in “BoxingMatch” defining the interface that the classes its going to use (Kangaroo, MikeTyson and JoeBoxer) are going to implement. So, “BoxingMatch” would depend on an IBoxer interface, and Kangaroo, MikeTyson and JoeBoxer will all implement IBoxer. There’s the inversion, and it makes perfect sense.

Now, my situation… The CartController constructor has two dependency parameters injected, ICartService and IProductService). CartService takes a single injected constructor argument (ICartRepository). CartController calls AddItem() on CartService, and CartService calls AddItem() on CartRepository.

ICartRepository has two implementations: CartRepository (in the main Web project) and TestCartRepository (in the Tests project). ICartService only has a single implementation.

My questions: how does my architecture stack up to the lesson in the above example? I don’t really see how my CartController could be less-coupled than it already is to the CartService. The controller only depends on CartService, not ICartRepository. So it doesn’t seem I can invert control here by having the CartController define which interface CartService and ICartRepository are going to use, since CartService and CartRepository are different layers entirely. Am I correct?

Down one level, and same question. CartService depends on CartRepository. Would the above inversion principle apply here? Or have I already inverted the dependency by requiring an ICartRepository injected parameter in the CartService constructor?

So my question really is, did I do this “right”?

Any thoughts or advice would be appreciated.

My code, for reference:

CartController:

//constructor
public CartController(ICartService cartService, IProductService productService)
{
    _cartService = cartService;
    _productService = productService;
}

public RedirectToRouteResult AddItem(Cart cart, int productId)
{
    var product = _productService.GetProduct(productId);
    if (product != null)
    {
        _cartService.AddItem(cart, product, 1);
    }
    return RedirectToAction("Index");
}

CartService (implementation):

//constructor
public CartService(ICartRepository repository)
{
    _repository = repository;
}

public void AddItem(Cart cart, Product product, int quantity)
{
    //simplified for brevity
    var cartProduct = _repository.CartProducts().SingleOrDefault(cp => cp.CartId == cart.CartId && cp.ProductId == product.ProductId);
    _repository.AddCartItem(cartProduct);
}

Cart Repository (implementation):

public void AddCartItem(CartProduct cartProduct)
{
    _context.CartProducts.Add(cartProduct);
    _context.SaveChanges();
}
  • 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-27T07:24:21+00:00Added an answer on May 27, 2026 at 7:24 am

    You mistakenly seem to believe that the only reason to use IoC is to provide multiple implementations. In fact, this is one of the least useful reasons to use IoC.

    By using an IoC container, you can manage object lifetimes (for instance, by making objects live for the life of a single web request).

    An IoC container also handles recursive dependencies. If you create a IMikeTyson and IMikeTyson depends on IBoxingShorts, then you don’t have to instantiate the boxing shorts, they come along for free.

    All this ignores the big reason to use IoC, and that’s to make unit testing easier. By using interfaces, you can supply mock IMikeTysons for your unit tests of BoxingMatch so that you can feed the BoxingMatch known values, and not have to reset your database after each test.

    And, there are about 100 other benefits that IoC and Interface based programming bring to the table.

    I think you’re overthinking your Cart scenario. You just need to pass the instances you depend on, and don’t worry too much about the academic definition of IoC.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a French site that I want to parse, but am running into
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.