8

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..

Evgeni
  • 3,341
  • 7
  • 37
  • 64
  • See this question..http://stackoverflow.com/questions/3720013/repository-pattern-poco-and-business-entities/4583099#4583099 – gideon Mar 17 '11 at 13:27
  • Search for the `IRepository` pattern here on SO...related to EF.. you'll find very similar implementations – gideon Mar 17 '11 at 13:28
  • +1, because right now I'm using a generic repository pattern + these GetAddressListByUserId ugly methods + some others generic methods. – Jonathan Mar 17 '11 at 13:29
  • 1
    I think there's a strong headwind developing for performing data access directly in your controllers (and other top-level callers). For example, see this series of articles on [The Onion Architecture](http://jeffreypalermo.com/blog/the-onion-architecture-part-1/), the classic (if controversial) ["Repository is the New Singleton"](http://ayende.com/Blog/archive/2009/04/17/repository-is-the-new-singleton.aspx), and [The evils of the repository abstraction layer](http://ayende.com/Blog/archive/2011/03/16/architecting-in-the-pit-of-doom-the-evils-of-the.aspx). – Jeff Sternal Mar 17 '11 at 14:17
  • That seems to work well with ORMs. The ORM themselves are the DAL, so wrapping that into yet another layer seems.. excessive. My problem with this approach is that at some point I might go to ado.net for performance reasons, so that will mean a lot of refactoring in controllers. – Evgeni Mar 17 '11 at 16:18
  • @Eugene - one of the strong points those articles make (especially Ayende's) is that there's no abstraction that will let you make that kind of a change without a lot of refactoring anyway. Performance optimizations, for example, are usually deeply implementation-specific: they change the very shape of your data access. – Jeff Sternal Mar 17 '11 at 17:33
  • Yeah, I realize that. If I go with EF, and later on decide it carries too much performance penalty, I _will_ have to redo the interface, as I sure won't want to do lambda to SQL translation:) That's besides the fact that lambdas themselves are somewhat slowish, afaik. Now there's an issue with DRY - I probably will have to write a web service doing same data access. Argh! – Evgeni Mar 17 '11 at 18:09
  • Check the answer on this question : http://stackoverflow.com/questions/458146/repository-pattern-how-to-lazy-load-or-should-i-split-this-aggregate – David Mar 17 '11 at 13:26
  • Never mind, that was meant to be an answer – Evgeni Mar 17 '11 at 18:24
  • Yeah but the other answers are by far better on this question my answer is was to focused on the lazy-load part. – David Mar 17 '11 at 19:40

3 Answers3

7

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.

Yakimych
  • 17,612
  • 7
  • 52
  • 69
  • 1
    "Ease of switching ORMS" is a terrible reason to advocate for the Repository pattern. 1. It doesn't happen often. 2. There are enough differences between them that you are probably looking at a large refactor anyway. – John Farrell Mar 17 '11 at 14:47
  • @jfar - I agree, it doesn't happen often, and it's not an important reason, but it is one of the reasons anyway, and IMO deserves being on the list. – Yakimych Mar 17 '11 at 15:04
  • And yes, regarding `2` - if it does happen and `"you are looking at a large refactor"`, the `"large refactor"` is limited to one layer, while the other layers - e.g. Services, and most importantly the Web Presentation layer don't know about it and continue using the same repository methods (why should the Web layer care about how data access is performed?) – Yakimych Mar 17 '11 at 15:09
  • 1
    @Yakimych - Why not? Abstractions are for when implementations may change or you want to hide implementation details. Chances are a system written to interact with a relational db will not ever change to not using a relational db. One layer isn't a good reason at all either. If all my db access is done via controllers thats still one layer to change.- Also wanted to add that your "repository" isn't a repository pattern at all if you have GetWithEmployees(). All you are doing is writing a DAL with a fancier name. ;) – John Farrell Mar 17 '11 at 15:25
  • @jfar - As I mentioned in the beginning of my answer, having data access code in the controllers may be OK for small projects and RAD, but if you want long-term maintainability, you would probably opt for a better architecture. Then again - it's ultimately a matter of preference and my answer is based on my opinion and experience of using those patterns in my work. – Yakimych Mar 17 '11 at 15:34
  • Regarding the repository comment - I've seen and heard quite a bunch of different definitions and descriptions of what a repository is and what it should do, so I will just say that it's pointless to (and definitely not the right place) to start another such discussion. – Yakimych Mar 17 '11 at 15:35
  • @Yakimych - How does a Repository increase maintainability? I'm using a 1500 line repository right now and it is certainly not maintainable. I have to touch a class that 100 other classes are dependent on just to edit a query for a single page. – John Farrell Mar 17 '11 at 15:40
  • @jfar - To answer this question I need to see your code, your general architecture etc. I could tell you how it increases maintainability for me, but that would take a lot of writing. I don't think such discussions here are fair to the OP. Feel free to post a question with related code samples and we can have a discussion there. – Yakimych Mar 17 '11 at 15:49
  • @Yakimych - Looking for a more generic reason. "The Repository pattern increases maintainability because..." – John Farrell Mar 17 '11 at 15:59
  • 2
    For starters, I didn't say `"The Repository pattern increases maintainability"`. I said you would probably not want data access in your controllers if you want long-term maintainability for large projects (see one of previous comments). In this case the repository pattern is just referred to as one of the ways to break out the data access code. In any case, maintainability increases because your data access code, business logic and presentation logic are not blended into one piece of code. We usually try to make it clean and have Web, Services, Repositories layers. (And no 1500 line repos ;)) – Yakimych Mar 17 '11 at 16:09
  • 2
    Furthermore, having data access in controllers rather severely limits your data access code reusal. Not the rarest of cases is when you have a Web app and then want to create a windows app which essentially does the same. In this case you'll be copypasting most of your data access code from your webapp instead of reusing existing methods from the services or repo assembly. – Yakimych Mar 17 '11 at 16:12
  • 1
    @Yakimych That is one of the reasons I want to externalize db calls - there's a fair chance we'll have to provide a web service that does same thing. We could still use MVC for that, I guess, returning JSON, but not sure if that's the best way to do it. – Evgeni Mar 17 '11 at 16:27
  • 1
    @Yakimych - How would you avoid 1500 line repos if you have 200 GetCustomerBySomeDifferentCriteria() methods? - As far as data method reuse in a windows app, YAGNI. Code to the spec. I can't afford to waste time implementing or worrying about features nobody has asked or paid me for yet. Curious that two of the reasons to use repositories are for "maybe" features like ORM swapping and query re-use. – John Farrell Mar 17 '11 at 16:57
  • 1
    @jfar - We don't have 200 `Get` methods. And I certainly would NOT choose to avoid a 1500 lines repo by moving those 1500 lines into my controllers and blending with presenation logic ;) Regarding time wasting - once you get the hang of it and follow the patterns, it actually goes quicker. I will mention RAD once again - if you have a small project, tight deadline and limited financing - worrying about architecture is the last thing on your mind. However, for larger projects - in my experience - having good design and architecture saves time in the long term. – Yakimych Mar 17 '11 at 17:18
  • 1
    @Yakimych - You have presentation logic in your controllers? - What do you consider a large project? The project I'm on now is 30+ repositories with the biggest being 1500 lines long. 110 database tables. Looks pretty "by the book" in terms of Services/Repos architecture, there are unit tests, clean SRP. The project really highlights the bloated monsters repository/service patterns become over time. I'd almost say Repo/Services are good for small, very CRUD projects and not for large non-CRUD projects. – John Farrell Mar 17 '11 at 17:41
  • @jfar - Sounds large enough to me. You are welcome to rewrite it by moving your data access and business logic into the controllers and compare to what you have now. Get back to us with your impressions and conclusions ;) On a more serious note, though - it would be interesting to actually look at this project and analyze the problems you are facing. – Yakimych Mar 17 '11 at 17:55
  • @Yakimych - there shouldn't be data access *logic* in the controller: that's in your ORM. As for business logic, I don't think anyone is suggesting putting business logic in the controller (application logic, sure) – Jeff Sternal Mar 17 '11 at 18:07
  • @jfar - How would you architect it now, if you had a chance to start it from the scratch ? – Evgeni Mar 17 '11 at 18:10
  • @jfar - What would you call the code that you now have in your repositories. I assume you have calls such as `context.Companies.Where(c => c.Name == companyName && !c.IsDeleted)...` in there? – Yakimych Mar 17 '11 at 18:25
  • @Yakimych: I'd call those conditions *query specifications*, and that logic is going to bleed into controllers no matter what, even if it's expressed as parameters on a repository method call. E.g., `GetCompanies(string companyName, bool includeDeletedCompanies)`. – Jeff Sternal Mar 17 '11 at 18:49
  • @Jeff - Not necessarily. But that's a completely different conversation topic. @jfar - So you are suggesting something like moving those "query specifications" into the controller, grabbing the data and passing it to some service that performs business logic and then using the results in the controller? – Yakimych Mar 17 '11 at 19:01
