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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:49:05+00:00 2026-05-20T16:49:05+00:00

I think I’ve hit that paralysis by analysis state. I have an MVC app,

  • 0

I think I’ve hit that “paralysis by analysis” state.
I have an MVC app, using EF as an ORM.
So I’m trying to decide on the best data access pattern, and so far I’m thinking putting all data access logic into controllers is the way to go.. but it kinda doesn’t sound right.
Another option is creating an external repository, handling data interactions.
Here’s my pros/cons:

If embedding data access to controllers, I will end up with code like this:

using (DbContext db = new DbContext())
{
    User user = db.Users.Where(x=>x.Name == "Bob").Single();
    user.Address.Street = "some st";
    db.SaveChanges();
}

So with this, I get full benefits of lazy loading, I close connection right after I’m done, I’m flexible on where clause – all the niceties.
The con – I’m mixing a bunch of stuff in a single method – data checking, data access, UI interactions.

With Repository, I’m externalizing data access, and in theory can just replace repos if I decide to use ado.net or go with different database.
But, I don’t see a good clean way to realize lazy loading, and how to control DbContext/connection life time.
Say, I have IRepository interface with CRUD methods, how would I load a List of addresses that belong to a given user ? Making methods like GetAddressListByUserId looks ugly, wrong,
and will make me to create a bunch of methods that are just as ugly, and make little sense when using ORM.

I’m sure this problem been solved like million times, and hope there’s a solution somewhere..


And one more question on repository pattern – how do you deal with objects that are properties ? E.g. User has a list of addresses, how would you retrieve that list ? Create a repository for the address ? With ORM the address object doesn’t have to have a reference back to user, nor Id field, with repo – it will have to have all that. More code, more exposed properties..

  • 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-20T16:49:06+00:00Added an answer on May 20, 2026 at 4:49 pm

    The approach you choose depends a lot on the type of project you are going to be working with. For small projects where a Rapid Application Development (RAD) approach is required, it might almost be OK to use your EF model directly in the MVC project and have data access in the controllers, but the more the project grows, the more messy it will become and you will start running into more and more problems. In case you want good design and maintainability, there are several different approaches, but in general you can stick to the following:

    Keep your controllers and Views clean. Controllers should only control the application flow and not contain data access or even business logic. Views should only be used for presentation – give it a ViewModel and it will present it as Html (no business logic or calculations). A ViewModel per view is a pretty clean way of doing it.

    A typical controller action would look like:

    public ActionResult UpdateCompany(CompanyViewModel model)
    {
        if (ModelState.IsValid)
        {
            Company company = SomeCompanyViewModelHelper.
                              MapCompanyViewModelToDomainObject(model);
            companyService.UpdateCompany(company);
            return RedirectToRoute(/* Wherever you go after company is updated */);
        }
        // Return the same view with highlighted errors
        return View(model);
    }
    

    Due to the aforementioned reasons, it is good to abstract your data access (testability, ease of switching the data provider or ORM or whatever, etc.). The Repository pattern is a good choice, but here you also get a few implementation options. There’s always been a lot of discussion about generic/non-generic repositories, whether or not one should return IQueryables, etc. But eventually it’s for you to choose.

    Btw, why do you want lazy loading? As a rule, you know exactly what data you require for a specific view, so why would you choose to fetch it in a deferred way, thus making extra database calls, instead of eager loading everything you need in one call? Personally, I think it’s okay to have multiple Get methods for fetching objects with or without children. E.g.

    public class CompanyRepository
    {
        Get(int Id);
        Get(string name);
        GetWithEmployees(int id);
        ...
    }
    

    It might seem a bit overkill and you may choose a different approach, but as long as you have a pattern you follow, maintaining the code is much easier.

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

Sidebar

Related Questions

Think Design: I have many applications that share the same user database! Other tables
I think that I am having a slow evening but I have a question
I think the file that is produced is an .asm file, any idea how
I think that java executables (jar files) are trivial to decompile and get the
Think that says it all?
Think Global, Act Local That's what they tell you, however during all my time
Think 2 entities OneToOne mapped. Person and Car. A Person can have a Car.
think about having a skript where you have some words as input. now you
I think I just need someone to show me the obvious. I have a
I think this is not really possible but worth asking anyway. Say I have

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.