Repository Pattern

Recep Mert ASLAN
5 min readFeb 10, 2019

--

Hello Guys,

In every Big or Small projects we perform database operations such as Insert Update Delete and Select. If you have a huge database and You do some operations for every table in that database you must avoid of repeating your code. There is a design pattern for this. That is Repository Pattern. Repository Pattern provide us to manage our database operations in one source. Repository pattern uses for accessing data because of this we should use it in Data Access Layer.

What I can tell you as writing is just that. Lets coding what I have Explaind so You will be learning more than my writing. Lets get started.

Firstly you should create an Interface. Lets name it IEntityRepository and make it Generic. I assume you guys know Generics. Briefly Generics define by using <T> after interface name. T comes from Type. T represents variable name. It is just a name You can rename it like RMA or EG what ever you want.

I will be changing our interface little bit more in this writing end. I do not want to do it right now because I do not want to confuse you. After created our Interface we might want to do database operations don’t we ? Those are very small operations. We will write 5 methods to do that and it will do every database operations in the end.

Lets create adding, deleting and updating methods. I will declare them as void because we do not have any return value. In an Interface You can only declare methods body. There is no code in those methos. Write void then give a method name and open brackets. Write “T entity” between those brackets. Do it for 3 methods. T represents a type which might be Product, Customer or Category. Entity is a variable for that Generic Type.

Now We will be doing Select operation. I will be doing it in 2 pieces. First piece will return us a List. We will get data from database according to the values that we provide. Second piece will return us only one value. Lets define them. Create a method and give a name as GetList. This method has List<T> return type. For example if we give Product for this T our return type will be List<Product>. I will write an Expression for this method which provide us to write Lambda expressions. This is where you write any queries in one method. It provides us to access data by writing different queries. For example If we want to get data which ProductName starts with Chocolate or ProductId is bigger than 3 We have to write a different method to provide that if we do not use Expression. By using Expression it is easy peasy. One method can handle every different queries. Expression is not enough alone. We should also use Func Delegate. Expression and Func Delegate are Generic Types. In here Func takes 2 parameters. First is our parameter tpye and the second is return type which is bool here. the T type It might be Product or Category or Supplier does not matter what we sent into it. You should write it like this. Expression<Func<T, bool>>. We should write a variable name for this Expression which is filter in our method. Filter represents our query which we will send into it. I gave a first value for filter which is null in the beginning because user might not want to send a query into our method so it must be null in the beginning. If it is null our method will return whole List. if it is not it will return what we want.

Now lets focus on second piece. It will return us only one data not a data List. Lets create a method. This method has T return type and the name is Get. We have same Expression here except the filter is not Null because we want to get one specific data and it is possible when we send a filter otherwise use GetList to get what ever you want. Generally we have finished our Repository Pattern definition. In the beginning I told you that we would change our Interface little bit more. Now time to change it.

We will define constraints for our Interface. We define a constraint by writing Where next to our Interface name. Then we write our type name to explain for which type I want to defina a constraint. In our case it is T and write colon next to T. It would be like this Where T : it means I have a constraint for T. If you ask me Why we define a constraint for our Interface I will say that we do not want to send unwanted parameter to our Interface. What is this constraint ? This is Where T : class, IEntity, new(). Class means reference type, IEntity means if you want to send a type to interface It needs to be implemented from IEntity. I wrote this because string is a reference type and if I did not write IEntiy constraint our Interface would accept string type. It is unwanted situation. New() means your type can create an instance so you can not send an abstract class even it is implemented from IEntity.

Generally it looks like a long writing but we have created a Interface which has 5 declared method. I just wanted to explain Repository Design Pattern in details. I hope I wrote this writing clearly for everyone gets it easily. In next section I will make an example by implementing this IEntityRepository to a class. I do not want to confuse you so I will write that example in next section. If you say it’s enough for you then take care and do not be dependent on the code…

--

--