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
- Dapper
- Venflow
- 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