Repository Pattern using a web API in .Net core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Hello friends today I'm going to show you how you can perform a crow operations on an API using repository pattern so what is a repository pattern a repository pattern is like it's uh we have interfaces we have their concrete classes and we would be inheriting from the interface uh so the business logic generally doesn't lie in the controller section it goes to another section where uh you have all the insert update and delete and search options there and then we present it to the client let's have a brief look at it repository pattern in an API what is a repository repository is a design pattern is a general reusable solution to a commonly occurring problem within a given context in a software design a repository mediates between the domain and the data mapping layers using a collection like interface for accessing domain objects now let's have a look at the graph so here we have a client which interacts with the controller action and that interacts with the repository with uh the repository is inheriting from the interface or the rules it follows the rules and then it connects with the Entity framework or the DB context that's the connection string so after connecting the database uh The Entity framework we reach the database so finally the database is reached after the repository so the controller does not have any direct Rel with the database or The Entity framework it will create another layer between the controller and the database let's see that in action we are going to create this with an API so as you know I'm using visual studio 2022 so you have to select the once you create a new project you select the asp.net C web API so that's the one then you create a next you add the name the location of the project then you go to next and uh you need to select the version that you are going to work out on net 6 soet 7 according to your choice https I'm not taking https but you can take that uh if you are making it for a live project uh create will have a new project with only a controllers folder and uh no other folder you wouldn't have the other folders here once you have the project it would be a blank project so you want to add uh the models folder the repositories the data and the controllers that you need so the first thing that we are going to do here is we will set up the new gits okay so we go to the new package manager managed new packages for the solution from the managed new packages uh you need to select the first one should be Entity framework core swashbuckle would be installed by default in net 6 so we need these all uh uh new gets so Diagnostics do Entity framework core microsoft. Entity framework core Entity framework core. SQL Server Entity framework Cod or tools okay so these are the required ones once we have uh all right just make sure that when you are installing ining it should be the latest version of your project so my project is net 6 I should select 6.0.2 two which is the latest version of 6 not seven okay the LED version of six uh Diagnostics uh is also okay uh swashbuckle it does not relate to the projects uh version so you can vers you can install it uh any of the latest versions uh the remaining ones it needs to be with the latest version of the project okay now once you are done with this one we will go ahead and set up the app settings. Json we would put the connection string over there once the connection string is set up we go to add a new data folder I'm sorry we will go to create a model so we will add right click on the project add oh come on I'm sorry uh we need to have the models first folder is the models we need to create the model class first so patient. CS here I'm using a patient so this is my table class or the model class you can see these are the columns that I'm having in the table once you have the uh model class here we will have the data folder add the data folder and a class file named application DB context which inherits from the DB context Base Class and in the Constructor you need to initialize uh this D context uh options base options and below that you mention the table name which is going to be related with the database so public virtual dbet uh patient and name it with any patient name or anything and that's going to create your database so this is my table which is created so patients is the name which I provided here and after you uh do this DB context you need to fix the program.cs without that it won't connect to the database so program.cs uh should have your um connection string with a default connection name so these are the two lines that you are going to put uh in the program.cs so that it connects to the database once this thing is done uh we need to have uh yeah the DB context the model class and the program.cs three things uh already done so next uh remember that the application DB context remains inside the data folder and the model class remains inside the models folder now we will move ahead to the repositories we will add another folder name it as repositories inside the repositories we will have two new subfolders one is implementation and one is the interface first of all let's clear the interface so the interface will have uh right click on the project add new class and when you are selecting a class select the interface not the class by default the class would be selected so you would be selecting the interface one uh the interface and it starts with an i that's the general naming concept you can put any name but uh it's suggested that you have interfaces with the beginning letter as I so mine is I named it as I patient and that's also inheriting I disposable so I disposable will inherit uh will Implement another method named as disposable which is going to dispose your connection string or the DB context class okay so we require that one so inside the interface I have one 2 3 4 five five new signatures what are the signatures the first one will give me all the records get all patience uh then we have uh one of the model type get patient by ID so single record next one we will have create a new record next one one we will have an integer type update a new record why I have this data types integer because these are going to return me an integer value and depending upon that I will decide if it's a success or if it's a failure so int delete patient and it requires an ID once the interface is designed you need to create the concrete class or the repository class so that will inherit from the interface now you see when I'm inheriting from the interface I patient by default it created another method named dispose okay it will create this one by default because I patient already inherits I disposable so dispose is a method from I disposable now when I'm implementing all the remaining um methods here I will Implement all those uh methods we will have to create an uh an object of the application DB context call that in the uh dependency injection in like uh using dependency injection and instantiate or initialize it in the Constructor then for the create one I will receive a patient type of data I will Mark a variable named result as minus one one if patient is null first I will check if there is any data inside the patient if it's null then I will return a zero else I will add that patient value and save the changes and return the ID of the patient that is being created as the result now this is the integer value that we are returning back similarly we will also create one for the delete and one for the update uh and one for uh for the select we will uh show I inumerable because this is going to send multiple records okay of patient type now uh for patient by ID we will fetch the ID we will use the Entity framework uh the linku statement to get uh the first record I have used uh the Turner operator here so that if it's a null it will get some value if it's a null then it will return null now it's returning the value of y which is stored here for the update I received all the data that are to be uh fetched that has to in the patient and according to that uh I will fetch one record from from the database using that ID and then whatever is received I will update with that one okay so you have to make sure that whatever you are updating it has to be the old format if you're changing just one or two columns that column remains changed and the remaining ones remain the same now after this one I will return the ID that has been changed else I will return one minus one okay now these are the concrete classes or the implementations of that interface now after this implementation of the interface we will move ahead we will create a controller class named anyone anything of our choice so mine is patients controller in the controller class we will not instantiate the application DB context we will have no relation with the application DB context we simply created an object of the interface we will interact with the interface so an object of the interface is created initiate instantiated or initialized inside the Constructor now in the index the first method I call is index that's recording ing that's giving me all the records from the patient's table and you can see that I have created a variable and that's of the object type of the repository dot get all patience this is going to connect me with the repository the interface object is going to connect me with the repository and get me results so if I have anything in it then it will return me that if not then it will return not found similarly we will go to the get by ID we will fetch the ID we will pass that as a parameter to the repository uh with get patient by ID and if you'll see that anything null is returned we will say not found if something is returned then we will display that on the screen now create I created as creates because I have some uh naming convention because create is used inside the repositories why instead of having a duplicate um uh action I I tried to make it a different one so I will receive an object of type of the model class I will receive the data of that uh model type and I will uh pass that one to the repository and I will check if it is less than or equal to zero if something is received then I will say that it has been added if nothing is received then I will say it's a bad request and a message has failed now same goes for the edit and same goes for the delete let's see that in action by running this one so as of now I have only one record in my database you can see that that's the 24th record I have it in the database so first of all we will create a new record and then we will try to update that and we will delete that okay so ran the program it's still running so I will wait until it comes up uh hopefully we already created different folders for different uh classes repositories has implementations and interface in it models has the model class in it um data has the application DB context in it and uh controller has all the controllers in it program. CSS separate app settings is outside okay we received that in Swagger because that's the default uh loader in6 now let's try to get uh let's try to create one so we will go for the Post request it's asking us for all the parameters we will not provide anything for the ID because that's a auto increment uh column first name I will provide suppose let's say um Sandy and in the last name I'll say pierce the age I will mention 56 and the address I will mention USA the patient type I will mention as the inpatient in the bed number I don't need anything I'll just put it blank and diagnosis I'll put blank now let's try to execute that this is going to connect with the repository and give me oh we got an error what is this error unable to resolve repository pattern interface while attempting to uh it's trying to get service okay so you see this is a message of service what we need to do here we need to go to the program.cs and we need to Mark the dependency injection builder. services. add scope. add transient or add uh add scope add transient and add Singleton so I marked it as scoped and I uh put those the first is the interface name and second is the repository name so these are both linked with each other so we will uh initi the first thing we put is the interface and second one is the repository class and uh put this line of code in the program.cs that resolves the issue we will save this one and we will rerun the program I hope this one doesn't make any errors this time it should go through and give us the result so this is one kind of uh uh decentralized architecture or you can say that this is a Loosely coupled architecture which is not tightly coupled so that you change one thing and everything changes no you just need to make changes whatever you want and still the application would run uh absolutely okay and do not have any issues now let's try TR to put a record try I'll not give anything in the ID let's say this is um Robert age let's say 25 address let's say USA patient let's say inpatient B number I'm not providing any okay bed number it's an inpatient with 36 LK diagnosis medication now I'll execute that and I will see that if it goes through it will give me a success message if not it will throw me an error and it will show me a fail record it says added 25 so 25th record is added now let's execute the database and see yes successfully added now um let's try to fetch all the records using the get one so I will go with the get all the patients and see if if it's giving me all the records I executed and it gave me two records okay two records have been fetched using the index now I will go with the fetch the records by ID so I will provide the ID as 24 and try to execute that that's going to give me the 24th record and now I got the 24th record so that is also working fine now let's try to update so I will put try it before doing the update I will try to fetch the record 24th record and try to update that because I need all this data I cannot write all of it I'm going to put that one in the put and I will change this is Jennifer canning so I will make it Jennifer GES and I will change her age to say 36 and I will make it as outpatient I will remove the bed number address Let It Be same I will just execute the that and wait for the result updated 24 let's go ahead and check that in the database Jennifer Kings let's execute that Idaho outpatient ped number is removed and diagnosis as vacine uh vaccine and last name is changed okay so now as this one is done let's do one with the uh Delete so we will uh Delete the 25th record now let's go to the delete and 25th it's asking me for the ID I'll put it 25 here execute it gives me a message deleted 25 now let's check that in the database absolutely correct we found that 25th record is deleted one thing I forgot to mention so once you created the model class and the application DB context uh where is it the DB context and you have set that up in the program.cs with the connection string and the app settings is ready you need to um you need to do the uh migrations okay so add migration and update database that is required if you are creating this for the first time otherwise if you already have this project with you you already have a database you need to match the model folder with the column names okay and the data type should match if you already have an existing table so I already had this existing table I did not use the uh ad migration but if this was for the first time then you need to create uh these few files and then you need to put the add migration and uh a short comment like any or something and then you need after this one is a success it will generate a create table script and after that you need to update the database so these are the two commands you need to pass if you're doing this for the first time and your table is a new table otherwise you can just follow the same process and uh uh everything should work fine all right guys so I'm going to put the source code in the GitHub repository and share that with you uh if you have any questions any doubts you can just put that on the comments and I will be more than happy to answer you and uh so this is just a simple one we will come up with uh another distributed architecture I will uh separate all these folders into different layers and we will also have a generic repository coming up so until we have our next set of programs stay tuned stay connected and happy coding thank you
Info
Channel: Fired Developer
Views: 486
Rating: undefined out of 5
Keywords: Repository Pattern in dotnet Web API, .NET Web API, Repository Pattern, Coding Best Practices, .NET Core Repository Pattern Tutorial for Web API, Implementing Repository Pattern in C# Web API, CRUD Operations with Repository Pattern in ASP.NET Web API, Building a .NET Web API with Repository Pattern, Understanding Repository Pattern in .NET Web API, How to Implement Repository Pattern in .NET Web API, How to Build a .NET Web API Using the Repository Pattern
Id: 4i6Ymi7yBBk
Channel Id: undefined
Length: 17min 21sec (1041 seconds)
Published: Mon Sep 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.