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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:48:04+00:00 2026-05-20T05:48:04+00:00

Could someone explain the benefits of using this pattern? I mean isn’t EF a

  • 0

Could someone explain the benefits of using this pattern?

I mean isn’t EF a repository in a sense already? Can’t you just query the container and return those objects?

I see a lot of talk about POCO’s, AutoMapper, Dependency Injection, Service Layers, IoC. Am I just mixing a bunch of stuff together, or does it all relate?

Can someone explain this to me?

Also, how does this all fit together with MVC.net, ViewModels vs DataModels?

Thanks,
Sam

  • 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-20T05:48:05+00:00Added an answer on May 20, 2026 at 5:48 am

    There is a bunch of random question’s there, so i’ll give a bunch of random answers:

    • A repository is defined (by Martin Fowler) as “Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects”
    • EF is an ORM, which is the data mapping layer
    • Yes, you can query the EF container and return the objects, but then the consumer “knows too much” about the underlying persistence mechanism, when it shouldn’t know/care – hence the Repository pattern
    • POCO’s are a technique used to free your objects from persistence meta data (used by default EF code gen). They also allow your POCO’s to double as your domain models.
    • ViewModels are used by MVC Views.
    • AutoMapper is used to translate ViewModels into Domain Models with ease.
    • Service Layers provide a middle-ground between Controllers and the Repository – used specifically with IQueryable<T> Repositories, to materialize queries into concrete sequences (e.g ICollection<T>).
    • IoC is used to de-couple dependencies between components, which gives a good/clean architecture and supreme testability (you can pass through a Mock repository to your Controllers, to unit test them in an isolated fashion).

    Elaboration Requested on #3

    Example Controller WITHOUT Repository:

    public class ProductsController
    {
       public ActionResult GetProduct(int productId)
       {
           Product p;
    
           using (var ctx = new MySecretContextWhichIsNowExposed())
           {
              p = ctx.Products.SingleOrDefault(x => x.ProductId == productId);
           }
    
           return View(p);
       }
    }
    

    Problems with the above:

    1. The Controller is working directly with an Entity Framework Object Context, which means the Web Application knows about it, and will need a reference to System.Data.Entity.
    2. Unit Testing is virtually impossible. You’ll be testing against the actual database which makes it an integration test and not a unit test.
    3. The Controller has the logic of “how to retrieve a single product”, when it shouldn’t – this is domain/repository logic.

    Example Controller WITH Repository (and IoC):

    public class ProductsController
    {
       private readonly IProductsRepository _repo;
    
       public ProductsController(IProductsRepository repo)
       {
          _repo = repo;
       }
    
       public ActionResult GetProduct(int productId)
       {
           Product p = _repo.FindById(productId);
           return View(p);
       }
    }
    

    Why above is better:

    1. Controller has no idea about EF. No dependency on System.Data.Entity.
    2. Controller doesn’t know about the implementation of the Repository, it’s working via an interface.
    3. Controller doesn’t supply logic on how to retrieve the product, only the id.
    4. 2 lines of simple code.
    5. Unit of Testing = piece of cake. Pass through a MockProductRepository in the ctor, and work off that (which could be implemented as an in-memory list).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone explain me how can this be possible: foreach (var pair in Expected.Zip(
Could someone explain why this works in C#.NET 2.0: Nullable<DateTime> foo; if (true) foo
Could someone explain why both tests using the latest versions of Moq and Rhino.Mocks
I was kind of shocked by this. Could someone explain why this works? A
I ran across this and was wondering if someone could explain why this works
Could someone explain the main benefits for choosing one over the other and the
Could someone explain me why this code: class safe_bool_base { //13 protected: typedef void
Could someone explain what's wrong with this snippet? I'm looking to get the width
I'm just starting with OSGi and Eclipse RCP. Could someone explain to me the
Could someone explain this for me? I have standard relations in my MSSQL DB:

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.