Published in


Repository Pattern with Entity framework in ASP.NET Core 6.0

Repository pattern is quite famous pattern in .NET world while accessing backend data using ORM (Object-Relational Mapping) system such as Entity framework. You can use any ORM based system like

  1. Dapper
  2. Venflow
  3. DevExpress XPO

Having an Repository pattern will create an abstraction from the ORM itself.

Repository pattern can help insulate our application from changes in the data store and can facilitate automated unit testing or test-driven development

Creating Repository Pattern

The first step in creating repository pattern is to create an Interface with all the methods declaration used by your application data access layer with ORM. In our case, we are using Entity framework and we need to access data from SQL Server. So, the code will look something like this

And the implementation of the interface code will look as shown below

Adding to DI service container

Finally, we need to add the Dependency Injection in Service container

builder.Services.AddScoped<IDataRepository, DataRepository>();

Complete Video

Here is the complete video of the above article



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store