Sitemap

Repository Pattern Example

4 min readFeb 13, 2019

--

Hello Guys,

Press enter or click to view image in full size

Let’s keep writing our Repository Pattern. In my previous article We have created IEntityRepository Interface. Now we will implement it to a class then we will learn how to use it. I am going to use Entity Framework for database operations. Define a class and name it EfEntityRepositoryBase and make it generic type. This class will get 2 generic variables. Those are <TEntity, TContext>. TEntity represents the class I will work on, TContext represents the Entity Framework Context I will work on . Do not worry about them. You will get it easily when we get into the code. If you remember my previous article I defined a constraint for IEntityRepository. We will write some more code here.

Press enter or click to view image in full size
Repository Pattern Implementation

I am writing a constraint for EfEntityRepository which I have done it For IEntityRepository in my previously article and also I am writing a constrait for TContext. It is like this => Where TContext : DbContext, new(). TContext must be inherited from DbContext and it must be able to create an instance. Now we can implement our Interface to our class. When you implement IEntityRepository Interface to EfEntityRepositoryBase class you will see our methods will be created.

Press enter or click to view image in full size
Our Methods

Guys if you know Entity Framework we perform database operations in Using block. Basically DbContext is an expensive operation. We want to dispose it when it is done. It is possible when we use Using block. Let’s focus on our Add Method. Firstly let’s write Using block. Type Using and open brackets then type fancy brackets. It should be like this => Using( ){ }. Now type TContext and create an instance inside of brackets. Give a name for that instance. I gave context. After that type context.Entry(entity).State = EntityState.Added; then context.SaveChanges(); inside of fancy brackets.

Get Recep Mert ASLAN’s stories in your inbox

Join Medium for free to get updates from this writer.

Press enter or click to view image in full size

This comes from Entity Framework. It performs database add operation instead of you and save all changes as well. For update and delete methods the main concept is the same. Just replace Added with Deleted For deleting operation and replace Added with Modified For updating operation.

Press enter or click to view image in full size

We are done with adding, deleting, and updating database. Now time to focus on select operation. It is very simple. Let’s start with Get Method. Type using block again and create your TContext instance. Now we have return statement return context.Set<TEntity>().SingleOrDefault(filter); It will bring us single data. You do not have to memorize it. You will get used to it when you have done lots of projects. Now time to type our GetList Method. Type using block again and create an instane from your TContext. Here is little bit tricky return filter == null ? context.Set<TEntity>().ToList() : context.Set<TEntity>().Where(filter).ToList(); If filter == null we will return whole List. If filter has something then we will use that filter in Where statement to get the specific data from database.

Press enter or click to view image in full size

We have created our class now we are able to use it. I assume you guys have Northwind database. I am going to use it to show you guys. Use EfEntityRepositoryBase to create an instance. Define TEntity and TContext for you and use it as well.

Press enter or click to view image in full size
Repository Pattern Example

As you see we are done here.We impelemted Interface and uset it in a project. I hope You guys like it. Until see each other in another article take care guys and do not be dependent on the code…

--

--