DEV Community

Cover image for Demystifying the Repository Pattern in ASP.Net Core Web API

Demystifying the Repository Pattern in ASP.Net Core Web API

Nzekwe Emmanuel on February 21, 2022

An ASP.Net Core RESTful API can be implemented in different ways. This article will dissect the various steps involved in implementing one using th...
Collapse
 
jwp profile image
John Peters •

Very good article. Exposing code patterns which are better served by going generic is a mark of maturity.

Collapse
 
nze02 profile image
Nzekwe Emmanuel •

Mark of maturity...lolz!
Thanks for the review dear John

Collapse
 
emekakezie profile image
Nnaemeka Chukwukezie •

Nice one. A simplified approach.

Collapse
 
nze02 profile image
Nzekwe Emmanuel •

Yea. Thanks for your review Chief

Collapse
 
nze02 profile image
Nzekwe Emmanuel •

Thanks alot dear Sir

Collapse
 
vussan profile image
Bhusan Bhele • • Edited

Nicely put. But where do my business logic and calculations belong? I don't think the logics and calculation should be done either in repository or controller.

Collapse
 
nze02 profile image
Nzekwe Emmanuel •

Yea, you're right. Business logic should not be placed directly into Models, Views, or even Controllers because it will reduce the re-use and maintainability of your code. The scope of this article is trivial but for none trivial applications, I'll suggest you create a business logic layer and simply call the business logic layer from your models when needed. The business logic layer will in turn call into the data access layer.

Collapse
 
kenchi profile image
Simeon Kenneth •

Well presented. Really demystified it. Thanks for the article.

Collapse
 
nze02 profile image
Nzekwe Emmanuel •

Thanks for your feedback dear friend.

Collapse
 
cbc700 profile image
cbc700 •

I'll note that if you want the RepositoryBase methods (Create, FindAll, etc.) to be callable when using RepositoryManager, then the IRepositoryManager members Department and Student should be the class references DepartmentRepository/StudentRepository, rather than the interface references IDepartmentRepository/IStudentRepository.

Collapse
 
webhacker profile image
webhacker •

What would be the best way to force others to use the repository classes and restrict them from going directly to the EF objects and DBContext?

Collapse
 
cbc700 profile image
cbc700 •

Clean. Logical. Brilliant. Pick any 3. Thanks!