5

Personally I do it this way:

I have an abstract Domain layer, which has methods not just CRUD, but specialized methods, for example UsersManager.Authenticate(), etc. It inside uses data access logic, or data-access layer abstraction (depending on the level of abstraction I need to have).

It is always better to have an abstract dependency at least. Here are some pros of it:

  • you can replace one implementation with another at a later time.
  • you can unit test your controller when needed.

As of controller itself, let it have 2 constructors: one with an abstract domain access class (e.g. facade of domain), and another (empty) constructor which chooses the default implementation. This way your controller lives well during web application run-time (calling empty constructor) and during the unit-testing (with mock domain layer injected).

Also, to be able to easily switch to another domain at a later time, be sure to inject the domain creator, instead of domain itself. This way, localizing the domain layer construction to the domain creator, you can switch to another implementation at any time, by just reconstructing the domain creator (by creator I mean some kind of factory).

I hope this helps.

Addition:

  • I would not recommend having CRUD methods in domain layer, because this will become a nightmare whenever you rich the unit-testing phase, or even more, when you need to change the implementation to the new one at a later time.
Tengiz
  • 8,011
  • 30
  • 39
  • +1 - see also data access object. It uses a repository to perform the queries you need. – Adam Rackis Mar 17 '11 at 13:39
  • So you have an abstract repository for each class that's data driven ? – Evgeni Mar 17 '11 at 16:20
  • What do you mean by class that's data driven? - controller? – Tengiz Mar 17 '11 at 16:23
  • He probably meant domain classes. – Yakimych Mar 17 '11 at 16:39
  • Thanks Yakimych! Actually, I do an abstract repository for each domain data class - yes. But it would be equally effective to have a direct data access code inside domain data classes for the simplicity. All depends on the project size and extensibility requirements. The main thing is - have everything injected into controller - and this will make your life better... well, at least repository :-) – Tengiz Mar 17 '11 at 16:45
  • I agree. That's why you should choose your way. As a general rule of thumb in architecture - there is no the only solution for all cases. I just mentioned how far you can go, but it's up to you to shorten the way. – Tengiz Mar 18 '11 at 05:59
