user251261
user251261

Reputation: 87

What's the point for IRepository?

What's the point of it? It just add more code that you need to write, which doesn't really do anything to an app that it couldn't do before adding a IRepository.

Upvotes: 2

Views: 198

Answers (4)

Omu
Omu

Reputation: 71228

It's for testing, ever heard of mocking ?

Upvotes: 0

queen3
queen3

Reputation: 15511

I think I'll be downvoted for this (as usual for my not-a-real-help answers), but what's the point for MVC, anyway? Can't we just code stuff right into the page, good old ASP-style? Heck, we can even do Response.Write() + Html helpers, no need for those leaky abstractions.

You need to stop thinking in term "X gets job done". There're more things about programs except "it works". The other answers listed few of them so I won't repeat.

Upvotes: 1

codekaizen
codekaizen

Reputation: 27419

The big idea behind the Repository pattern is that you get a way to query for and save objects without needing to know how they are stored.

This is useful since you can decouple your UI and flow model from how the objects are stored, cached and retrieved. This allows greater ability to test the UI layers and lower layers independently, work on them independently, and service them independently.

Most mature Object Relational Mapping (ORM) frameworks allow you to do this.

Upvotes: 1

womp
womp

Reputation: 116977

Couple of reasons:

  1. Testing. You can mock your repository interface and pass it to all methods that are dependent on your repository (which would be just about any controller accessing the data layer). This in turn allows you to test properly, decoupled from a real database or data source.

  2. Inheritance. You can pass repository interfaces around rather than class types, allowing you to have heterogenous data sources at various levels of your inheritance tree, but still be guaranteed the same functionality exists. This is pretty much the same argument as "abstract classes vs. interfaces" at any time, but it stands here as well.

Upvotes: 5

Related Questions