locked
Purpose of creating an Interface for each Repository RRS feed

  • Question

  • User1851820814 posted

    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();
            }

    _IExpense.AddExpense();

    I hope you understand my confusion here, Thanks.

    Monday, October 5, 2020 11:16 AM

All replies

  • User475983607 posted

    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.

    Monday, October 5, 2020 12:43 PM
  • User1851820814 posted

    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.

    Monday, October 5, 2020 2:36 PM
  • User475983607 posted

    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.

    Monday, October 5, 2020 2:56 PM
  • User-821857111 posted

    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:

    public async Task Delete(TEntity entity)
    {
        context.Entry(entity).State = EntityState.Deleted;
        await context.SaveChangesAsync();
    }

    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. 

    Tuesday, October 6, 2020 8:31 AM
  • User1851820814 posted

    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

    Tuesday, October 13, 2020 1:07 PM