0

It really comes down to where you want your code. If you need to have data access for an object you can put it behind an IRepository object or in the controller doesn't matter: you will still wind up with either a series of GetByXXX calls or the equivilent code. Either way you can lazy load and control the lifetime of the connection. So now you need to ask yourself: where do I want my code to live?

Personally, I would argue to get it out of the controller. By that I mean moving it to another layer. Probably using an IRespository type of pattern where you have a series of GetByXXX calls. Sure they are ugly. Wrong? I would argue otherwise. At least they are all contained within the same logical layer together rather than being scattered throughout the controllers where they are mixed in with validation code, etc.

Ken Brittain
  • 2,255
  • 17
  • 21
  • 3
    -1 'for the 'or in the controller doesn't matter.' It **does** matter. A class should have only one reason to change. If the controller is doing two things, then it has two reasons to change, and that's bad design. – George Stocker Mar 17 '11 at 13:45
  • Well said George. Ken, see also http://en.wikipedia.org/wiki/Single_responsibility_principle – Adam Rackis Mar 17 '11 at 13:46
  • 1
    @George Stocker - Controllers by their very nature violate SRP. Big repositories/services violate SRP as well. You can change a repository/service layer because GetCustomer needs additional fields because and then GetCustomerBalanceByDateRange needs to change as well. Repos/Services become monstrous towers of SRP violations when each class dependent on it uses only a fraction of its functionality. – John Farrell Mar 17 '11 at 14:44