how to implement Curd operation using dapper in net core web api

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's get start Demo First so this is a web API using Dapper and also we'll use a transaction in later but here we Implement card operation in the using paper so here I just Implement get so let me try execute so once execute I will get that all the result using the Dapper get API and then we if I want to post so let me post it try and post so once I post post I will get that whatever ID is created in 43 and that I can get by that 43 as well 43 so yeah that 43 data is available now if I want to update something L to 43 so I can just update as well using Dapper so let me change name it uh change name it and uh also put the that 43 ID 43 ID and then execute once we execute we can rify again over here let me see yeah so already is updated with 43 and if I want to delete so we can easily delete that 43 ID so those are things we will Implement uh with this Tapper using net web API so I will show you the from the scratch how we can do that and this is a new series so many things we will do in this series with the depper like how we can use multiple transaction how we can handle the transaction as well as how we can use reposit pattern with the you know deer with the Dapper so many things we will do in this so let's get start implementation so before implementation I'm especially recommend if you first time visiting my channel so just check out my um my net uh net real word example YouTube channel here you will get you know other also net core web API and many other example over here you can just check and check out so here you can get you know many many other things like how we can easily handle that expression methods and deposity pattern and unit of work everything actually we have done so you can just check out and put the learning prospective so uh yeah so before moving uh further discussion let me start the implementation so first of all I'll just create a new application with the using visual studio 2022 and uh let me create a new project and I'll choose that net core B API choose next use here just as a name it um Dapper B API and next use that see I'm using net 8 long-term supports and just created as the default setting we have used now our default template has been created for uh B API so as of now here's nothing just we have a default controller so we can just we can leave it as of now there is no harm so first first of all you need to know two packages for using depper so let me import that two package what are the that package first of all I'll just use the Dapper you have to use that Dapper so this is that one deer in apach license is apach 2.0 which is a free we can use it and uh let me zoom in for you so that you can see the what is that package we have using by by the Sam saph they actually created this Dapper package I'll use the Dapper and let me install this now second package is once you search SQL client so you will get this Microsoft data SQL client so you have to install this one so I am just using this one so that our connectivity can easily uh get it accept it now if two this two packages done then moving further to implementation so first of all what I will do for I'll just create a data folder so that our data can manage over here and let me create a let me create a data folder over here and here I just need to create a one data provider class over there so I'll just use first I'll just use interface name interface would be DV connection provider once I created the DB connection provider what I will do I'll just you know create a one method to actually stable our connection so yeah see I'll just use that idv connection which is in Port system. data and I just you know expose that one method and that method also will Implement in our concrete class so let me create that class implementation class go to the Deer uh class in this data folder and use that uh same name with that uh implementation class add and here just import that I data idb provider provider connection and then Implement that now here uh where is simple and straightforward implementation would be over here simply what I will do uh I have to create a Constructor so that I can get that connection string over there so let me create a connection string and here I will get that connection string so let me use that IE options I options and bring the name space this one and then you have to just pass whatever connection thing you want so I'll just create a uh a class to store our connection string so as of now we don't have but yeah we'll we'll use on a little bit later and make it that options and simply I have to assign this you know in our Constructor using dependency injection so let me create a field and assign it to over here and you can just remove it over there dot value that's it so this is done for this you know getting the connection string from this you know using the dependence injection let me create this connection string class as well so that we can get this one in our here so let me create a setting folder over there so that all the setting folder we can use it over there settings and create that class and here what I need to do I need to create a you know our connection string name over there so let me create that and as of now we have one connection string so I will use but yeah upcoming session we'll use you know multiple connection how we can estable the multiple Connection in Deer so just for the starting perspective I think that is fine for that one connection as at a time and uh now finally what I will do now uh just that is go to another DB connection provider now should be bring the name space over there which just we have to create it and now we have to implement our create connections so let me start that one it is very simple first of all what I need to do I to create a variable over there let me name it as connections and here I'll just call that SQL uh connection and uh this one which will coming from the data client which we import already right and here whatever connection string we have I can just pass it over there dot SQL connections and that's it for here and go to that next line and we have to open this connection so first we have to uh you know use the SLE client and SLE client has a extension method we have to open so that whatever connection we have to try to open and then simply return that connection that's it so this is a simple straightforward implementation for the creat a connection connections with the idb connections in daer so once is done let me move it to our repository class so where I can just use our you know insert up to delete all the transaction things we can just Implement so moving forward what I will know what I will to what I will to do over here I'll just create a uh repository Repository repository uh folder over here and here I'll just create a interface inside the repository I will just create an interface so that our service can interact only with the repository abstraction so let me create a interface over there let's me call it to company repository so let me name it to company repository and add and here uh I'll just expose all the method over there like for example uh get con companies details this is a get company details and we don't have as of now a company entity model which will create it as of now a little bit later but yeah as of now just first we have to just Implement all that uh signature first so get companies all the details and get company by the ID and the other one is create company so this is a create company simply uh return the company as well as whatever create how we can create the company so we have to pass that entity as well and uh finally uh the other two things we have to implement uh we have to declare here as well update company and the delete company so this is done for declaration all the signature so these are the transaction our uh we can maintain where like gets all the details get by ID company create company update company and delete company that's it and now pending to you know create this company's entity so let me create this company entity as well so I just need to create one another folder that is called entity model right uh yes and uh let me create that name uh class entity model class over here like that and here we will get more properties so these are that you know Common uh property for this company like ID name address and also country and uh we can use know the relationship as well but uh uh that will be do later but yeah as first I'll just create you know that uh that one over here so that in upcoming series that will help for us like to understand like that relationship how we can easily maintain like uh all that how all the company how many employees we can get together so those are things we also will do but yeah let me create that first entity and now another entity we have to create this employee model so let me create that EMP employee entity as well uh class create a employee an employe also have you know some uh property along with this company for en key relationship so these are all the properties like employee has ID name age and position and also which company has a which employee has a which company right so that also has a foreign ke relationship over here so that is done now moving forward moving forward to our you know repository implementation so I'll just Implement whatever we have just signature class right okay first we have to bring the name space of the company let me bring the name space and now we have to ready to implement our concrete class for this here so let me create a concrete class to implement I company repository here make it name is a company repository add and here I'll just Implement uh all the all the operations so first of all I will just create a Constructor so that I can inject you know our connection string or know connection which will create a DV connection provider right so so we have a DV connection provider what is the name of that which is name is ID contact provider right so let me inject that one in our company repository uh go to that company repository here and let me call as a factory or whatever you things you want let me call it to that as a variable and then we have to assign in this Constructor so that we can use in further our implementation so that is already to initialize over there and now let me Import in inherit that I company repository and this I company repository have many method right which we have to declare as a signature so that we will Implement over here so first I will Implement uh with the order like see a companies and get company by the uh ID so that I'll just take it at top so that I can implement as the sequence so first of all it's a very simple uh I need to create a query so which will return our companies so let me create a query like that where query I just simply you know query just I set SQL select star from that company right whatever our company table I just create a query and then what I will do I'll just create a using block and inside that using block what I will do where connection I have to use that you know I have to open that connection so how we can that which we already have done in this DB DB connection provider right we have just create a con establ the connection so call that DB connection provider and that create connection method that's it be nothing to do over here so it will create a connections over there and in in that connection whatever things we want I we can use it for example let me use AIT connections dot see here uh we have many many method over there for example see these all the extension method provided by the depper see this is a depper right so we have a query query sync query first query first and default execute query many extension method actually provid by the deer so those are will be use in this series one by one don't worry about that every extension method we will use as per the requirement so here I need to use that query query a sync uh a sync method here's the two two way actually query without a sync and also a sync so I will use that a sync method and I can pass it also as a whatever know generic actually is a generic exchange method so whatever entity you want to return so you can just pass it to as a name so I want to return as a company which we have to create it and pass it to query that's it and simply we can store as a where companies as a variable so that whatever is a data return over there we can store it here and simply I can return over this returns as a company but see we just returning as a you know I numerable so so we can just simply companies dot two string to list actually we can we can return as a this but yeah ideally it is returning same things but we don't need to do that but yeah just make sure like return as a list so that's why I just set it a list okay similar way I have to use uh there a query see I using this query we can get the result right but yeah later on we have a many options to use that views how we can use the view how we can use that procedure many things and also we'll use generic repository how we can handle instead of this using the generic repository that also we'll do in this upcoming Series so we'll do one by one so so that you can easily digest so let me move forward the further implementation for the get Company ID so how we can do that again it's very simple similar again I have to just pass the query this is a simple query select start from that company and we are ID with this whatever ID we are passing right so we have set this over here and now again we have to do same things whatever we have done using this connection string and let me just do similar way using that connections and then create connections and use that connections and use single now see this time I using another extension method uh which is written as single one so also we have extension method which will return single query single or default sing so so it will return return only single data with this parameter so I just pass that query whatever query I have are along with this whatever ID I want right means as a filter so that ID I can use it over here and upcoming session also I'll show you like how we can use you know others way to pass that parameter like uh how we can pass as a dictionary how we can pass as a know a dynamic parameter so everything we will use not issue and it is a syn AIT so what I need to do simply you have to use that as synchronize and that is also done now move it to next here what I'll do I'll just create another query for the create uh company so simple we have to create a insert query so insert into company whatever property we want to insert just insert uh the values and pass that as a uh you know property uh as a for parameter and finally what I'll do I just return as a whatever know last identity generated for this insert query after that that I will return as well so that is a simple query of SQL query and then what I will do again we have to use that using statement to create a connection and call it to that so I'll just not writing again again so that I'll just using this you know existing code so using that connection again stable the connection remove that and uh what I will do simply connection dot this time see I'll use that execute escalar so that in this execute scalar return as a whatever the last identity and single you know uh it is option actually whatever you want like this uh you want to return as a integer you want to return whatever things right so it is a gen genic extension method so I'll just pass it in int because I just returning as a end but if you want to return something else you can return not a issue and simply make it to a syn of it so that we can resolve our error simply I just pass the query and also see we nothing we have to do over here uh to you know there's a many way actually to pass that parameter so simply whatever our we have entity we can simply pass it over here direct like this way but make sure whatever object and whatever entity we have that should be a same name which we have to Define like for example it is a name capital N right and if you go back so I just as a paste as a value as a value parameter like as a same name so that should be match then it will work if it is not match that it should it should not work right so for this actually there's another way also so for first you know directly you can just pass the entity can just you know you know pass the value as well like it's a single entity right there's another way also I'll I'll just for the knowledge knowledge prospective show you how we can use another way like see I can just create a parameter as a dynamic parameter and here we can specify suppose I want to pass something else like name uh for example one two name example 1 2 three right but here we can just use same name name 1 to three and here you can just whatever data you can just assign over here as a parameters so like this way actually also you can do that right so I'll just keep it over here I'll just save it so that now uh if you required you can use this way also and there's another way also uh we can use that also I'll show you uh for example dictionary so let me show you how we can pass the dictionary as well so simply what you have to do you have to create a you know query parameter okay and here you can just pass whatever name so if you want to name so just pass it to name over here uh for the value parameter and suppose you are using 1 2 3 so just me pass it to 1 2 3 and whatever object value you can pass it to as a object value for example this dot name like this so you can just multiple uh uh object also you can use over here and you can pass it to this quy parameter to over here like that okay so this is a way actually just for the knowledge perspective I just show you many way actually we can pass as a parameter but this is a fine way actually right because if we have entity same entity which we have store in our table right so we can just pass as an entity so that is fine it is a clean approach so that why I'm using this way okay so moving forward forward forward now we have returning um Company ID whatever last Company ID we just again adding over here and just uh return to the uh this company model right and now uh moving forward to that update one so let me implement this update one also so it is again uh we have to create a one query to update query is like that let me bring back in a second line so that you can easily see so query update company set name whatever name address address country whatever country and where I want to update this whatever ID right based on this we have to do that and again there's a same way approach you can use um many whatever parameter type you want uh to pass you can use that similar way but yeah I will use uh that common way whatever we using that entity model and here what I need to do let me fix first as syn of it and create a connections use that execute uh see there's another extension method execute ass sync and here I just need to execute I don't want to return anything so that's why I'm just using execute and is the execute whatever query they want to execute with this parameter they will just execute or return back to if any exception they will occuring the exception so like this way we can Implement update and then finally I will an Implement for that delete so again I have to create a delete query for this delete SQL query so this is a delete SQL query over here like delete from company we this ID and based on this I just Implement again create a connection uh connection and create connection provider and again exe I will use that execute uh extension method pass it the query with the ID and that will be delete our data so see these are the way actually this is a simple way we can use a deer to implement our you know insert update get and get y ID implementation so that is done once is done so what I will do I'll just create a service that service actually inct this repository and that service will be I know connect with our controller we have actually abstraction layer of the service layer so that whatever business things we have do in this service layer they don't need to do any I know implementation expose for that you know uh to client so that's why I will use as a service layer so let me create a service layer uh let me create a service folder service services and in this Services what I will do again I'll just create a u interface this this is a actually the common approach we use that interface so that uh we can abstract whatever service things we are doing in our controller so let me create interface for that service layer and create I company service and here again we have to expose some of the method me so that uh whatever method you want you can just expose over here as a service layer and see this service layer um just passing the you know uh that view model right and the service layer service actually inct with the depository whatever you know that things actually whatever business logic we will put inside this implementation of the service layer so that our client does not know what actually going on and what is that real imp implement ation and they don't bother about that in know implementation as well so what I will do simply see uh as I see uh as a company or as a service we are using that BM model so I have to create this BM model again in our client side so let me create a view model CL uh folder uh view model and inside this view model I need to create that model so create a class as a view model over here and uh we have to use whatever property we want right so let me create a few property of this company view model so we have ID name as a as a as as like a same uh entity model right but yeah we can use as a different as well uh that is not dependent only that you know whatever we using over here but for the demos perspective I'm just showing you like how we can use it and S similar way the company has a know relationship of the employee right so so that also will maintain over here so that I know for the further series of session I can show you like how can interact with the employee data with along with the company or company how many company have a how many employees so those are things we Implement so that's why I just created again for this you know employ model class as well inside the view model so let me create that one to so this is again just create employee model class and it have also same kind of relationship like those employee informations along with the whatever the uh company forign key relationship right so that we can maintain over here so once you've done go back to your uh service layer bring back that name space here so can we can get rid that error so this is done or all that uh service we have just create a signature or as a declaration let me implement this as a concrete class so inside the service folder let me create again as a call it as a company service and just add it and now here what I will do simply I will just inherit that company service so that whatever Implement whatever service we have to know declaration so we can Implement over here let me Implement so before implementation start what I to do I just create a Constructor and I have to inject in our repository right which repository we have to do over here so my repository would be over here company repository right so company company repository and here company deposit just seted the variable and use a Constructor assign and that is done now we have to start our implementation so again we'll start implementation of get it from here get it from here and take it on top so that we can uh start implementation is a sequential so let me start for this company's details so first of all take it that company repository and remove that and that company repository do get companies right all the companies that there is a method right get all companies and that get all companies we can use over here as a return and so that what I will do as a store as a companies and uh see it is a just a syn a synchronized method so I'll do as a wait and once I await so this companies what I will do is this company returning as a a company entity model right but in this here you will see that the service returning our view model right so here we have to do some our business logic right so that's why we have to use our mapper so what I will do I will use as a auto maper so if you're not aware that how is the Implement automator you can just check out my uh old session where actually I net core API with the repository pattern I just show you showing you like how we can use that uh Auto maper easily so as of now just I will use that auto maper and this for the auto maper what I need to do I have to import a package this is called is you know this one so first of all I just import this uh package I'm not going to detail but yeah I'll just show you uh and also I have to just create a sign and a field so that I can use so see the automapper has been installed and just assign in this Constructor so that I can use so why I'm using the auto simply I have an entity model but that uh service will return as a dto right as a view model so what that automator will do automap will just easily map without any you know expandable uh expansion or you know extendable uh method to map our entity so it's simply whatever uh mapper you want so like just entity to model model to ENT it will just easily map automatically their side so let me show you let simply I just put as a uh response response and then how we can map uh simply I have to use that mapper do map and here see which actually destination you want so I want to destination of this you know company model and what is the source source is our entity right so that I just added over over here see this is our destination and this is source so I just pass as a source this guy is a source and this is a destination so they will map automatically and finally I'll just simply return to that response so this is done but one more thing actually we have to do uh uh see what actually it is asking it is asking it is some error okay so it is saying as a I numerable right so we are returning I numerable but it is just simply returning that you know as a single entity right so what I need to do simply I can convert it to as a pass it to iable so that they can map and easily as a list and then simply return so this is done now one more thing actually once we use the mapper we have to do one more things so that I have to do over here so what I need to do go to that uh before the program start file what I need to do I need to create a one class mapper class so let me create that mapper and here what I have to do I have to use uh one class to create manage all the mapping so let me create that class to map all the D to entity and entity to D so using that mapping profile and here I have to use that profile this is a best way actually use that uh mapper so profile and finally Constructor and in this Constructor what I will do here I I have to set actually to which map I want to do so let me show you so that you can better understand so what I'm trying to do over here simply I want to map this model to our entity model right uh this company so that we have to specify over there so that they can understand while mapping you as you just forcefully tell what mapping you want to try to over here right so that's why actually this class to help to map so what is we are doing the create map and I want to map company company what whatever company uh entity this is a company and then uh our company model right so whatever view model name is our company model so this is the way we can easily uh set it and same we can use for that create uh we have another model right see if you go to company model so there is a one model of company model but inside again we have a employee model which will you know manage with this employee entity so that also be up to map so that we can avoid that exception while that so employee and similar employee model and there's a one more method actually reverse One reverse map so this will actually helps to uh vice versa as well like suppose uh you want to company to uh company model you want to entity to entity model entity to dto and dto to entity also it will helps to this extension method so that's why we have used this one so similar way I can use for here that's it and go back to your program. CS file one more thing actually while mapping if you map this one so this mapping also be have to do something in the program. CSL as a registration so that they can net core can understand okay uh we have registered the mapper so what you need to do over here simply you have to expose something uh for example if you go to here and uh before app. Builder what you have to do simply just you have to create a one variable mapper configuration and use this you know mapper configuration which is coming from here uh Auto mapper and inside this you have to put the Lambda expression and use that add profile uh you know extension and here you can pass whatever your mapping mapping uh mapping class right so that you can just import over here so let me call that name I maybe maybe it is a different name so copy this name and type up you can just pass it to that and you can just import whatever name is space and that's it and then finally this two you have to create a mapper using again that mapper mapper configuration and then finally Builder do service. add single turn you have to just register like this way so this is a way let me zoom in for you so that you can understand again so this is the way actually you can just register your mapper profile so once is done go back to your uh service implementation so now whatever mapper you want to map so easily we can map like like this way entity2 dto dt2 entity whatever things so that would be a clean approach that's why I'm showing you so let me move it to further for that another one so another one also we can use a similar way simply I have to call that uh repository and get company and that company I have to pass that ID let me fix for a sync a wait and uh simply get the data again we have to try to map so there's no additional things with we have to do similar approach and for further also we'll use for that create as well so again I have to call that repository but before that see in this create let me put as a sync in this create what I'm trying to do first actually see this is a view model right but our repository expected uh entity so what I need to do first I have to just map actually convert to model to d uh entity uh view model to entity model so that's why I just using this company to this and we have store and then will pass and then again once I get this response as the entity model again I am just trying to uh that view model so this is actually way the clean way so that we can uh that's why I'm using that automapper and simply I have to return that response that's it and now moving forward to that uh update one go to update let me Implement that first update again I have to just convert to our entity VI view model to entity and return back to our uh return return to whatever uh it is just uh true or and whatever you know things we can just return return and similar things we have to do for the delete so this is our delete simple we don't need to do anything conversion over here because it is a simple passing that uh ID so we just pass it as a call as our service uh repository and that's it so this is done let me build so that we can make sure there's a no issue so now it's successfully build there's no issue and now finally what need to do I have to call this service in our controller so let me create a One controller let me call it to controller and make it a b API controller uh not only web API controller API you can just use this service for any application like you know if you want to MBC application you want to you know our Blazer application anywhere actually whatever client you want to use simply this service you can just use it so as of now I will show you in here but upcoming session we can do many many things for other as well so let me call it to company company controller but uh it is a simple uh company company uh company controller with simple approach right so let me add this compiling controller and here we need to expose that all that uh we have to expose actually all that method over here all that Endo so first what I need to do I just use the Constructor so that our service can interact with our our here like controller so what is our service name my service name would be company company I company service and then we can initialize this as a service I'm not interacting with this you know uh repository I will interact only that service uh that's because this is actually way that approach so that I know that abstraction actually the service layer can abstract abstract layer and that abstraction we can use in over here and simply uh once you initialize then we have to use a all that end points so first endpoint would be our get all end points and here simply I will use company and get companies call that get company methods and pass it whatever company we have we simply return that company similar way I'll just use for that other method for quickly so I'll just create another endpoint get by ID end point get by ID passing the ID call that company service passing the ID and just our things okay if it is null then return not found and then if it is we have a not null then we have to return the data similar way we'll use that post method so whatever post I want to do so I can use and one more thing you if you want to use that logger so you can use it so let me use that logger as well so that you can aware that how we can use logger so use that I loger existing uh interface you can use as a logger use that company whatever class you want to use so you can set it over here and uh create an instance set a variable and create the instance so once it's done you can use as a logger as well so this is additional things I'm showing but it is not a part of any deaper and things but yeah see it is again this company called the company service pass that entity model and get the result and just passing okay just return that okay it is created and whatever response you just return back similar I will use for that put end point so this is put end point here again we expect uh form body ID and form body model as a model whatever you want to update so whatever ID first I just checking with this get Company ID method if it is is not not null then we have to move forward for the process if it is null then we return that and finally finally is for delete so this is delete endpoint here again delete company and Company ID pass similar way just delete so once is done I hope everything should work but yeah one more thing actually we have to do uh see this controller has a dependent that I company right means that abstraction layer of the company if you go to that uh company service uh let me go show you the company service this company service again dependent on that repository right so those things actually that NET Framework and in net actually you have to uh resolve the dependency without resolve the dependency you can't achieve actually successfully execute so I just just created one dependency injection um session if you want you can check out in my uh Channel but yeah here also I'll just show you like how we can resolve that dependency also uh one more thing actually still missing uh if you go to the data here DB provider right so see we also we are getting some connection string right based on this connection string we can create a SQL connection so those connection isting we have to store as well as over here here like SQL connection setting right so this configuration is still pending that we have to do so let me go one by one first what I will do I'll just create a connection string so that we can use that connection string so go to that uh app do setting here so go to that app do settings here and let me create a one connection string uh so that I can use that connection string so copy over here and let me create a variable connection string and SQL connection and whatever my connections I can use it whatever your connection you can just pass it over here as a like see data source is I just use window credentials but yeah you can use whatever in know SQL credential else so you can just pass whatever DB name so I have already DB but but uh yeah uh if you want so you can create uh yourself as well so with the with the with again you can just create a whatever DB and things you can just create a your side so this is the way uh we can set a connection string and this is a DB we have already as a employee DB which have those table but U make sure uh if you implement this so whatever entity you have created right so see if you go to that entity model so I have created a company and as well as companies so companies are employees so those relationship also you have to taken care in your DB so let me open and show you uh how I can maintain that so let me show you or you can just create a new DB as well not a issue but see I just using this employ DB and this employee DB also I have this you know company table and uh this company table have this and also has a uh employee table so those employee table has also a foreign key relationship of that company ID right so this script if you want I can I can available also I can show you I can give you actually in this repository you can check out but yeah it is a better way actually you can just create yourself this is a simple things right so you can just create uh your DB with along with this two tables and then you can use it and also I'll just mention those script in this repository once I push so you can take it and you can execute your this is SK in your SQL DB before you know running this application so this is done like connection isting done SQL script also done now moving further to your program.cs file to registration all the stuff so first I what I need to do I have to just whatever you know that connection string things right so that I have to map so let me start this Builder dot services do configure so there's a way actually we can configure our uh that class so what is our my class my class name is setting class would be the SQL connection and the SQL connection we have a just make sure see SQL connection is string which I have the same name so make sure whatever name property name you just make sure it's the same name that's why it will bind if if it is not same name it should not bind so let me paste it over here not this one copy that same name connection string name this one so copy this one go back to your program.cs file here just mention your connection string setting name and here the way you can buy find directly so I will use that Builder do configuration do get connection get uh no there's a many way actually we can use but I will use that get Section get section and here I have to pass that name of the key see it is expecting the key so what is my key name my key name is connection string so copy this one and pass it to over here and that's it so this is actually we have to bind that con SQL connection string now we can use easily where we want right so this is done for that SQL connection string and then what I need to do I have to use I have to register this you know company service and you know that other service so let me register those things as well so I can register those things here so that I company projectory let me copy that import that and also that import this guy so see that repository I have just used the adcope uh there's a many way to register but yeah add scope is fine for here so add scope I just register for this company repository and this repository and this repos and one more things we have have to do that uh that idb connection provider right so one more thing actually we have to register for that this guy so let me import or let me actually register this guy also so these three things you have to register like idb provider and what is that concrete class and I company service and I depository so this three things you have to do that once is done I hope that should be fine okay let me run and see in action so now see uh it is running successfully there's no issue once uh company let me try execute I'm executing but I'm getting some issue so that is fine let me fix what is the issue so it is saying see it is saying connection is staying passing you know some parameter right right and it's only inv variant support Global just made okay so this is issue for our you know net 8 have a one issue so I'll show you how we can fix that so if you use creating. net 8 API or any application I think it is providing see this invariant globalization true right so that's actually complaining if you go and see the check it's saying the only invariant cultur support globalization mode right so this is actually creating a problem in our case what I need to do I have to just false so that uh we can successfully run our application so if you go back let me stop this application and run it again so once you run let me execute one more time let's see what happened this time now see I'm getting data because they already exist the DB and DB have a data so that's why we are getting the data and if you go to company create a new company so for example I want to create a new company uh for example my new company right and address would be uh Delhi and Country would be India and we can keep it PR up things and we can remove as well not I show so let me execute once you execute at the name okay see uh actually we have changed some name right for the showing the example like okay how we can pass the parameter so that's actually complaining because what I am sending as a name that name is does not match right so that's why it's complaining so this is a good way to show you like what how we can map that our correct things right so go to that your Repository uh where is my depository go to my depository go to the company deposit implementation go to that insert method and here see I just passed extra name right but it is if it is your name also same so there is no issue but yeah uh that's a issue right if I not match whatever parameter I'm passing so once you execute stop run it again let me run it again uh yeah post try my new company and the company is a Delhi and Country would be India and execute once you execute see it just get loaded and created a new company with 44 also you you can make sure in your DB so if you execute your query select star from company you will get the data whatever data is created last one see this is the last one you can see over here which is created so that one is created now moving back let me get it over here as well uh what is that ID was 44 so let me get this 44 using the API now you also get the data and if you want to update something that 44 you can update as well 44 and uh if you want to update something for example new company to Old Company right so let me change the Delhi to G and also country would be India to something else like or India or keep it India as well India so once you add execute it should be update so let me just make sure uh verify perspective it is updated or not yeah this is updated if I see that uh data in the SQL DV also it is updated with the goodgo and also India and let me delete as well so if if you trying to delete 44 it should be delete if you go to the DV it should not be over there see the last record also has been deleted so this is the way and uh you can Implement your know insert update delete or any uh card operation in the deer in net core B API that I'll show you from the scratch everything like using the maper everything let me recap what we have done so first of all you have to create uh this DB DB connection provider and you have to C declare the signature of the DB connection and then you have to implement that using that uh uh passing that u connection string and in the connection string you have to just create a SQL client over here and once you create it then you have to just create your repository and the repository you have to expose whatever you know your operation you want de uh signature that insert update delete whatever things we have to create it over here and then we also Implement one by one using that uh idb connection provider and this idb connection provider create a connection and using all the operations over here one by one and once it repository done then what I have to do I just create a service so that our client uh can interact with the service layer not in the deposit earlier so again just I create a expose some method for that client perspective and uh that also we have Implement over here with the help of the know I company repository that is I injected and also whatever method we want that actually we have interact that DB that is I calling and just using that mapper to map our entity and finally uh this company know company service actually call as a controller as a company controller which is our API nothing and this API we are just in import or just you know using as a as a dependency injection we use and also we just expose all that end point one by one and be using that service abstraction uh company service layer right and finally all of things we have done then we have to store uh our connection string over here in the app settings and before the create create a connection string you have to just create your you know DB and your SQL setup and whatever setup you want you can just use it like for example SQL my SQL and you know and other DB as well so you can just create setup and just store that connection string over here and also just create a uh script uh which I will share you but if you want you can just create a simple company and uh employee table script you can create it as a script and finally once all the things done you have to go to the program.cs file you have to register all the services all that know uh layer which we have created like company repository I company service DB connection provider and also we using mapping so you can just map and one more thing we have to over here just configured or just we have set up storing actually uh our connection string read and just store this class right SQL client so that I can use so those are things actually we can done and we can use it and upcoming session uh we'll do uh further like we can maintain the transaction as well like how we can maintain the transaction like uh suppose we have a two table company and employee so how we can just manage the transaction in together so that once one transaction can fail so next should be not execute or just once that's completed and next is failed so previous one should not execute it should be also roll back so something kind of we will use in further in for this deer Series so that we can understand easily uh step by step so thanks for now have a nice day and don't forget to like subscribe and share
Info
Channel: DotNet Real world example
Views: 661
Rating: undefined out of 5
Keywords: Curd operation using dapper, insert update delete and read using dapper, how to implement Curd operation, how to implement Curd operation using dapper, dapper in net core web api, how to implement Curd operation using dapper in net core web api, dapper, .net core web api, crud tutorial, .net 8, dapper tutorial, .net coding example, web development, database operations, curd operation with dapper, curd with dapper, what is dapper, how to integrate dapper, why do we need dapper
Id: 9ZPoScUrIGQ
Channel Id: undefined
Length: 63min 38sec (3818 seconds)
Published: Sat Feb 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.