I have started a new project in ASP.NET MVC, and in a phase to create an application Architecture, I have download couple of sample projects where I saw an Interfaces been created for each repository class, I didn't understand why we need to do that as we
can access all methods in our repository without been inherited by interfaces, like:
expRepository = new ExpenseRepository(); expRepository.AddExpense();
instead of:
IExpense _IExpense;
public ExpenseController()
{
_IExpense = new ExpenseRepository();
}
Interfaces are a programming construct that allows you to plugin any class that adheres to the interface. This approach works well for unit testing.
We see Interfaces in the repository pattern because every repository class implements CRUD operations; Create, Retrieve, Update, and Delete. This pattern allows you to pass any class (that inherits the interface) to a generic method. The generic method
does not care about the actual type as long as the type implements Create, Retrieve, Update, and Delete.
Unfortunately, the example code you shared is too specific and does not follow the repository pattern. It looks more like a service or API which is generally bound to business logic operations specific to the API.
If we are not using Entity Framework and using Dapper as an ORM, does Generic Repository Pattern will work in that case.
Because EF automatically generate Add/Update/Get/Delete methods but with dapper we need to create separate method for each repository.
I can't answer your question.
The generic repository is a design pattern. Design patterns solve very specific programming problems. In my experience, the generic repository pattern while common, plays a small role in any project. Typically business logic is complex and does not fall
into simple CRUD operations. The majority of code fits into an API or service pattern.
If we are not using Entity Framework and using Dapper as an ORM, does Generic Repository Pattern will work in that case.
No. A Generic Repository is useful for centralising code that would otherwise be duplicated across multiple individual repository classes. For example, the code for deleting an entity in EF is identical if you take this approach:
That makes it a good candidate for a generic repository. With Dapper, you are going to write SQL that is specific to the entity (table) which can't be shared across entities.
Member
2 Points
8 Posts
Purpose of creating an Interface for each Repository
Oct 05, 2020 11:16 AM|ASPatel|LINK
I have started a new project in ASP.NET MVC, and in a phase to create an application Architecture, I have download couple of sample projects where I saw an Interfaces been created for each repository class, I didn't understand why we need to do that as we can access all methods in our repository without been inherited by interfaces, like:
instead of:
I hope you understand my confusion here, Thanks.
All-Star
53111 Points
23668 Posts
Re: Purpose of creating an Interface for each Repository
Oct 05, 2020 12:43 PM|mgebhard|LINK
Interfaces are a programming construct that allows you to plugin any class that adheres to the interface. This approach works well for unit testing.
We see Interfaces in the repository pattern because every repository class implements CRUD operations; Create, Retrieve, Update, and Delete. This pattern allows you to pass any class (that inherits the interface) to a generic method. The generic method does not care about the actual type as long as the type implements Create, Retrieve, Update, and Delete.
Unfortunately, the example code you shared is too specific and does not follow the repository pattern. It looks more like a service or API which is generally bound to business logic operations specific to the API.
Member
2 Points
8 Posts
Re: Purpose of creating an Interface for each Repository
Oct 05, 2020 02:36 PM|ASPatel|LINK
Thanks! I got your answer.
If we are not using Entity Framework and using Dapper as an ORM, does Generic Repository Pattern will work in that case.
Because EF automatically generate Add/Update/Get/Delete methods but with dapper we need to create separate method for each repository.
Thanks in advance.
All-Star
53111 Points
23668 Posts
Re: Purpose of creating an Interface for each Repository
Oct 05, 2020 02:56 PM|mgebhard|LINK
I can't answer your question.
The generic repository is a design pattern. Design patterns solve very specific programming problems. In my experience, the generic repository pattern while common, plays a small role in any project. Typically business logic is complex and does not fall into simple CRUD operations. The majority of code fits into an API or service pattern.
All-Star
194524 Points
28081 Posts
Moderator
Re: Purpose of creating an Interface for each Repository
Oct 06, 2020 08:31 AM|Mikesdotnetting|LINK
That makes it a good candidate for a generic repository. With Dapper, you are going to write SQL that is specific to the entity (table) which can't be shared across entities.
Member
2 Points
8 Posts
Re: Purpose of creating an Interface for each Repository
Oct 13, 2020 01:07 PM|ASPatel|LINK
I found Generic Repository with Dapper, and it works great for me.
https://github.com/moraleslarios/MoralesLarios.Data
https://itnext.io/generic-repository-pattern-using-dapper-bd48d9cd7ead