Repository Pattern - Simplified

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
map hi guys welcome to digital tech join in today's session i'm going to demonstrate to you explain to you the role of one of the design pattern called the repository pattern now why does a design pattern come into picture design pattern provides a proven solution to real-world problems phased in software design the repository pattern is used to decouple business logic and the data access layer in your application now we'll get into the basics and we'll we will start this session by by running through ppt which i've created for all users so let's get into the the actual definition of design pattern by martin flower which is defined in this book called patterns of enterprise application architect the book is very interesting and if you have not read the book yet then i have added that link in the description please go and purchase this from amazon i am sure it will be a worth readings book so let's get back to our definition the repository pattern is a well documented way of working with data sources the repository performs a task of any intermediary between the domain model layers and the data mapping acting in a similar way to set a domain object in memory so let me break this down and explain this concept in a very simple and in a very simple manner so basically the data access layers typically contain storage specific code and method to operate on data or from the data storage the data access layer that the repository abstract can be orm entity framework or any xml file or it can be any collection of sql statement what repository pattern does is acts as intermediary between the business logic and your data access layer it acts as intermediary it encapsulates your command it narrows down the number of operations required to access with the data data layer also having repository pattern in your project enables you to absolutely take the advantage of the repository you know abstraction so you don't have to the repository pattern does not need to know whether you are connecting with your sql database or if you are connecting with your file database or sqlite or your calling some services and i'm going to walk you through some of the examples and simplify this entire process so that everybody understands what exactly is repository pattern now let's go ahead from the definition to a scenario which will actually clear out most of your queries assuming you're a developer and you have developed a mobile application as of now this application connects to your ms sql database server now you have written code without the repository pattern in place and you are connecting your services directly from the app to mssql database project is live and after couple of days your project manager walks in and he tells you that your code will also now connect to your sqlite so having ms sqlitup is there in place there is a functionality which requires you to connect to your sql lite database you write code obviously you need to make some changes and you do write some routines and connect to your sqli database now after a couple more more days your manager comes up and says that you are supposed to connect to some api and fetch some data or do some database operations through the apis again you need to make changes in your code and then again you need to publish the application and obviously you need to take care of your coding unit testing and everything and finally after few more days your manager comes in and then it is to connect to your aws s3 bucket to fetch some data or upload some data now if you see at this situation as a developer you'll have to keep changing the code because your data access layer is continuously changing its data source now this is not a desired approach because we generally need to develop anything any solution whether it's a mobile app or it's a website we need to make a solution that is scalable and that is flexible now there is no scalability over here and there is no flexibility over here each time you make a code change you need to do an unit testing you need to put some efforts then there are some exceptions that will come through then you push it on a uat production and everything this is totally not a desirable approach let us now see how repository pattern comes into picture assuming look at this diagram now what happens once repository pattern is in picture now you are now you have implemented an interface class which actually is connecting to your repository implementation actual implementation and your repository is connecting to your different data source now as just assuming that you have this repository you add this repository in place when you actually first implemented your database as ms sql database ms sql database while it was connecting to that now if you add repository pattern and if your boss if a project manager walks in and says that you need to connect to escalate you need to you don't need to do anything with your in your app level code because your your app only talks to your repository interface class and that is how you just change your repository interface class and your data access layer will take care your implementation class will take care of connecting to various data source now this has become so flexible and obviously scalable tomorrow if the fifth data source comes into picture you just need to make some changes in your interface and your repository implementation and your work is done so i hope i hope guys you understood this we are not stopping here we need we have also created a small demo application and which i've created in mvc and i'm going to show you how that's going to work too now going back to a demo application currently i've set up a mvc web application which uses this architect it is a web server it has a controller controller connects directly to the data db contest entity framework for database operations so db context entity frame is connect to your database so in our case the existing project which have set up connects to your ms sql database now what are we proposing to implement repository pattern which i am going to demonstrate right now so we are proposing this layer we are going to change this layer this level of architect and we are going to come back to this one so what are we doing here as i explained in the demo of mobile application we have a controller now it connects to your repository repository implementation db context and database so this is the proposed structure of our architect so just to finish off with the slide work so what are the advantage minimize the duplicate queries reduce redundancy of code easy to maintain you can change the data access layer without changes repository code or logic forces programmer to use certain pattern across application for data access i hope you guys have understood the actual meaning of repository and how that can be implemented now let's get back to our project so guys this is a project as i mentioned this is a a project which i have created on mvc application right now it is not implementing any repository pattern also it has a it has a dto which has which is which is customer id name and mobile uh it has a db contest class which which is referred over here and then obviously it has a controller which is actually calling the db context class directly and fetching the customer information i've created one method called get users let's show this i'll show the data and i will show in the postman what is happening correctly so this is the api i click on get and see you can see the data been populated fetched from the database now what are we going to do is that we are going to actually implement the repository pattern over here so guys let's go ahead the first thing we are going to do is we will create an interface so i create a folder interface and i create an interface since it's customer i'm calling as a creator interface make sure you select interface and say i customer so our interface class is ready okay make sure that you say public now in this interface class i am going to add three method public task and customer dto and this says get customers we'll call the dto reference over here and it's using this one okay it's come over here so this is get customer and i can also add one more method for you guys so now our interface is only implementing it's not implementing is declaring two methods one is get customers and add customers once our interface is ready let's go ahead and create the actual implementation class so we go ahead and in your data folder we create a class called customer report settery now the customer repository obviously will interface inherit interface which we created customer repository and we add this now obviously this error is doing because we have not implemented these two fixes so we just say so these are the two methods right so let's go ahead and implement this now before we implement this we need to call the data contest class over here so we say private data contest and we create we create a constructor yeah generate constructor okay so we have created a constructor which takes in the data contest class and which assigns it okay now what we are going to do is we are implemented we let's implement it get customer first so we are seeing a return await contest do list i think so we are supposed to use this framework dot co microsoft framework dot co yes still we are getting an error okay so this is a key so this method will return you all the customers now let's go ahead and implement add new customers so for adding new customers we new see tto and say customer name so we have added two parameter two fields and we say contest dot customers dot add and we say new customer s dot save changes async and then we what is an error over here sorry changes we need to use async and then we return a confirmation the calling method as customers customer details saved successfully so we have actually done the implementation here if you notice our repository implementation repository class is actually calling the data contest and as implemented to uh the routines to fetch the customer and add a new customer now let's go back to our main controller customer here if we see that data contest is currently used now we need to get rid of this data context because we don't need to directly connect with the data context so what are we going to do we are going to replace this with the i repository class so we are saying private read only i customer repository we need to call in the required references okay and let's change it to user or customer impossible let's do the same over here and and this is now we go back to our and just replace this too and we don't need to call the database we just say get that's it now this is the level of abstraction which you're doing we're just saying repository get customer that is the level of data abstraction which i was talking about so now this class controller does not know the data context it does not know this class and it also it just knows the interface and as i as i mentioned the repository class over here customer suppository is actually uh is a class which i will be do doing the actual implementation whether it is uh it is a ms sql database or it is a mysql database whatever it is it suppose this will handle all the data source and data logic uh you know operations requirements and your controller just knows the interface so this is so simply explained now let me also go ahead and add do our implementation for adding a new customer so this will be basically a post method and just copy this http post and we are going to see public sync task action is string because we are going to return string saying that the custom add customer and we are going to save us a let's go to dto and we are just going to say return okay not add okay so we are going to say customer customer repository that's it so this is what we need to do in our controller class let's run this and see the output i showed you the output before implementing the repository pattern there won't be absolutely any change in that so yes the project is now running let's get the same method again and see the output service okay let me just see yeah guys so so basically the exception was because i had not injected this interface in the class so i'll say services dot add scope and i say i customer repository and customer repository just build this yes so it's built now let's go ahead and try it again okay just close the post since i had not injected this interface into the class it threw that error so now i'm just on it and as i said click send okay i had to put a table it's not required now just remove it and then and if you can see the output is there so guys see i hope you have understood the concept i have explained this with i've explained theoretically as well as i have shown you practically how you can implement the repository pattern how this pattern which we interpret we create an interface and now we can implement the uh the actual class which will take care of the data access layer and handle all the data access operations so your controller your main class your business logic does not need to know what is the database what is the backend and from where it is connecting to get the data so i i hope this this was insightful and please do subscribe to channel and please do like this video thank you guys thanks a lot
Info
Channel: Digital TECHJOINT
Views: 2,288
Rating: undefined out of 5
Keywords: repository pattern, repository, asp.net core, repository pattern explained, asp.net core mvc, entity framework repository pattern, enterprise architecture, design pattern, repository design pattern
Id: 5HahAQ6V35k
Channel Id: undefined
Length: 21min 50sec (1310 seconds)
Published: Sun Apr 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.