.NET Core 3.1 MVC REST API - Full Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
according to the song three is a magic number well I'm not sure about that but what I am sure about is it in the video coming up you're going to learn completely step by step with no skipping how to build a dotnet core MVC REST API using dotnet core framework version 3.1 we've got a lot to get through so let's get started [Music] well hello wherever you are whenever you are my name is Louis Jackson and Who am I I'm in Melbourne Australia locked away inside my home office and when is it it's April 2020 so I hope wherever you are and whenever you are you are safe and well so as I said in that very cheesy introduction yes today we're going to be building completely step-by-step a.net core MVC REST API using dotnet core version 3.1 so we've got a lot to get through a lot cording to do so we're gonna jump almost straight into that I do want to do it just a quick introduction just to preface what we're going to do and set the scene for you before I do that though just the usual begging if you liked the video give it a like if you haven't done so already please subscribe and you'll be notified of all the new stuff I put out and if you're watching this on patreon or you've signed up to my patreon channel huge thank you to you guys as well and just a reminder your names will be appearing at the end of the video by way of a special thank you for all the support but with that let's take a quick look at what you're going to be learning and then we'll move on to a quick demo of the API and then we're going to get in cording so I don't want to see this anymore so a tongue twister and this is episode 2 season 3 for those of you interested in the chronology of what I'm putting out and this is what we're going to cover today this is the introduction so as I said we'll be doing a demo I'm going to go through everything you're going to cover right now and then I'll quickly go through the ingredients you need the tooling to follow along and then an overview of an application architecture but we touch base with the application architecture throughout the whole tutorial so I won't spend too much time on that upfront coding part 1 will create our model and our controller will also create our repository and we'll talk a bit about dependency injection and we'll also create our first two API endpoints that will respond to get requests to read data or read resources from our API coding part 2 we'll move into talking about entity framework or and specifically create a DB context to talk to our sequel server and we'll revisit our repository as part of that according part three we talked about data transfer objects we don't just talk about them we write some and create some and as part of that we will use a tool called automatic to make our lives a lot easier and we'll also introduce our next API endpoint which responds to a post request to create resources at our API endpoint coding part 4 we talk about put patch and delete type requests which are involved in updating resources either in full or partially and of course delete it's quite self-explanatory and then we move into final thoughts and all that kind of lovely warm and fluffy stuff so just before I do the demo I'm just going to finish this couple of last couple of slides then we'll move on to the demo and then relaunch spent according so to follow along you'll need a text editor I recommend vs cord it's awesome and free you'll need sequel server Express or the local DB you can use any other database you like and I kind of give you some pointers or futures to do that but I recommend for simplicity to use sequel server as part of this video postman we used to test an API I highly recommend that it's free as well after doing any type of API development I guarantee you'll be using postman so you may as well get used to it and I think I've been quite conservative but seeing an hour of your time it probably will take quite a bit longer than that but say I said an hour at least initially so let's move on to taking a quick look at the architecture of the application you're going to be building now don't worry if some of the stuff doesn't make sense to you we cover it all as we move through the video so of course we'll get a program and startup class we'll be working a lot with in our startup class throughout the rest of this project you will create a model and that of course is the N in MVC and that's just an internal representation of our beta to put it simply V stands for views we're not really creating any view artifacts in this project as it's an API eivol caveat that statement a bit later though we will of course be creating a controller and controllers are basically the orchestrated of the requests that command in the house are action results or our API endpoints when we come on to rating those so and key piece of infrastructure there's a data base context DB context or the data access layer which is part of the entity framework or stuff I mentioned and that basically talks to our sequel server database I think of the DB context there's a kind of mediator between our internal representation of our models and the database it kind of maps that down to persistence layer something new in this video that wasn't in my previous API tutorial I actually introduced the use of a repository don't worry if if that looks like we're duplicating leo's of stuff here we cover that often more detail as we move through the tutorial and I'm not going to talk at length about it here again we're going to be using details data transfer objects these are simply an external representation of our internal models out to our consuming endpoints and of course you'll have a client we will use postman in this case that will make requests to our controller and get responses back via HTTP and they will have in the case of read requests and some other requests as well a serialized JSON payload and it will be serialize a serialized details that will begin passed back now in fact some of that that make some of all that didn't make sense to you do not worry do not stress we'll cover it all as we move through but for those of you who maybe have some semblance of MVC it should maybe start to make a bit of sense just suck them back to my commentary about views yes we're not specifically creating view artifacts in this project but you could argue that the JSON payload of the JSON response or just the HTTP response in general is a view from an MVC perspective I'll leave that with you to mull over and discuss at your leisure so just before we start coding let's just take a quick look at what you're going to be building and I'm using something called swagger or or peon API is it's more commonly referred to today and this is just basically uh an automatic view that's been generated for me by actually having some plugins to the API that you're going to be building and it just gives you a view of the endpoints that our API will respond to it's kind of like a self documenting language and the really cool thing is you can actually even test out and try out but before we launch into that what is the API we're going to be building well the use case is this I don't know about you but my memory is getting worse the older I get and in gray here gray Heroes and my beard and less here on my head and my memory is not as good as it used to be and with all the new tools and all the different things that get thrown at you these days as a developer I often find it quite hard to keep track of all the different command line prompts that you have to type in for things like dotnet core or darker or just even you know Linux commands - I don't know set up a firewall born or any of that kind of stuff way too much for me to remember so let's API is going some way to help with that and it's going to store command line snippets along with looking descriptive bit takes on what that command bang snippet is supposed to do and we're also going to store what platform of what application that command lines and if it refers to so I can't actually tell you I actually use this I have a localized the option of this API running on my desktop and I actually use it in your life to recall commands even for things I use quite a lot so that's basically the use case we are going to be using very standard crud operations create read and update so this command here or get force get command will just get a list of all the command line plummet parameters in our in our backend so you can try it out and execute it using open API and you can see the the JSON payload and the response cords and all that kind of cool stuff we are going to have a to create commands with our API is have already said we can get an individual command based on its ID we can update an entire command or an entire command resource we can patch specific parts of a command resource or we can delete the command resource in its entirety so that's basically our API that's what we're going to be building it's a fairly simple use case I've done enough talking I think it's time you start coding all right so let's kick this off I'm going to open the S chord to star vs chord obviously and I'll bring up a command line within that and that was just control ' I'm going to change into my working directory change into season three change into episode two I'm going to create my project in here so you're probably a bit of this already but for those of you or not you can get a list of project templates by just typing donate new and that will give you a list of the different templates that you can scaffold up now in the last video well you know the last video the previous video I did on writing an API I actually used the web template which is the empty asp net core template this time round I'm going to use Web API now don't worry you're not going to miss anything the scaffold called most of it we're going to actually delete them is just a little bit that I wanted to retain so you're not gonna miss any learnings or anything like that I don't particularly like scaffold Accords but in this case you're not missing much at all so aapki dotnet new and you just type in the name of the template I don't know why that keeps jumping my apologies you have to move this down quite annoying so web api is a template we want and then the name of our project call it anything you like but remember what you call that I'm just going to call this one commander and donate Kabul Gua B and it will build up the relevant project for us and it will create a folder called commander so to open that in be s chord you can just take called our being at the folder and that will open the folder and be escort which is rather nice now I'm not going to do a deep dive of the anatomy of a.net core application I will cover off the relevant bits asn't when we need to you may get this pop up you're asking you to include any required assets just click yes you only have to do that once one thing I'm gonna do though is we're going to delete some of the scaffolded cord that we don't need sort the is part of the scaffolding it created the controller for us called the weather forecast controller I'm going to delete that because we will create our own controller so don't worry I will leave the folder there and we have one model class that we don't need either so I'm going to delete that also just what I will call out that you know there are some really important bits of the application program class is basically if we our application as an entry point and if it gets kicked off we have a main method which you'll probably have seen before if you've done any dotnet core c-sharp programming that then just goes on to creating a host using it to create default builder method on our host and that then calls our startup class so we're not going to touch program class again so we can close that down it is important but we don't need to do much more with it the startup class however is one of the spots for me it's the central hub of a dotnet core application and we do a lot of stuff a lot of work and you have to set our application up just in terms of scaffolded cord we got this as part of the Web API scaffolding which basically just means we have access to the configuration API that allows us to read in config and we come on to using that when we come to doing our connection string so I don't need to take that code and that's really the only scaffold that code that you would get over and above the empty where the API same the way those stuff that we just deleted I will talk more about both the configure services and configure methods as we move through the project rather than doing it here configure services the names quite self-explanatory though that's where we add services that we want to use throughout the rest of our application we're doing a lot of work in here later so I'm gonna leave it there for now and our configure method this where we set up a request pipeline so the request pipeline is made up of multiple bits of middleware each bit of middleware has a function and it may or may not pass on the request further down the chain of middleware or me short socket and pass the request back the order in which you add middleware is important and again I'll touch on the relevant this is a little wheel that have been added to your by default but cover that as we move through the project CS plod failed us where we set up our any references to packages that we need to build come cover that later app settings Jason's where you can set up additional config or connection string we'll go in here app settings development Jason's exactly the same it's just the development version of the same file and will only be used in a development environment again I'll come on that a bit later and the last fight I want to talk very briefly about this launch settings Jason that just sets up a number of profiles when we start an application it will start off on a particular URL as you can see here 5,000 then 1 and 5,000 for HTTP and HTTP and you can also see here it's setting up a development environment or yeah an environment as a development should I see enough of that we'll cover it as we need to as we move through the app so the first thing we want to do is create our command model so again models if you remember our representation of the main data within our application so I'm gonna create a folder called models to hold all our models we're only going to have one norm it's a relatively simple app in that respect so select the models folder you've just created and click on new file and I'm going to call our model command don't forget to put CS in to denote as a CS file and we'll get started quickly by defining the namespace which is the name of the project in the name of the folder that this particular class happens to be in and that is models cool and then we'll define the public class the models are typically fairly well in this instance it's a very very simple class it's not terribly complex will evolve over time as you will see but public class command and it's gonna want to put the commander in I'm not going to love it though and then within our command class we're just going to define a number of properties four in total and they are now a little tip if you take prop you'll get this code snippet just take tab when you take prop and it will define me this template is going to be an integer in this case which is cool and we're just going to call it ID then define a second property so again prop tab this thing is going to be a string we'll call it o 2 and then a third prop again it's going to be a string and this time we'll call it line this is going to be the command line slipper that we're going to store in our database and then finally we're going to store another string and this time we'll call it platform and that will be the application of platform that our command line relates to no just a very quick sidebar I am going to do a follow-up video where we're going to have multiple domain objects parent-child objects and in that video I will pull out the platform into a separate domain class it will be the pin object that will have a number of related command objects as children I decided to leave that at this video because I had included it the video would just be way too big so that'll do it I'll do that next time however everything else I will cover in this video in terms of rest resources those repositories all that cool stuff so just a little sidebar there will be a next video that will follow on from this one but for the moment we're just going to leave that as a string as opposed to a reference ID and with that our first and only model is complete and I think now we will move on to looking to building or repository so we were going to move on to working with a repository now and just just a quick sense checker forever that we don't have a control that anymore because we deleted the scaffold that one we do a program and start up classes cool we don't have our data access or dbcontext class yet which we will come on to it in a minute and we do have a model sort we've got a model we've got some of other bits and pieces but what we're going to do next is create a repository or a repository interface so just clear that down and go back to our application now for a repository chord I'm going to create another folder in the root of our project so NBS code click out of the whatever folder you have selected into the root just click on new folder and I'm just going to call this data you may see it called something else in other tutorials that you watch but this is my naming convention and the first thing that we're going to create by way of setting up a repository is an interface definition now this is a really important concept and it's a theme that's going to run throughout the rest of this video and by that I mean we're going to have multiple contracts that will exist in different parts of an application and the whole idea of providing a contract or an interface is effectively what you're seeing is here you are I'm gonna I'm gonna make this deal with you that I'm gonna provide this to you and what that means is if you define that contract you shouldn't typically break that contract because that's bad news basically and the way we're going to define that contract here for our repository is define an interface which is basically just a list of definitions or interface an interface definition basically and then we can implement that interface as we see fit depending on what technology we want to use from our persistence perspective or not so the idea is that the consumer of this interface doesn't really care how you've implemented it just so long as you have implemented it and that leads to a nice decoupling of implementation from the contract you're providing to your end-users and that's a theme that's going to run through to this video so by way of a repository the first thing we're going to create is an interface so in our new folder just make sure that selected click on new file and interfaces within c-sharp dotnet always begin with an eye and as this is an interface for our whole application I'm just going to call this eye commander repository in fact to make this call it repo just to be a bit shorter rather than taping repository all the time and I'll probably spell it wrong as well that's a CS so I commander reports yes cool now again the fighting the name space within the commander namespace and this time we're in data so to define it relatively straightforward public interface and you can see they are I just click tab on the available short chords that came up it will provide the name of your interface with a capped line and then you give it a name so we're just going to give it exactly the same name is a file sword commander repo no CS obviously because this is not a file and then all an interface is is just a list of the method signatures that you are going to provide to the consumer of this interface and in how that gets implemented I'll come on to that in a bit so just thinking logically we are building a REST API that's going to perform the typical create read update and delete delete operations so our repository should kind of matter those operations you can add other stuff in there if you want but I'm just going to keep it relatively simple and do some crud operations and I'm gonna start with reading as that's typically a lot simpler than doing a operations actually change our Noora positive change our resources in the database so the first one I'm going to create is the first method signature I'm gonna create is give me a list of all our command resources or all our command objects and to define that you have to define an ienumerable as the return type and it will complain because we don't have the relevant namespace and yet that's okay and it's going to be of type command again it will commit complain because we don't have that namespace in and you just give your method a name get all commands and we're not going to pass anything in now as I said it's complaining so the first thing we'll bring in is ienumerable which is again hover your mouse over the complaining artifact hold them control impeded and that gives you a list of potential resolutions to your problem so yes we're going to put that using statement in and it will complain book command as well again control and pede will bring in use commander models which is the one we want and that should resolve itself and that is it we don't implement anything in the interface we're just defining the operations or the methods are available via this interface so that's the first one and the next one will be to return a single command back to the user based on an ID that they're going to provide so return type is command name of the method will be you get command by ID and we are going to pass in an integer by ID again that's it and I actually struggled a bit with defining interfaces and originally because it was like no fear there's implementation go don't worry we'll come on to that now our interface will build out as we move throughout the video that's all we're going to do for the moment we will add more methods to it as we progress through but we're just going to do it bit by bit and build up so those are two fairly straightforward fairly simple commands that other repository should provide to any consumer of our repository fantastic so we're going to create an implementation of this interface now and we're going to put it in exactly the same place as the interface definition itself so select your data folder and click new file and we're going to call this mock and repo dots yes don't forget the CS namespace commander data and this time we are going to create a class that implements that interface so public class call it mark and I've been really careful with my typing here because I don't want to go back and have to change it and that is going to implement our interface and how do we do that well after the class name you could call on and then the name of the interface that you're going to implement and you can see here that we're going to implement I command or repo fantastic now if you use them heritance before it's a similar syntax to inheritance this isn't an heritance we're not really in editing from anything we're implementing something don't worry we will come on and use and hit it and submit later on no straightaway you can see that we've put this interface name in here and it's complaining why is it complaining well it's completing because we've not yet implemented any of the methods that we have specified in our interface so if you're saying I'm writing a class it's going to inherit or not it's going to implement from the interface then you have to implement the methods that's specified in the interface so control period to implement your interface and you can see straight away it's put in a couple of place alders the map back to our interface and it's also brought in a couple of namespaces which is very very helpful so what we need to do now is actually implement these methods now this is a mock repository so we're not going to connect our database or anything with this class we're just going to put some hard coded data in which can act it be a useful use case when you come to testing and stuff of that and again this separation of interface from implementation assists with decoupling code so again when we come to actually using this interface our application is just going to ask for an implementation of the interface it doesn't care how we've implemented it doesn't know how into that and it doesn't care so again decouples cord from our contract implementation from our contract so back over here let's implement this first let's rotate this implement the simpler one Faust and all we're going to do is we're just going to get it to return a new command and we're just going to hard code some values and it's really quite simple as that so with an ID of zero how to string of I don't know boil an egg that this is not the type of stuff we're going to put in a TP I'm just putting some data in line will equal you know boil water and a platform will be I don't know cat with pattern or something like that I don't know fight um let me call on at the India now as you appreciate when we call this method yes it will accept an ID but it's not going to do anything with it it's not going to do all the couple anything it's just going to a ton back this command object very straightforward okay and then we just need to do something very similar for our get at commands this timer is going to return a list of mock command objects back so it's to clear a variable we'll call it commands and that's the new list command objects curly braces terminate with a semicolon and then all I'm actually going to do and I don't like doing this is I'm just gonna copy this line here up to here don't really want to see me type that in put a comma after it taste another one in comma after taste another one in will only have three let's just change the data a little bit just to just to make it a little bit more realistic I don't know cut bread that's gonna keep a random one how to cut bread garish okay and a platform is I put way too much thought into this Nathan chopping board oh my goodness shoulda just should've just done something realistic and then boil an egg cut bread I don't know make a cup of tea being British you know we drink a lot of tea so for this tea bag and cup and a platform will be Catalan top actually fine cool so probably I let a lot more detail into that had anticipated but you get you get you get the idea and then of course is still complaining we have to return back our commands fantastic so again labeling the point this is a mock repository with fake data very fake rubbish data that's not going anywhere that's okay that's actually proving the point of what a repository should be and that's the eventually come on to implement doing a proper implementation of our interface actually committing inter database obviously so cool so we're done on what repository based on our command our repository interface all good so now we want to come on and actually use it in an application and the way we're going to do that is actually by creating our first controller class okay so we've already got controllers folder from when we scaffold at our project so we're just going to select that and create our new commands controller in there now you will get you'll typically have one controller power resource type so we're dealing with rest we're going to have it we've got a command resource type if we had another resource tape such as platforms you would have a controller for platforms so it's not like our repository interface what we're having an interface for an entire project our controller is a resource label so we're going to create a controller class for our commands object or our commands model so looks like the controllers folder click on you file we're going to call it command plural controller commands controller and you're typically it's good practice or you'll usually see the resource type that you are building a controller for will be pluralized as opposed to the model which would be singular so you'll see our model is called command our controller for our command object will be called commands controller all good okay I'm just going to I was gonna clear that yeah let's get rid of these just to give it a bit more just make it looking some pop so first thing is usual namespace we're in the commander project and we're in controllers I think it would be really nice if vs cord picked up this name from scratch it will do it next time because we've already added it manually but it didn't the first time and first thing is it doesn't okay and we are going to create a public class and we're going to call it commands controller okay now gets a bit more complicated than this and I was talking about inheritance in the last section and this class is actually going to inherit from a base class called controller put forward base actually and it will complain so let's bring in the namespace using it Microsoft is p.net core MVC and the controller base class as it says there is a class for an MVC controller without view support we're not having views in this case in this project you could just and head it from controller and that would bring in a MVC controller with view support but if you leave us beasts you want to in the same way when you're defining security security permissions you want to include the least amount of stuff and your application as possible to make it simple and is lean and as efficient as you can cool now the first thing we it's not the first thing the next thing we want to do is decorate our class theme with an API controller attribute now what that does is it gives us some out the box behaviors that our controller will perform and I'll call those out as we move through the video not mandatory to do this but it makes your life a lot easier if you do this you'll get yeah as I see some default behaviors out the box and then the other thing I want to define is a class label or a controller level wrote now the Rope in short is basically how do you get to the resources and how do you get to the API endpoints within your controller and in this case we're going to define one a controller level which gives us our kind of base route to our controller and then if as and when required our API endpoint or action result layer but we me add to that road and that doesn't make sense you'll see what I mean when we come on to doing that but for now we're just going to put our string in here and we're going to have API now you could put in square brackets and then the name of a actually tape controller and what that will do is the rope will in our case was common to it would be API commands because what this does it takes this string out and it pops it in here basically so you would have the wrote to our API base wrote the API commands know what will happen if you change the name of your class this route will change as well now that may be the behavior that you want it may not be again coming back to having a contract with people I wouldn't be too keen on having the name or the route of our API changing if we decided to change some implementational detail so what you can do and what I'm gonna do is take out that kind of wildcard approach and just put in a hard-coded string that won't change even if our class name changes but it's up to you you can use the other methods that's totally fine fantastic so we want to create effectively our API endpoint so let's quickly pop back over to our PowerPoint and we're going to implement all of these end points as action results in our API controller no just one thing I want to draw your attention to you'll notice that if you just look at the you are a component for the firsts endpoint and our third endpoint they are identical so you might go well how when we call that URI how can we differentiate between what will ultimately become two separate action result so how does ESP their core application know how to differentiate between the two of them it differentiates between the two of them based on the verb so the Vail taken in conjunction with the route of the URI should be unique and you can see that is indeed the case these you are eyes are all exactly the same as this one but they are different because of the Vale that they respond to so it's going to be on that amount when we come to creating our action results also just bear in mind the success chords that we want to pull back for our to read endpoints that we're going to implement now so back on our controller class we are going to create our first action result endpoint which will relate to getting all of our resources so public action result if what does it return back it returns back anti enumerable bubble enumerable off what it will return back and high enumerable of our commands not fertilized and it keeps wanting to pin any of the project which is fair enough and we can call that anything make it all nuts and I will start completing on a number of fronts first being that it doesn't know what this is or again control P need to bring in that using statement and it doesn't know what this is at the moment so control peds bring in models and it will complain because this isn't returning anything back yet which is all key let's just quickly write up the shell construct for our next action result public action result whatsit returning back again this is just the one we are returning back a single resource in this case it's a command and we can call it anything we like get command by Ikey and then - that will pass and then - JA anything you like call out ok cool and that's completing as well clearly because you're not returning anything back yet no what we need to do in order to see this action result and indeed this one are going to respond to HTTP GET request is by decorating each one with HTTP GET and likewise for this as well so that resolves that issue of how to be know which action is always going to respond to which and again looking at there's base you are route should look quite familiar this is what both of these at this point and time will respond to now as I said the URI or the route in combination with the verb needs to be unique no currently these are not unique they're both responding to the same URI and they're both responding to get which is not what we want so in fact we need to change this one just a little bit because when we come to calling this the way we're going to call it going back over to our PowerPoint not postman that is by supplying an IE so it will be that string exactly it would be something like 1 or 5 whatever to denote the ID of the command everyone to call back so the URI for this one is actually going to be different even though they're responding to the same bill so the way we do that is by going back into you there's number of ways you can do you can actually add a new route statement above this or you can just append it in here which is what I'm going to do and it's round brackets double quotes and then curly brackets ID and what that effectively does is that then gives us a route to this action as a let's see get request that will respond to this URI if I can get the fingers going and how the ID happens to be and again this would actually be an unboxing to use the number five to denote URI of that type but this that is now what this one will respond to so these know are two different end points responding to different you are eyes or ball they are using the same they are cool and I'm actually maybe just going to leave that in there because it's quite nice documentation I think and I'm gonna be just for the this one here it's going to respond to get obviously API commands although once you get used to this you will not need stuff you will not need a comment like this to understand what they're doing the cord is self documenting so again they're complaining so what we want to really do is make use of our repository to pull back data and present that back to the user so what I'm going to show you here actually is I'll Bitterman see it's not the right way to do it but it's not the optimal way to do it and it's not going to leverage the benefits of having an interface and separating out that implementation from our interface but I'm going to go back and show you a the the way that you may have made use of something like our mock repository class to a ton data back to an application and we'll then swap it out for a better approach so I'm just going to create a private read on the instance of our mock commander repo and we'll call it underscore repository and then we use the very familiar equals new mock commander recall which will instantiate a concrete instance of this actual class now it's complaining because it doesn't know where it is and you'll bring in development namespace so it stops complaining so we want to basically make use of this repository now in our get all commands action results done back all our commands so I'm first of all going to create able to hold our results or command items equals repository get all commands cool and then we want to return that list of commands back with our 206s or return okay and innocents and some selecting okay if we were doing some other logic in here to return back some other result you have all sorts of other result HTTP results that you can have a handle back I'm just going to use okay for 200 success and I'm going to pass back command items cool so coming down here we're going to do something very some local directory are being able to contain our single command item and that's going to come from our repository and that's going to be our get line by ID now we need to pass in an ID wait are we going to get that we are going to get that from here from the ID that's passed then to our action result and we are going to return that back as well we are 200 okay and we're going to pass back to our command item now just one question that you probably have here is okay where does this ID come from well it actually comes from the request that we pass in via the URI so when we use postman to call these endpoints you'll pass in an ID and it will actually be resolved from the URI now that actually comes from something called binding sources and there are a number of binding sources that you can have and again because we've set our controller up in the way we have and we've decorated that with the API controller so on and so forth there are certain default behaviors but it will give you out the box and saw in this instance when we call this endpoint via something like this the ID will be derived directly from the URI so just going back to here you can have binding sources from query from route from form from body from head and they relate to different places in a request where data could come from and there are certain default behaviors that we get with dotnet core now I will call these out as and when we may be using a different binding source default or otherwise to obtain data but for now all you need to really worry about is that this ID is going to come from our wrote as we've specified here and that will be passed in to this action result and will be resolved here now I'm sort of going off on a tangent a little bit and I've showed you how to do something not the right way but let's just see if this all works because this is a good touch point just to see if we're on the right track so make sure you have saved everything I've fallen into that trap numerous times were you doing something saved but don't net run to run it up fantastic and let's go over to postman and call both these endpoints so already have one running here let's try it again so base URI which is the just the web server that it's running on here's the route to our controller and we're not passing anything else and we've set the verb to get so what that should mean is it should hit our first action result which is down a little bit this one here so we've already got our route specified that the controller level there's no additional attributes we've decorated this with HTTP GET so as we've we'll flip back to postman and a second when we make that call it should hit this endpoint and return all our command items so let's try that now and indeed it does and more importantly if it turns back this thesis of 200 and our hard-coded array of objects now we can put in a different URI and again as you will remember our mock repository doesn't do anything with this ID it should just return back a single object and in fact it does one with idea zero in this case but again you could type in any integer here and it's going to work okay so that's all working I hear you say can we move on to doing something else no well not not quite and the reason is this our core base at this point in time isn't that big however we have still used a concrete implementation of our mock repository class within our code base now imagine diamond align our core base has grown with useless views the same approach everywhere IRA making use of the new keyboard and the actual name of our class if you then wanted to change that implementation and use something like a sequel salvato implementation of a repository or an oracle implementation of a poster you're gonna have to go through your codebase and not only replace the the chord but you may not be sure that it's going to provide the same behavior so what we're going to pivot to now is instead of asking for a concrete implementation of a class we're just going to ask for an implementation of our interface and we can then at the backend using dependency injection swap out how we implement that interface and our commands controller class in this case it doesn't care however meant that as Long's have implemented the interface according to the contract that's all it cares about and we can then do a relatively strong degree of confidence that we've implemented according to the interface that we're going to get a consistent result set back and we don't know we don't need to know how it's been implemented and if we do need to change the implementation we only need to do it based simply in one place and I'm going to show you how we can do that so just before we start coding I thought it would be worthwhile just doing a couple of quick slides on how the dependency injection system hangs together and dotnet core as well I think it will help you when we come to doing the cording so we begin with something called the service container now the service container is configured by configure services method in our startup class so you should remember that from maybe about half an hour ago and that is where we set up effectively registrations so as you can see on the screen there what we're going to register is we're going to see RI whenever anybody asks for our I command or repo interface we are going to gather some concrete implementation and in this case as you can see on screen we're going to give it initially at least a hour mock command or repository implementation and what that means is we can figure that one time in one place in our startup class and if we then six months down the line or two months down the line open Everett then they're like we need to swap out the Marc repository for a real repository we just need to change it in that one place within our service container and so within the rest of our application whenever our application asks for an instance of our I command or people it won't get given the relevant concrete implementation and so the rest of our code doesn't need to change so that's the power or that that's the decoupling aspect coming into play so in reality how that works within our commands controller class we are going to create a constructor and into that constructor we are going to expect a night commander repository instance and at that point when that constructed is called the dependency injection system will inject whatever dependency we've told our service container to provide whenever anybody asks for I command a repo and so at that point our commands control lot will actually get a mock command over for instance to play with that's it now one last slide I just wanted to cover this because it's a if you've not seen it before you might be going for is this let me just bring them all up there are three ways you can register your services within the service container add singleton add scoped and add transient there's lots of documentation on Microsoft's website and elsewhere like on stack or the floor that goes into the level detail why you would use one over the other I've put some high-level categorizations up there add singleton you will get the same object for every request scoped you will get a new object for every client request and transient you'll get a new instance every time one is requested so we're going to use add scoped when we come to doing our registration so I just wanted to preempt that conversation here and when I can't doing in the cording you'll know what we're doing so with that let's go over and start cording right so we're back over in our startup class and specifically we're in our configure services method we were going to add our registration so again we'll make use of our services collection and this is where you'll pick up the ad sculpt ad singleton transient so on and so forth but we're going to do an ad sculpt and then it's very simple open angle bracket and then you just tell the dependency injection system your interface so in our case is I commander repo and what's it mapped to it's mapped to our mock commander repo now it's not going to resolve these at this point so assuming I've spelled them correctly it should bring in what we need so I command our data and that looks good so that's it that's all you need to do in the startup class that's all you need to do in configure services we're just saying basically whenever our application asks for one of these give it one of these now if this changes somewhere down the line and it's going to all we need to do is swap this out the rest of the kordell's from the application should have mean absolutely the same so it's very powerful so we'll save that off that's as added the necessary bits and pieces to our startup class one line not much so moving back over to our commands controller class you will remember the first thing I said we need to do is create a constructor in order for that dependency to be injected so cetyl to create a constructor tab tab to take us to our parameter list and we're going to ask for an aye commander people and we're just going to call that repository Torrey cool right now why actually also what to do is I want to render so because we're not going to use that anymore and what we're going to see is whatever is injected via the dependency injection system into this value here you want to sign it to underscore repository that won't make that equal to repository cause semicolon there is still completing because our underscore repository hasn't been created anywhere so control period and we will select this second one here generate a read-only field and that should resolve now that should or what no before we go on to test it one thing I want to see when you're using dependency injection you will see this pattern repeated time and time again and we're going to use again and by that I mean we'll have a controller we'll have some attributes created here we'll have some private read-only fields here I'm always going to use an underscore to denote those and we will then assign the dependency injected value to our private field for use within the rest of the class you will see that used time and time again and you'll see it in examples over there in the real world no assuming I've done everything correctly unless I've forgotten something really obvious I don't think I have but let's run it and find out if I have forgotten something that will sure enough complain everything's saved so dotnet run and back over to postman and we should run this and we should get the same result and in fact we do fantastic let's just check that out to an arbitrary integer fantastic so because it's running exactly the scenes before but we're now using dependency injection and we're now using a repository the way it was meant to be used fantastic so give yourself a round of applause pet quite a big milestone here the next part of the tutorial we're going to pivot away from repositories just slightly for a moment and we're going to use entity framework to actually set up a connection to our physical data store and I'm going to use a database context in which to do that so that's coming up next so just before we move on to doing any coding I'm just going to do a quick sense check if rewrites you should be familiar with these things know so I'm not going to go over them again the controllers still of what can progress but what we're moving on to now is there's data access or DB context which is part of the entity framework and as I said previously it acts as a mediator between our physical reporter user between our physical database and our models and in fact the rest of the application as I said previously and my last example the control of talk directly to this we are now putting a repository in between which you should be starting to get familiar with nodes but in this next section we're going to go over a sequel server just a little bit but it's going to make sure that's all set up correctly and then we're going to go ahead and start doing our data base context class so let me just kill that for in the moment and the first thing we're going to do before we move on to even cording is just to bring up and make sure that we've configured or sequel sailboat correctly now the tool I'm using for this is sequel server management studio which is free to download it is only available on Windows though if you're using Visual Studio you have I can avail it that's built into Visual Studio if you're not using that then you can use the command line to do everything we're going to do here I'm not going to go through that in this video don't just take too long so my sequel server is running on localhost it's running on a non-standard port double for double for so I've just took that in and you'll notice those accommodate as well if you're running on the standard port which would be I think it's 14 33 you don't need to put that in sickle-cell authentication is selected there are a number of other types of authentication mechanisms windows and then a number of is your authentication types but we were using sequel server I'm going to login with the SA account in this instance which is system administrator which is the kind of god account when you install sequel server you'll be asked to provide a password for this so hopefully I can remember mine and we'll login that will just bring up your sequel server instance which is here partner for the norm in the update and then with on each server you can have multiple databases now you'll have a number of system databases the ritual normally touch and then I have a user created database here for another I'm working on which we're not going to touch either so you will notice there is no database for our API yeah that will get created later so the only other thing I want to do is create a dedicated account for our application to use to log in and do all its work rather than using something like the SA account which you would never do although I have done it in previous tutorials but I mean good this time I'm taking that extra couple of minutes to create a specific login so expand the security folder and you don't need to expand logins you just right-click it you log in and we'll call this commander API and I'm gonna make sure I remember that in fact I'm gonna just copy it and just paste into a little notepad over here so I remember what it was and I'm gonna give it a password cool I'm gonna take this off although I think I have hid password policy maybe not gonna give any default date but there's a default database there but I'll just leave it at that for the moment and the other thing I want to do is give us a role I'm just going to give it a sysadmin which is more than you should give it but I just want this to work can I frost Titan without messing around too much but that's something you should probably go back and revisit in the real life scenario I think other than that we're all good so click OK and that should go away and if we expand this now you'll see there it's created a login so the only thing I want to do and this is a really important step and advise you all do this is disconnect and reconnect and with that user I've had a number of questions on previous tutorials for people and we're having trouble from the chord and singing chord isn't you know from the application I can't connect in and the first thing I always ask them to do is check that you can login to the sequencer performer a tool separate from the chord that you're working on and more often than not that's where the issue hasn't lied blade line so our login was commander API I'm just checking that that was the case and hopefully if I take the correct passwords it connects in so that's cool we know that our new login is all working we can connect and we can see our databases that's good so or set up and ready to move on to the next step so let's just we don't need that just no let's just minimize that sore back or what in our project what we need to do before we do anything else although you know so you can according is add some package references to our project that will enable us to use entity framework so the way I like to do this is just to bring up a terminal and I'm gonna do it from the dotnet core CLI I've brought up our CS proj file because as we use the CLI to add packages you'll see them being added into the connector the CSP launch file I do that just as I can a double check so let's go I just want to show you the packages we are going to install so let's just bring up new get again bring that over us on my other screen my apologies and the first package we are going to install is just search for entity entity framework the first one we're going to install actually is this one here a Microsoft entity framework core and if you've not used NuGet before sure you probably have you get different methods of installing it we could just copy this text and paste it straight into our CS proj file I don't like doing that I've been much rather run this command at the CLI and NC add seeing see the package reference being added in to approach file it's just a little for me a nice-looking double check that they've done it correctly so I'm actually going to take out I don't want to use the preview version I just want to use the latest version which you will get if you don't specify of a option so let's hit enter that will go off and pull the package done and then you'll see it being added to the CS proj file the next one we want is the design so this one here let's copy that Pease thing you know just clear this just so you have a bit more it's a bit more readable and I'm deliberately going to make a mistake here this is what I'm talking about see many of many Titans I've done something like that not copied the string correctly hit Enter and I've not really been paying attention and I've kind of looked at the command line going okay that's cool that's what but you can see it didn't actually add the package reference and if you look at the output is actually an arrow but if you're not paying attention you might not catch that and that's caught me out a few times so that's why I like to do it this way and watch the cs proj file being populated with the package reference so op key I'll add on the end and it should this time work there we go and adds in a number of other stuff so you know you know that's working you've added it correctly so that's all good and then the last one we want to use is because we're using sequel server there's another package for sequel server and an office on here will be here somewhere but we'll just type it in it's fairly straightforward so it's basically exactly the same as the other references we've been adding except we just add on sequel server and let's check that we added the right one and we'll see it we should see being added to a CS proj file then we cool now it's at this point here if you were using some of the post grads or my sequel you wouldn't add this package you would add the relevant package for your provider awesome so that's our package reference is set up the only other check I want to make is we're going to use something called the dotnet entity framework core toolset and that will allow us to create and generate something called migrations which I'll come on to in a minute but I just want to make sure we've got the tools set up and running so don't net yes just to check that you have the tools up and running and you should see something very similar to this you'll get a little unicorn it will give you a Belgian of the tools all that kind of stuff no if you don't see that and you get an arrow then I'll just just copy and paste this alright I'll put this in the know you can look at it on the video here but also maybe put it in the video narrative if you don't see that then just run this command to your dotnet to install the - global dotnet - EF and that will get the toolset if you're running an although the tools say you're running you know 3.12 then you can instead of using install you can replace that updating that will update you to the latest version but you may you may get that complaint anyway we don't need to do that that's all working so we're ready we're ready to go so what we want to do now is actually create our DB context class and so back over and our project I'm just clear down a number of open windows I had here just to give us a bit more breathing space and yes we're going to create our DB context which you'll recall from our architecture overview you may see placed in the models folder I'm not going to do that in listen since I'm gonna put it in with our data components or just right-click data folder new file and give a name call it the command or context dot CX if I spell that correctly yeah awesome a name or just specify the name space as usual commander data and then we'll declare off the class public class commander context now what we actually want to do here is inherit from a base class called DB context and it will complain because it doesn't know what the heck that is so control V need to bring in the relevant namespace which should look familiar if you were paying attention in the last section and the first thing we want to do is define a constructor and we're going to actually use the base keyword to call the constructor or for the derived class which is the DB context in this clip in this case so public actually let's let's use the uses a short cord seat our command context the planet oh we want to pass in our some options so DB context options of type commander context opt and then we'll use the base able to actually call the constructor from a DB context and we'll pass him those options cool you don't need to put anything in here at the moment and I don't think we do for the rest of the tutorial but we need to set this up anyway so the next thing we need to do which is probably a bit more interesting is create a representation of our command model in our database and the way we do that an entity framework is to use a DB set so we define that as a property in our class or prop tab and it's a DB set of type command and we'll give it a name commands and that you'll notice this pluralized cool now it's complaining but commanders are doesn't know what that is so control P need to bring in the using statement and that should resolve and that's basically it so just to Gore what this is again this is basically seeing we want to represent our command objects don't to our database as a DB set and it's going to be called commands no just remember this name here and you will see it real car in throughout the rest of the project and in one place specifically that I won't call out right now and that's it now just to again labor the point if we had more models and here we could add other models and here so platform and users or whatever loads of different models and but unless we add an Associated DB set within our DB context those classes won't be map then to our database we need to do this mapping here ok ok so we've got our dbcontext awesome we've got our database awesome but at the moment there's still a disconnect between the two our application doesn't know about our database so we need to rectify that clearly and the first step in doing that is to tell our application where it can find a database and we do that through a connection string the name is fairly self-explanatory so don't think I need to go into too much more detail but it just basically has the configuration elements such as horse name IP address user ID all the good stuff we need to connect our database we can configure the config for our application in a number of spaces places I'm just gonna put into app settings Jason if you wanted development specific connection string you you could put it app settings development Jason but we're just going to use the main app settings file that's fine so put any comma to denote that we're moving to in you Jason item and we're going to specify the key here we'll call it connection of strings now you can you could call this anything but you would then have to reference the connection string and a slightly more long-winded way when we come to doing that from within the cord if you use this string here connection strings again it's kind of convention over configuration the entity framework offering what Knaus that our connection string is going to be contained here so that does make any sense I'm probably not explaining it very well you'll see when we come to referencing it in the cord what I mean call on and then another pair of curly brackets and you can add in a number of connection strings you may be connecting to multiple databases we're just going to be connecting to one so double quotes again you can call us anything you like I'm just going to call it command log connection that's our key and then the value is going to be an actual connection string so I am going to know let me tape it in I was going to copy it and but let me let me type in so the first item we want is our server and that will just equal the localhost comma 4 4 4 that I use previously and obviously you and your host name or IP address or your sequel server resides configuration elements are in a Microsoft sequel server connection string are delineated by semicolon so that's another good point this connection string is specific to a sequel salvo it will change if you're using a different database provider so if you're using Postgres Oracle on my sequel or something like that your connection string will look slightly different and you'll need to do a Google i/o to see what that connection string would look like so package provider that you add into your CS plaud file would be different and then the connection string would be different and then the last that according we need to do would be slightly different as well if you're interested in how to do for Postgres I do cover that in my book but to be honest with you it's quite easy to find it on Google so that's also a component the next thing we want to specify is the database we want to use and sequel server you can specify that using the initial catalog key and we can call it anything you like and Clinton : commander DB now you'll remember from looking at sequel server we have a database in here that's and they be database I treated for something else but we don't have that database and yet that will be created at some point so that's really the name of the database here and again its initial catalog name on the specify the user ID user ID and I need to bring up a little Excel spreadsheet of what I called it I'm just going to paste that in in my instance that was commander API it some extra whitespace there and then the last key you want is no surprises password now red flag red flag are there I'm just going to type in my passwords it's plain texts or everybody can see it and if I then push this up to get hub for example everybody can see it big big normal and a development environment you reduce something called user secrets to squirt all those data values away so that you wouldn't see them here and they don't get pushed up to github and in a production environment you would use something like because your key ball or something else to do that I'm not going to cover that here just in the interest of time if you're interested then use our secrets again you can google it or if you want to buy my book I cover it all in that both production and the to set up secure connection strings but for now I'm just going to specify my passwords in plain text which is a password I only used for test stuff so feel free to try and hack my online accounts with it if you want you will not succeed so password is password I think that's correct cool so I think that is everything we need to do I don't think I need to put a semicolon in there but I'm going to as it's in my notes so I think that's our connection string all set up cool all right so now what we want to do is we actually want to again start to tie all this together a bit more still already fragmented so we're going to finalize this setting up our database context so where do you think we configure our DB context class for use within the rest of our application any ideas yes for those of you who said startup give yourself five gold stars and a million points correct and what methods do we configure it in correct for those of you who said configure services your our way to get yourself another ten gold stars and another three million points you're doing really well so we do it through our services collection services add DB context and what type of dbcontext are we adding and rounding it commander context and I want to supply some options through lambda expression and we want to use sequel server now it won't pick this up as we thought the right name spacing now this is specific to sequel service so again if you're using a different database provider different package reference within your cs pod file different format of a connection string and then at this point here you would use a slightly different syntax to add the provider for your database so you're using sequel server in this instance and then we're going to pass in some config elements so we're going to make use of the configuration API configuration you're Asian and when I was whistling on about you know being easier to get connection strength we use a particular key within our app settings Jason felt this was what I was talking about we can make use of this get connection string method and then all we need to do is provide the name of the connection string in here which was commander connection okay so the chords a lot more readable that's a lot nicer if you didn't use this key here and you just call that something else you would have to reference your config again using the configuration API but you'd have to do in a slightly more long-winded way which are not going to cover yeah it's not that difficult just doesn't look as nice so the only thing we needed thing to tidy up is just bring in the correct using statement to make use of use sequel server and that is using the Microsoft entity framework and that should work so I'm not gonna go into too much about this I think this should be fairly obvious to you know we're adding a DB context for use without for use within the rest of an application through the panes engaging and we're just providing in at some config which is our connection string so with that we should have joined all the dots we have a DB context we have a database we have a connection string or no register that all together with our start our method we should be good to go to move on to thinking about migrations ok so just make sure you actually save your work and not save the startup class then before we move on to anything else so what are migrations migrations are really they're just a list of instructions given to our database to allow it to recreate or create if you've not done so right already a database schema that marrows our internal representation of the data within our application so in our case if you look at our DB context class we've said b11 DB set of commands so our migration will basically replicate that desire or don't or sequel server and as we've not run it already it will create a database for us as well over time as you make changes to your application and configuration detail to your model and you'll see what I mean in a minute your migrations will change over time and you will apply those migrations subsequently to your database to keep it up to date and you can roll back migrations and all that kind of cool stuff so enough talking let's go on and do it so I'm gonna bring up a command line and I'm gonna basically use up the dotnet core tools that I was talking about earlier on so if you haven't got those installed now it would be a good time to do it so dotnet EF migrations ad you give it a name and try and make the name meaningful to the change that you're going to apply so as this is our first migration something like an initial migration is actually a totally play maybe capitalize that okay capitalize this alright so what does that do well let's say enter and find out keep an eye over here and what you should see is we have a new folder automatically created them and just minimize these so we have a bit more screen real estate called migrations and in here we have a number of files now the only one who are really interested in is this last one here and that basically killed us as well are the steps that are going to be performed on our database to create our table down on the database there are two methods up and down up will create stuff down will delete stuff and it's use the down methods use the field rolling back migrations once they have been applied so all we have done at this stage is create a migration file I've not done anything on a database if you have a look at our sequel server and it'll do a refresh still don't have a database not have any tables nothing has happened to our database yeah all we have done is create a migration file I mean you need to run that migration know before we do that just taking a look at the up method you can see what's going to happen we're going to create a table it's going to be called command so if you'll remember if we go back to our dbcontext class we called our DB set commands that's where that's coming from okay you go back go up it is on migration okay it is don't know that right maybe it's off screen somewhere and then you can see we are going to create a number of columns with a bat table so ID is going to be not nullable multiple as false it's going to set something called the sequel server identity with our seed of one and an increment of one so that means if you insert a row into a table that ID column will automatically be populated in initially with a 1 and then every subsequent insert will add one to it and you can also see here we're adding a constraint to our table and that is creating a primary key using the ID now again when we created our command model we use the name ID for a first property this is this idea of convention over configuration and entity framework or laws that we intended that to be our primary key so it did all the stuff for us without us having to specify anything specifically cool so I'm happy with that however I'm not happy with these other elements here so you'll see again if we go back to our Model S and a quick recap of what was in it we have the ID how to lying on platform so over here the migration is basically saying yeah we want to create those columns that's all cool but you'll see here nullable has been set to true for all of them because we've not specified anywhere that these don't need to be null so I don't think that's very good because what's the point in creating an ancient it doesn't have a command like and snip in it there's absolutely no value so what I want to do now is go this migration is not quite what I want and we need to change our model to reflect the validations that we require in our database to ensure that there's actually data there so bringing back our terminal prompt you can see here there was a little comment right at the end undo this action use EF migrations remove so I'm going to do that and all that's going to do is just remove those migrations that have not yet been run so yeah we've got net yeah move that costs load the way so you can see migrations beam it okay so let's remove that and all that we'll do is we'll just remove these migrations from our folder and the folder will eventually be empty it's not actually rolling back a migration that's slightly different bullying back and migration is once we've applied it and actually implemented on our database we can roll it back that's not what this was this was just basically getting rid of a migration set that we had created cool so you can see this has now been deleted so we'll just close that down so what we want to do now is revisit our model and actually rectify the issue that we had there so let's kill that let's get rid of this let's get rid of this let's get rid of this we shouldn't need those again and go back over to our command model now it's the simplest sort of taper class you could ever define we're going to make it slightly more complex now so we're going to create some room here and what we can do to specify that our how to call them and in fact our line column and in fact a platform column or not null is to simply decorate them with something called data annotations and those loads of these available and you can get quite complex with it but all you need to do is specify the required annotation now we don't know what that is so again control period and we'll bring in system component model data annotations now you'll see I'm not actually putting that on here because again convention of a configuration it knows that's our primary key and by its very nature that it's require they're not nullable what you can do is you can actually specify the key annotation you don't really need to do that because it knows already but there let's let's put in it maybe it sort of helps document Accord a bit but you don't really need to do it let's put require them for all the others as well and you can get really complex with this I'm not going to delve into too much in this video but you can get a lot more complex one thing I do want to probably do is constrain let's see the length of the string just to show you how that works so you can specify something called the max length and then just give it a chart come to 50 over so you're not all undoing the strings that you're putting in again that's just an arbitrary number just to show you how that works all right cool so we've updated our model let's rerun our migration or treat might create migration command and see what our migration file looks like so let's kill that let's bring up our command line again let's do an op key up key again and let's run that one more time again it creates from your migrations the only other thing to note there is a time stamp they are so again that allows this entity framework court to apply the migrations in the right order if you have multiple migrations and you may be wiped your entire database you could run your migrations again and a bit run all of them from the start in that instance or if you've rollback it will run them from the point at which you roll back and we may actually come to creating database there's a table in the database that has a track of the migrations that have been applied cool so that's a quick look at the file and it looks very similar but you'll start to see some new stuff in here most notably for all of these three columns that nullable has now been set to false which is good so it basically means we require all that data and for our how-to column you can see here that the max length annotation has been added in as well cool so I think that looks good for our purposes so we're now ready to run this migration file and see fingers crossed if we've done everything correctly that it creates our database and our table and our database that will hold our commands so the way we run migrations in entity framework or is dot let EF database with space database update now this is where the whole call wrong if we've not done things correctly but let's see how we go and fingers crossed I'm confident that we have done everything correctly and if not I blame you is your fault not mine joking of course so that that may take a while to run shouldn't take too long though so we need to make sure you of database is up and running and all that good stuff so it looks like it's done it's not complained significantly so let's go back over to our database and let's do a refresh fantastic you did a good job they are well up and running so it's created a database called command ODB just to refresh your memory back over in our connection string when we specified an initial catalog that's where this value comes from and then within our commander dB we have or it should have a couple of tables and did we do so we've got this internal system table EF migrations history let's right-click and do a select top 1000 rows and that will just show you the migration ID that was applied and the name of the migration and what plot that version was that so this is how it can a keeps track of what my creations have been run cool we're not going to use that again and then of course our commands table so there's its to select there won't be any data in there cool so that's all been set up correctly so give yourself a clap give yourself a round of applause that's the same thing isn't it but a good so that's our that's a big big milestone so the only thing I want to do now is actually add some real data to our table now look but a number of automated ways you can do this and I have again detailed that and other tutorials and I think it's in my book as well in the interest of time I'm not going to do that here I'm just going to do a bit of a hack and just right-click our commands table and do edit top 200 laws and you get a kind of Excel like entry not an entry form but yeah the excel tape setup where you can just directly put data in to the table what better way to kick off our command-line database than to just detail the two commands that we've already used to create migration suits cannot occur a meta type scenario so the first one would be how to create migrations and then the command line let's just remind ourselves of what that was there you go copy that out and we'll paste that in here now again doesn't matter what we're putting in here but to be honest with you I actually I actually do useless in my in my real life I've got a sequel server database not this one another one it has a list of all these many many commands for things that docker anti-free mark in UNIX and Linux and I actually use all the time so I've found it useful maybe you will too platform is yeah cool let's capitalize that one thing you'll notice is that we didn't put an ID in and this is a concept we'll revisit a bit later when we come to creating resources through an API the database created our ID for us okay and again if we add our second command line snapped in Oh to run migrations day to be something that's correct not quite on ATF database update that's correct doesn't my memory is absolutely shocking yeah that is correct cool you'll see actually if you have to fit them all right so there you'll see it puts in an idea to something else let's just try put some junk data in and if we try and add that like I just danke there you'll see it will complain because we have a normal value in our platform and that is because our data annotations have been applied to the table so let's just escape that we only want two lines of data let's do a quick runs or select everything from commands we can see we've got our data in our database cool just going back to what I was seeing about database design if you right-click the commands table hit this item you'll see down here I'm just going to order the attributes out ethically you can see a number of things that are in place first of all this identity specification which was you saw that in the migration file where we have the increment and the seed one and one is it an identity yes oh good you can see here it's a primary key you can see here that nulls are not allowed on any of them and you can also see the 250 character limit on our how to column as well so all matches what we specified and our cord has been migrated then to the database so it's actually really cool and very powerful alright so we are basically done if you just quickly have a look at our architecture again in terms of this bit here the data access class or sequel server database and indeed our model no I think I'm correct the doesn't me change our model again now we are done that's all done that's all locked in but what we do need to do is now revisit our repository and what do you think we need to do currently we're using a lovely interface which is not no it's going to change because we're going to add more methods to it but that's not going to change initially what we're going to do is we're going to swap out the concrete implementation if that's using which is currently on lock repository and we're going to swap out for a new implementation using our DB context so this is where it starts to get quite exciting we're going to use real data via a DB context and the new concrete repository class so why don't we go on and do that now all right back over in our application we want to create a new implementation of what I command are equal now just before we do that there was something it was slightly bothering me I can have renamed this wrong it was just a slip that's just a typo error sin of get app commands it should be get all commands makes no actual difference to the running of our chord that it was just something that was allowing me however illustrates a really interesting point we've never just changed our interface which is almost like a contract and you can see now because we've changed the interface we now have multiple arrows or cord so it's a good illustration of how important it is to interfaces correct so we'll just fix that up so back over in mock commander ripple it's claiming they've not implemented it interface correctly which is in fact correct so let's just update that and that should get rid of that error and then obviously we're using a repository from within our commands controller so we just need to update that so really useful illustration on yes just because you're using repositories and dependency injection if you get ultimately get the contract wrong when you change it it will have ramifications all right so we don't need any of that stuff now so yes coming back to what we were intending to do we want to create a sequel implementation of our I command or interface so select your data folder new file sequel commander repo CS name space commander data and in a public class called sequel and repo and what we're going to implement we're going to implement our I command a repo interface and then again straight away it should start to complain why because we've not implemented the members yet so let's implement it and we get placeholder stuff that's okay so let's implement our get all commands know that come with the correct name that was really bothering me from before so we want to basically make use of our DB context class to return our command items from our database where are we going to get our dbcontext instance from correct we're going to get it from constructor dependency injection so back over on our class seat or to wired up a constructor public sequel commander equals plane and this time we're going to ask for not an interface but an actual implementation of our context I will call that context it should be resolving this because it's all in the same namespace commander data command seemed to think that because I've got three M's in it there we go bit overzealous with the ends and we are going to again following the same pattern or dependency injection system is going to populate this context attribute here we're going to pass that into a private read-only field called context and it will complain doesn't know what this is so control P need to generate a read-only field now again this is a very similar pattern that you'll have seen before also if we just pop over to startup you'll see exactly the same pattern used in our startup class this was the scaffolded code that we got so it's used everywhere so you'll get very familiar with this setup so that means we now have an instance be a dependency injection of a DB context class called context so we want to literally just return port from our context well you can see here we the list of operations and methods and attributes but the one we want is this DB set called commands fantastic and we just want to return all commands to a list now welcome it will come kids it it will complain because we need to bring in the link namespace in order to do that and it's literally that simple so we've implemented I get all commands using our DB context and it's just going to return a list of all the commands from our database for our get command by ID is slightly more complex not much more so but we're going to return for more context class and using our commands DB set we were going to use the link command first for default and then we're going to use a lambda expression to specify what we want and we want something equal to the ID that we have passed in in our method so that's basically just getting the foster default member from our commands day to say where the ID equals that fairly straightforward now this is real the magic starts to happen we've just implemented a sequel command or concrete class that implements our interface let's look at how easy it is to swap out a lock implementation for our actual sequel implementation and hopefully it should all work so just going to bring this back up again I'm gonna do a dotnet bill just to make sure we got any arrows hiding anywhere you should get nothing cool so back over and I'll start our class real do you think we make the swap arun no no surprises in fact that's just calming that out so you have a reference point for it drop it back in instead of mock command the repository simply type in sequel command of the pool that's it you don't need to make changes to Accord anywhere else we've created a new implementation using sequel Silva think you might want to use a different database or you might want to use a different war amps or not entity framework something like in hibernate you could create a class for that you could have multiple who are Em's via the same repository it doesn't really matter so very very powerful so all that's left to do really is to build it again but you should get no arrows and that's just run it node and let me bring up the ocean of a instance you may see of I keep forgetting a name of this application postman use all the time for getting the name of it so it's just clear these already yep and let's another thing I was playing with so local host 500 API commands and we get no response because I think we're using HTTP yeah and it should be HTTP so again these are little gacho's if be put in HTTP and we can use port 501 or as I was trying to do they'll just use HTTP let's see if that works and in fact it no it doesn't is still returning back our hard-coded repository I wonder why that is I wonder if it's because I've not saved the file Hey and indeed it is oh my goodness that's gonna drive me nuts Oh make sure you save your work I don't know how many times that is call me oh let's try it again I'm leaving this stuff in I could quite easily edit this stuff out but I want to show you the mistakes I make as well let's give that another Google ID that's quite good because that was showing us what we were all returning when I was using the mark it actually it's fortuitous try it one more time there we go so after saving our start up file and after having swapped out the concrete implementation that we're using none of the other court has to change but we're now getting data form our sequel salvo so that is another part of this tutorial finished it's a lot of lighting up that was required but yeah well they are repository and dbcontext are looking really good so now we're going to move on to another concept we'll revisit our architecture and we'll move on to another concept around data transfer objects now so yes let's take a quick look around architecture just to see how we're progressing along with the build so I would do a lot of the things we have already covered basically in the last section we did we set up our DB context class to access our sequel server database using entity framework or and additionally we set on a sequel repository and implementation of our interface to use that dbcontext all good still so what can do in that repository but it's progressing well now our discussion tunnels to DT AWS and that's the next part of our architecture here so I'm going to talk a bit about them and it's basically again talking to this theme of decoupling implementation from contract detail so they just I want to go through the rest of those animations bring up this next slide so this is our current state and I've denoted our API or application as a black box and what I really are trying to see it as my client perspective it does not care what's inside that black box how we've implemented it whether it's c-sharp Java whether it's using a sequel database it doesn't Kim it shouldn't care all it cares about is the contract that we have established with the client to see this is the interface the API interface that we are going to provide to you and conversely the client should be using that contract to make calls to our API otherwise it may end up with some strange results so it's a bilateral agreement really now at this point in time our client will make a HTTP GET request to our API and our API will return some serialized data to the client and at this point in time it's acini alized instance of our command object command class as an object and I've got a representation of that on the screen there so you may go well that all sounds perfectly reasonable what's wrong with that well what is wrong with that basically really exposing internal domain implementational detail back to our client and what I mean by that is that we might be providing potentially irrelevant data back to our client as its low-level implementational detail ur might be attributes or properties within that class that we don't want to expose back to our client things are not relevant things are perhaps in the wrong format for our client to deal with maybe we have an internal representation orbit and we want to provide it back in some other format to our client or possibly even bit sedated our secure that we shouldn't be exposing to our client so a very common one that's used is data both we might be storing date of birth in our internal domain model but we don't want to supply that back to our clients for obvious reasons we might instead want to supply the age of that person rather than their date of birth so at the moment we are really just exposing our internal domain what do they mean back to our client and that also means we are coupling our internals to our external contract so in the last layer I was talking about providing a contract basically we've made our internal the main models part effectively part of our contract with our external clients so that means if you want to then change our implementational detail change of the main models in some way we're gonna have to be very careful that we don't impact a contract because that's the last thing you want especially from developing api's especially from developing api's that are being consumed by public clients even even internal clients with the middle and organizations still definitely a big no-no or as I've said there you may not care about that you may just make changes to your domain models and break the contract altogether I don't know if you do that you're probably your organization or certainly yourself as an individual probably aren't going to be in business too long if you if you behave like that so it's a big big no-no so what is the answer well a high level we need to decouple our little terminals from what we send and receive from our contract basically and that's real data transfer objects come in so I've opened up a black box I've denoted it by a dotted blue line that you should be familiar with now and here is our command object that is our terminal domain model what we want to do now is set up an external model or data transfer object that represents that internal domain model now there could be a complete one-to-one mapping you me is still a valid use case to set up a command ETL class that's exactly the same as the command object and you're just mapping all the fields one for one you could do that but more than likely you're going to know it's going to be there's going to be some differences between them but the important thing is there needs to be a mapping involved now if you're going to come on a bit later let me come on recording this - doing it off the mapping in an automated way rather than doing it manually which you could do if you so chose to but I'm going to show you how to use something called auto map to perform that mapping and what would be then happen once you create this command DT all it's a representation of and that is what will get passed back to the client and again I've just put a bit of a representation of what that might look like on screen and you'll notice just for the purposes of illustration I've removed the platform attribute format so maybe not the best use case but what I'm saying there is that platform attribute is not relevant from our consumers perspective so I'm not going to pass it back as part of the DTO and what that means is you can then change your command domain model to your heart's content so long as you map it through to whatever that eventually looks like to your command ETO which should not change or not change much then it's all good now just before we go into coding just taking a little step further on ribbon and be taking this approach ourselves this is the use case I've just shown on the last slide and I was talking about reading data so I'll get requests or get action results and we're good to create a command read detail so a representation of our command model for reading but you can take it further and actually create our detail for the creation of data and we're going to come on to doing that again later when we are posting to our API so HTTP POST request we will include some object data in the request body and pass that through our API we may and in fact we will want to have a different representation or a different detail for the creation of data and I'll just mention that what primary key or ID field maybe have a better think about whether we would include that or not in our command and create DT or so I think with that we'll review I think it was worth doing that on screen just to give you a bit of a visualization of what we are going to do we're going to now move on to actually writing up and starting to implement details okay so we're back over in our application we have that running from just previously so I'm going to do a ctrl C to stop our web server and I'm just going to get rid of our command terminal Day stickers a bit more space on screen so what we're going to do first and it's kind of similar to when we use the entity framework we need to add some new package references so actually if you do need a command line because I'm going to use the dotnet core CLI to add them as usual let's go over and have a quick look on new gets to pop packages we need to support details so we're going to use something called auto map or no just let just a quick digression we don't actually need to use auto map or to use detours when we come onto actually mapping our domain model lock my models to the detail that we create we could do that manually okay it's very error-prone and again as the objects getting bigger it's just something you don't want to do so this auto map our package basically the thing suggests automatically maps your command objects to your details and actually vice versa so when we come on to doing the create action all that our create detail to our model as well so Maps both ways you can do some really interesting clever stuff with it you know I'm going to stress it too much nonetheless it's a great tool and it seems there's a lot of time so what we're actually going through the package we're actually going to install is this one here automatic extensions Microsoft dependency injection and it has auto map as a dependency so we just need to add this so again I'm using dotnet CLI let's copy that to the clipboard let's clear the screen and paste that and I don't about I could leave as the latest version of I'm just going to take that and let's go and see what we get so I'll go off the new gate I'll bring the package down which should add it into our CS plot file so that all looks good so to make use of our auto map or a facility again where do you think we need to kind of start wiring up that's right of course in our startup class so let's move over there and add another service for us within the rest of an application so over and start up let's just move over here I'm going to take this out now I think you guys should probably understand that well enough now you don't need that cord lying around so that's all good so again we're going to make yourself a services collection and yeah we'll add the names basically so so yes this time it's good to be add Auto mapper and it won't find that because we don't have the namespace so we'll bring that in in a minute and the way that it needs to get created on Stan sees that it needs to get the current assemblies with they're not application so we access that by going abdomen Condamine and then get same please don't forget the double brackets and the semi colon and as you can see here it's having trouble resolving this so ctrl P need to bring in using automata at the top there and that's all we need to do again within our start up class with a lot configure services method to make auto map are available through dependency injection to the rest of our application fantastic so that's the kind of wiring up done let's move on to actually using and implementing it so the next thing we want to do is actually create our DTO object so that will require yet again another folder so click out into the root space of the file that directory new folder and we call it the not dots details goodness details and in our detail folder we're just going to create our command read DTO so new file command read DTO OCS and i'm going to cheat a little bit here I'm just going to go over to our existing command model and I'm gonna copy all of the stuff in the namespace done and I'm gonna copy it into our command we'd DTO I'm just going to change the name space from models to details make sure you do that and the name of the class to command read DT oh cool now I'm going to take out the data annotations because as this is a read detail we don't really make as much sense you could argue that yeah I mean it helps with the contract I suppose to understand what is required what you can expect coming back but for simplicity sake I'm just going to take these over for this detail we will inject them back in when we come to doing it create DT all and just looking at that that's probably about right just making sure of named it correct the command read D to you it's the right namespace know as per the PowerPoint and just for the purposes of illustration we're going to take out this property here the platform property and we can do that under the guise that it's implementational detail that we don't need to expose to our client I would argue that it's probably quite a useful bit of information but it's just for illustration and that's basically our DT or created now what we have at the stages we've got our command object still or domain model and we have our DT or now we have to tie them together via a mapping profile so let's do that now all right so I'm just going to kill a few of these things I don't think we really need to reference at the moment will your command radicchio and our command object up there and what yes as I said what we want to do is create a mapping profile that map's our command object to our read DT also that when we pass out that's what's going to get mapped and we require yet another folder and our route so click out anywhere and new folder and no surprises what this is going to be called we're going to call it poor files and we're going to create you create a profile file for each of your domain object so we're going to create one for our commands so we'll call it commands profile make sure you put it in the right folder not in properties so create file commands profile that's yes okay fantastic and I'll just just clear our name space commander profiles not there profile I awesome and we'll just to clear a public class in here and we'll call it commands profile awesome now we want to actually and head it from a base object called profile and that is available through the autumn app namespace or control pede to bring that in and then all will be something to do is create a constructor for our class commands profile is perfectly fine we don't have any parameters for it but what we are going to do is use this create map command to map between a source object any destination object so what we mapping from and what we mapping to so great map we're going to map from our command object singular probably shouldn't name this project something different that's coming up annoying and we're going to map it to for the new color DT or commandery DT or we're going to map it to command V DT oh cool don't forget the black brackets so it's complaining or it doesn't know what that is so using DT o--'s and similarly using models and that's it now this these creep maps can get a lot more complicated if you're concatenated Atta or joining data of rules and or something on that we are just going to make use of it from a fairly simple level and all that's doing is basically looking at both objects automatically just map things straight across so don't forget to save that so now we're going to actually come on and we're going to revisit our commands controller and use auto map to return back ADT or rather than the internal domain object okay so let's move over to our controller now and I'm just going to do a little bit of tidying up of our code a little bit of that must not really be factoring by taking out a comment but I'm gonna take that Oh anyway just to clean up I'm going to leave these and for the moment because it might prove useful when we come on to doing our other action results but I'm gonna return that to being a variable ID and our wrote and the only other thing I want to change is this action result heat a bit it's not really adhering to our specification and by that I mean if we call this with an invalid ID of the resource that doesn't exist it will return I believe are not nor content error code we don't want that we want not found error code so let's just run it and test current behavior in Batam a postman didn't want to start over look don't want to do that kill that postman so let's get the valid result I was just playing with this before yeah so we have a resource with ID 1 in our database if we pass in an invalid ID or one that doesn't exist we get this nor content so we actually want to return and not fund instead as per our spec so let's close that down and it's also it's also slightly nicer cord as well so back down here we're still going to continue to do this but all we're simply going to do is check whether this is null or not so if command item doesn't equal null then we're going to continue to to do this otherwise we'll just return the north end so save that off let's bring up a command line let's rerun it excellent and let's try this again so the one it should bring back a 200 okay fantastic and if you do a 4 it should bring back he north and 4 or 4 north and which is much better cool so let's stop this let's kill that and now get back to what we were wanting to do it which was use auto map on Oh quick question for you we want to use auto map or forum within our controller class how are we going to get an instance of our auto map or object mmm well of course constructor dependencies yet constructor dependency injection and so the way you add more services into your constructors you just simply add them as another argument into your constructor so this time we're going to ask for an eye mapper object and we're going to put it in something called an apple now it's going to complain because we're not using auto map we all know and then we're going to put it into a private read-only field called underscore mapper and the exact equal to Apple and yes you guessed that control period create a read-only field just the same pattern again and again so hopefully this dependency injection constructor of injection is starting to look pretty good and pretty straightforward so we're going to now use this map our object within our read or get action results so let's do our get command at it commit it's been along the get command by ID action result now the first thing I draw your attention to currently this action result is returning a command do you think we want to return my command well we don't want to return a command you want to return a command read DT or so let's update that and let's bring in the relevant name space come details so that's the first thing we want to do and then within our return method here everything else is going to stay the same we're going to use a repository in exactly the same way and we're going to bring back a command object but what we are going to do is not the ton back the command item if one is fund we're going to return back the command read DT or that's mapped from the Dineen object and the way we do that is by making us of our map our instance and then the map method here and what we're going to map but we're going to map into our command read DT or type that's what putting data into and wheels that data are going to come from I'm sure you can guess but it's going to come form that's right it's going to come from our command item and I think that's probably it to be honest with you so why don't we give that a go and see what we get now just before we'd nothing let's run out first and run this is going to test the number of things to make sure we've wild it all up correctly so back over here mmm if we call a valid endpoint of valid resource we do indeed get it but you will notice that the platform element has disappeared them you're actually getting our DT all back give yourself a round of applause it's incredibly exciting I know and to do it for our other action result we were returning a list of objects it's very very very similar it's not much different instead of tunneling we're tunneling an enumeration of commands we are going to the tongue and enumeration of command read DT ORS and instead of a tunneling back elimination of command items let's get rid of that we're going to make use of our mapper we're going to map what are we going to map went to map an ienumerable of what of command read DT o--'s and creators the data coming from it's coming from our command items they've returned back from our repository and that should hopefully stop complaining it'd help if I copied the full thing command items fantastic let's save that off let's run it up again and see how we're going I'm feeling positive about this I hope you are too and bring up horse man again and let's bring back all our commands and again the only get two in there but you will see that we brought back our array of objects - the the platform attribute that's DT or that's V DT or anyway we are getting very very close to them and in fact we're pretty much with that we've really set up all our plumbing it's taking a really long time to set up a repository set up our entity theme what core stuff set up our auto map or stuff now we've still got to add bits for other actions but in terms of a base level wiring up of everything are pretty much done all we need to do now is return to our controller and end and our repository and start building up our other action results so just a quick application architecture check just to see how we're traveling I'm just going to skip through the stuff you're already familiar with so recover DT ores just in the last section and in fact based on what you're seeing here we've actually implemented all of our architecture and as I just mentioned we still have other gaps to fill in but the overall architecture was pretty much covered everything now and all that's really left is to come on to looking at the remaining end points we need to implement and the next one we're going to pick off our list is our ability to create resources in our database so let's let's start with that and hope so back over in our application let's just shut down our web server let's get rid of our command line now we've covered a lot of stuff and if we're going to introduce another action result you can of go we're will do I start with this there's a number of places you could start but I'm going to start with our repository interface and I feel that we need to specify a new contract item in there to denote their ability to create something in our ultimately in our database so let's go back into data let's go back or - I command or repo and let's add in a new effectively contract item that's going to denote an ability to create something in our database so we're going to have a void return type we can come back to why I'm doing that later but void return type for the moment and we'll call it create command and what will that take it will take a command object as its input and you can call it something like CMD again that simple now we are actually going to add another interface item in here we're going to call that Save Changes so make a bill this time call it save changes and it doesn't actually take anything now what is that well let's park that for the moment just trust me on this we're going to need something called Save Changes we'll cover it in a bit later so let's save that off so that's all we need to do for our command report interface we've specified two new methods one is quite obvious one is not quite as obvious but we'll come back to what that is in a minute and straight away you can see that our mock repository and our sequel repository are complaining why because of not implemented interface know at this point um you can leave the mock repo here or you can delete it altogether that's entirely up to you I'm not going to do anything particularly amazing here I'm just going to implement the interface and we'll just throw these not implemented exceptions that's all I'm going to do for me I quite like having a mock repository hang in a remnant case I do want to use it so rather than deleting it I'm going to leave it there I'll just no save that off and then we come back to our sequel command report where we actually do need to provide some implementational detail around that so as usual control period doing that want control period implement interface and clear that it okay create commanders here and Save Changes is here so let's do Save Changes first it's a really simple one to be honest with you override all but ultimately going to do on our DB context we're just going to call a method C Save Changes and what that actually does is whenever you make a change to data via dbcontext the the data won't actually be changed on the database unless you call this method so we can we're going to use other methods on our context like create and I think that delete remove all these kind of things but until you call Save Changes that won't then be replicated down your database so we need to implement that method here and we did see a return type of bill so let's sort that Tom put it in brackets and greater than zero I think a little to zero should suffice okay cool so we come cleared up when we actually come on to using it okay so we want to move on to actually do not create command which is a bit more interesting so let's get rid of that and first we want to check is to see if the command passed then is actually equal to null and if so we want to throw an exception where the hell did that come from I don't know so throw argument now give us reaction when we use this name of construct to return the name back then it's going to complain about something spelt among an argument and we need to bring in system namespace cool no if it's not null then what we want to do it's actually really very simple on our dbcontext on our commands DB set we want to call add what do we want to add we just want to add to the command now this is what I'm talking about what it's talking about previously simply adding the command to the commands DB set in our context is great and it will actually be tracked within our DB context but unless you then call save changes on the context that data won't be flashed on to the database cool so that's pretty good we've updated our I command a repo with two new contract items and we've updated our sequel commander repository with actual implementation for our sequel server as far as our mock repositories gone with not implemented that but you could if you want to mock it up that's maybe an exercise for you to look at yourself okay so what do we want to do next let's take a quick look at our architecture and we're trying to pick one that's not animated I think they're all animated open up so we don't need so we're talking purely about implementing our create action on our controller what we really need to focus our attention our models good we don't need to do anything with our model our controller definitely needs to have a controller action result in there so perhaps we look there data access DB context we don't actually need to do anything with that it already had that add method built-in so that's all okay a repository is done cool detail so we did mention that when we make an API request to create a resource we're going to expect some body text in that request that will map to a resource that we want to create so in my mind I think that's probably where we should go next is it's a more foundational element so let's go and create our command create DTO and have a better discussion around that so back over in our application I don't think anything's running it's not cool let's get rid of that so let's this is all looking a bit busy this is why I don't really like running in a a low-resolution but it's definitely easier to read when you're on a mobile device or something so don't need to do anything more in our data folder we do want to have a look as I said at our DT or folder of dto space and we want to create a new DTO source like that new file and you'll call this command create D QCs and let's just copy this through and paste it in here and we will just change the name to command create DT all now looking at our model I'm looking at the items that ultimately are going to be required in a database somewhere these three fields are all required so if we're going to attempt to create a resort at abase we need to supply all of those soul and our reads we're not supplying it back but I'm going to copy it and I'm going to paste it into our create detail because we will get an error if that is not supplied no coming back to this we obviously require an idea primary key it's absolutely mandatory should we be passing it over from our client application to our API in this instance the answer to that is no because our ID our primary key is actually being created by our database it's taking care of that so and not create DT or we don't to be supplying that as an input it's not needed it's not relevant the ID doesn't exist before it's created and it's going to get created by the database so with that in mind we take it out now just going back to our command model and our EDT or we took out all of these data attributes data annotations I'm gonna put them back in for our DT or because we'll you'll see actually let's leave them out for the moment and let's see how we go without them and then we'll reintroduce them a bit later it's probably a better way to demonstrate it so for now command create DTO is being created we've taken out the ID we don't need and have re inserted the platform attribute into our create command DT all so let's save that off and let's think about how we're going to use that now so back over in commands controller there's actually a surprising amount of work to do to get a create action to work so maybe we just give a quick peek back at our definitions yeah I can find them there we go so we are implementing this one here so look at the the route it's API commands which is firing we don't really need to do much with the route the Vale was going to be post we're going to create a resource if we're successful we're going to return a 201 created and we should actually return back an instance of the created object as well which have not really got on this slide here but that's what we need to do so that adds a bit of extra complexity we have some feel you feel your cases here that we'll deal with a bit later so back over in commands control R let's begin rating up our next action results so before we even do that let's following this kind of syntax you're what Vale probably going to use we're going to be using post what's the wrote API commands that's it we don't supply an ID because the ID doesn't yet exist so that's just that's just a comment obviously so we will decorate our action result with HTTP POST so that makes it no unique within our controller because although it responds to the same route as this one it's using a different bill fantastic and public action result now as I just said we're going to create a resource in our database if it's successful nothing gets created cool we're going to return a tool one success callback but we're also going to the ton a representation of that model and what's the representation of that model remember we're reading it's going to be a command read detail so it's a little bit counterintuitive because we're going to be creating a resource with our platform attribute and then when we get it seeing it being created it's going to return back or command read detail without the platform nonetheless it helps kind of enforce the principles I'm trying to teach you so back to this action result what is that going to return it's going to return that's right a command read DT all and we are going to call it okay anything you like really but let's call something meaningful creates command what we're going to expect as an input a command create D tool and we'll just call that command create detail with a small C wonderful okay so hopefully that that method signature makes sense we're using HTTP POST it's an action result obviously and what is it returning back it's going to a ton back yet command read DT or we've called the create command and it's going to take as an input a command create DT or cool so ultimately what we're wanting to do is we're wanting to put whatever we get in our API request body and convert it into e model so we can actually put it into our repository so first thing we need to do is make use of Auto map to create a command model and we're going to make use of Mapple and we're going to map no we have a little bit of an issue we did if you remember create our profiles folder yeah and we created this mapping here where we are mapping and maybe it's actually worth putting in some common tree here source to target or destination so in this instance what they think about reading think about only about reading we are reading a command from our database and bypassing it back to our client so the source is our command the target is a read DTO so we've created a map that way we're now kind of slightly flipping it on its head and really expecting what we're expecting a command create detail and we need to map that to each command the main object so that actually requires another create map command so create map command create DT or this time and what we're mapping that to our mapping that back to a command assuming I can spell it correctly okay so hopefully that made sense so any time you're doing a translation one way to the other you have to explicitly declare it here you just because we had that belt in fact by mapping to completely different dto so you have to be explicit with your mappings that's what I'm saying so let's save all for commands profile and let's get back to our command controller and this map operation should work no because we've told automata about this mapping via our profile and so what do we actually want to map we want to map to a command object and what is the source a source is this command create DQ so we could perhaps do some kind of validation check on this before it comes in I'm not doing that just for the purposes of brevity so again just to reiterate we want to get a command model because we're gonna put that into a database and that's what a database understands in order to do that we need to map whatever was passed through in a request body and map it into command and we're using auto map or to do that so that's that better translation there and then something we need to do is make use of our repository and call the create command method on that and what does that expect that expects a command so we can pass in command model fantastic now just to prove my point I'm actually need to return something here in order for it to work so let's just do a ton let's just do this for the moment just just to get our command working because I'm gonna build this up in a few steps I wanted to take it one step at a time so I just want to get this working this is not the final chord I just wanted to stop that complaining so let's save that off and let's test that for my client and see how see how we go on so just before we actually do attempt to test that make sure you've saved your work and we'll do a dotnet run to run up now one thing I do want to say is we were seeing that we're expecting it to pass back the command VD fuel and we're actually passing back a command model it probably won't complain about that it will actually pass back the command model which is not of course what we want but again I just put that just as a temporary measure just to get this working and see what stage we've got to with this actual result so our web server is running let's move water postman and we have an old existing get request we were pulling back this resource here what we want to do is switch the spiders let's just make sure that's still working it is fantastic so we want to change this to a post request now we want to make sure we have the right URI so it doesn't have a integer at the end of it and we want to add in some body text so this is where the the new object is going to be placed the SI realized object now I'm sure you won't mind but I've got some JSON over here that I'm just going to paste then it should look very familiar it basically represents our command create DT or the how to align in a platform make sure that the body insect set to raw and the data type is set to Jason now what we should expect well it should be is a 200 ticket on what which is not what we want and an object created in our database and that object pass back so let's send that now so we're getting a 200 ok it has returned back a full command object with an ID of 0 which is kind of interesting so let's see whether that was created in our database or not so back over and sequel server let's do a select everything from commands execute it and there's nothing there let's well of course if we actually tried to do our a get request no you only get the course what I'm able to get those two objects so why was that well for those of you who are king of AI and understood what I was prattling on about a bit earlier what we actually didn't do was called repository see it changes so what we called a repository create command that added it to our DB set but it didn't persist the change is done to the database so now by saving the changes we should get an object created in our database so let's try that another don't forget to save your work let's try that again ok so back over in postman let's switch this to a post request raw and Jason and let's send this all that okay so we're getting a 200 which is not what we want but that cool and we're actually getting an object of tongue back this time with an ID of four which ultimately we don't want actually do want that and I've in detail but we don't want this platform that should be here but anyway well we'll cover that off in a minute that's looking more promising so why'd it be tested from our API rather than going directly to the database and let's just get a list of all our commands and what you should see is in fact yes we have a new resource with an idea for that was created by our database and that is no no data base okay so good we are getting there but we still are having that issue that when we created the resource we are we just an attempt we measure to a ton back a command object which is not what we want we want to a ton back a command read detail to maintain our maintain our contract as we have specified now the other thing that rest also requires us when you return a resource like that not only should you return of a source you should return the URI to where that resource can be found that's part of the surest specification if you want to call it that which if you look at the headers of our return type we don't have anything like that either all we got back was actually look at the other nice to post again let's send that so again we've created a duplicate entry it's got a new ID that's cool if you look at the headers that is well they've got the sort of got the resource back we didn't get a URI to where that resource exists and that's really what you should be doing and we should be providing a 201 response which is created so let's fix that upload right so let's get rid of that let's stop our web server from running so we are getting there but at the moment we are returning back a full on command model which is taking us back to the issue we had wait at the start and we want to pass back a command B DT all so how do we do that well something you've already seen before and think about P and actually actually let's type out because it gets you used to the syntax rather than you just pasting stuff in us any good as it so what we want to do is we want to create a placeholder command read D fuel and we'll just make that equal to mapper and it's going to map to what you command read detail and realize the data coming from quite simply it's coming from yeah octoman model that was created in our database so you bubble call b8 we did actually pass that model back fix that we did actually pass that back to post mountain last time so it was created so we're just going to use that data and map it to e command read DT or let's let's just do this iteratively let's paste that in here and let's try that again so let's do a run go back over to our model posts make sure that we have something in here let's put in how to let's just leave it doesn't matter I'm going to have a duplicate data that's okay let's send that what did we get done the figure body yep so we've got our resource with an ID of six that's cool to two hundred and we don't have a platform attribute they are so cool we're now returning command V DT of course that's we're getting close up we need to change this to a 201 and we need to also pass back the URI of this resource which is part of the race specification so let's just kill the web server and we're going to not make use of this return or key and we're going to make use of something called created at wrote no it looks a bit cumbersome the way it's the way it's written so I'm going to write out first and then I'll explain it as we call now before I even do that we're actually going to as part that creator that wrote operation we actually need to make use of this existing action rizzo queer result where we can retrieve a resource for more database now in order to make use of this we have to actually name it and you simply put that in here after the the other attributes that we have and to get that name name equals and I'm just going to give it exactly the same name as the method name no I I'll be totally honest with you I've had some issues with this my understanding is you shouldn't need to actually explicitly name it and you should be able to use the name of operator just to get the method name I've had some really weird behaviors with it so I'm gonna follow this convention because this way works but just be aware there are some there are a few gotchas with us and haven't quite got to the bottom of it one of them I experienced the point of good was to do with asynchronous methods which is a whole other story and I'm not going to cover that here all our methods are synchronous that's fine for the purposes of this video so anyway long story short we are actually going to make use of this but in order to make use of it we need to explicitly mean it and the name is just exactly the same as the method name hope will become a bit clearer when we know come on to doing what we're going to do so we are going to return and we're going to make use of created at Rhodes now those created action created that route I will cover off the differences differences between those and a blog article at some other point later but for the moment we're going to use creator net Road so what do we expect we expect the name of our method that retrieves a single command go source so I'm going to pass that into here then we have to create a new anonymous object which is effectively our command object ID equals command read DT or ID and then finally we pass back our command greed DTO so other said it's a little bit convoluted not very nice let's just test that to see if it works and gets the result that we expect and then I'll take you through what each of those bits means so save this and let's run this up and let's go back to postman and I think everything is set up as we expected we're going to have lots of duplicate resources and they are obviously that IDs are going to be different but the contents going to be seen we can tell you that out later so yeah everything looks good let's create send excellent so we get a 201 created fantastic we get our resource to baton that's jason fantastic and you can see here we had previously getting for headers returned we're now getting if F header with a location in it and this indicates the URL to the the resource basically and you can see here yes indeed there's the full URI to our resource so let's just copy that and we should be able to take that paste it into our postman instance make sure you change the gate verb and really confident if you click send we actually get back we go back to body we get back the resource that we just created so that's a really important part of the rest architectural principle if you create a source you need not only do you have to pass it back but you also have to pass back the location of where that was created so I did promise you that I would explain their created a drought method a little little better so just to refresh your memory let's just stop this and go than that and kill this this was I created a drought method here so rather than me trying to explain it from the ground up why don't we let microsoft do the hard work it's here on Microsoft documentation and it basically specifies what it was doing it creates it created a drought result the tool one created with the specified values now the values we passed and where the rope name this is the name of the Rope used to generate the URL sorghum back to a cord this was basically our action result that was done back a single resource okay so that generated that URI popping back over we then gather the Rope values this is the Rope data used to generate the URL cool so that's basically the number seven in that instance and then finally the content value to format the entire body and again that's just our command V DT or all right so it's a little bit unintuitive again that is a created at work was it called creature to action method as well take a look at that and look at what the differences are between the two but for now we are going to leave that and move on just before we do they'll give yourself another round of applause because we've now implemented three of our six actions and results so let's just take a quick sense check if we were actually in terms of the entire video we're really moving through this so let's come back to our overview we've done the introduction we've done the coding part one we've done our entity framework stuff we've done our data transfer objects and we have now just finished our endpoint for creating resources so really we've only got one more bit to go and that's doing updates patching and deletes and then we're going to wrap up and do some final thoughts so well done for sticking this far we're almost there but we'll get a bit more to cool so just before we actually move on to doing our update action results using put and patch I do just want to circle back to our post action result with regards the creation of data so I just want to show you something very quickly and hopefully this will make sense as to where I'm going here there's our postman instance let me just make sure that application is running and can we have stopped it let me run that up again and there are no data annotations on our command create DT own or either on our command read detail so there's no yeah there's no annotations on them at all we do have annotation ie that all these fuels are required at the domain model level okay so that's both valid a domain model level and also aiming to replicate that that requirement and scheme I don't our database is there as well so what happens if we don't supply one of these attributes when we try to create a request now according to our detail that's totally fine there's no data annotation or constraint near mother as a domain level so let's just send that see what happens and B okay I need to make sure that this is on screen we get a 500 internal server error and a whole pile of exceptions and you can see here this actually are an exception from entity framework saying there's a DB up a DB update exception and basically what's happening there you can see here : platform you can't answer a null value into it so that's definitely not a good state but feels to have no it's entirely possible that a client using our API and this goes back to talking about contracts may supply data like that to our client and we definitely don't want to throw a la blah and throw a 510 or sylvio because what that saying is the sale was made a mistake not the client and in this instance it's actually the client that's made the mistake not or so well it kinda has so we need to fix that up and let me do it it's actually really simple we just employ data annotations with it on create DT or so let me just stop this for the moment let me just we need to copy it's actually fairly self explanatory thus one can stay a little more complex and let's go over to our command create DT oh and let's apply those same annotations to our command create D fuel now what this is going to do it's not going to solve the issue of whether a client does or does not pass something through we have no control all of that let's just bring in the mean space but what it will do it we'll throw back a 400 type error or cord saying you've made a mistake nas and it looks a lot in a so I mean you have some detail around to the climb about what the problem is rather than flowing a stack trace back at them but you wouldn't do anything in a production environment so let's just save off our command create DTO let's run up our web server again and let's try that one more time and see what we get so make sure that's on the screen send that through it we go so we get a 400 bad request this time that's much much better and you can see here you get some nice Jason error cord the ton back and it tells us what the error is and that could be multiple arrows within there and in this instance the platform field is required so the client can then read that and understand what's going on rather than just throwing a horrible stack trace back so it's wanted to actually forgot about cycling back to that glad I remembered so yes very important that on our create and we're going to actually create an update DT or that you put data annotations in there because it makes error handling from a client perspective much much nicer and also doesn't make us look bad by throwing back 500 behind requests or cerebral error cords cool so I think with that we can now actually finally move on to our update air action results ok so we move on to implementing our put endpoint and that is one of two update in points that we're going to create so put this our first one excuse me and patches our second one and cover off obviously what both of those are so before big one at this stage now I wanted to get into a bit but let them sort of put together that plan of attack on how we will tackle all three of our remaining endpoints or what we need to do in the cord and how we should tackle it just to kind of yeah get it sent a bit about rhythm so this remainder of the venue should be relatively quick I hope so I'll give you an overview of what put requesters and how it's different from a patch request have it's cold so I'll just go and have a look at mock topping sample imports man what success chord we should expect back and then this is our plan of attack so we'll update our repository interface to reflect the need to perform an update action we will of course then need to implement that in our sequel repository you'll then create a command update DTO and then finally we'll go into our controller and we'll actually clear the action result that responds to it put request so what is a put request well as he fill update so by that I mean is you need to supply the entire object even if you're only wanting to update one of the attributes in it so for example our really small object has four attributes technically including the primary key so even if you only wanted to update the how to text and our object you still have to pass through the upper two attributes as well even if you're not updating them so they look bad user would stay the same so it's really really inefficient and of course error-prone so for large objects where you've got 30 or 40 attributes you've got to make sure you pass the exact same object back over if you want it to basically remain the same except for the changes that you're making and you may want to be making a change in one or two the attributes so inefficient over the wire it's error-prone especially for larger objects and it's acting not used so much know a patch is the field approach where we can do partial updates much easier and I'll cover that of course later but I'm including it because it's the traditional way of updating and a fuel you should be across how that works and for your purposes it may be fighting and for this project it's actually kinda low-key okay so let's just quickly pop over to postman and I've got a mocked up request here I mean just move that out of the way so as you will have seen before we have this or standard the URI probably where we need to supply the ID of the of the resource that we want to update so that is obviously important that we need to tell the API what single resource we are updating and we supply that via the URI and of course the veil that we said our postman and is put and these two things taken in conjunction are unique so coming down to the body text that we supply your one thing you'll notice straightaway and you might ask the question where's the ID field or the ID attribute I didn't mention you have to supply the whole object that is true but as the ID is our primary key and it's not going to be updated you can update it then we don't need to really supply that and I'm in my previous video I think I did supply the ID it's not really needed to be honest with you so you only need to supply the object that have updatable fields and ID is not one of them and we're also supplying in the URI everything else we need to supply and if you remember what our create pay Lord looks like it looks quite similar because of the way we structured our bond data validation so we'll talk about about that later in terms of a successful baton you would most usually when you successfully update the hold the API employed to perform an update you can either at under 200 okay and you can possibly supply back the object that's been updated I have done that in the past but more usually what you typically do for the put request is to supply back in 2 or 4 no Content response and it's then the clients responsibility to go okay that was successful and if they actually want to check that the object has been updated they can call the respective endpoint to pull back the death or two methods we're going to for the purposes of brevity but it's going to return a two or four and that's probably what you're more likely to see over there in the view world ok so I think that's enough of an introduction I think no we need to move on to actually calling up a lot put action result okay so let's just minimize this and move back over to our chord project maybe just check to see if the web server is running no it's not fantastic so as per our plan of attack the first thing we said we were going to do was update our interface for our repository now just before we do that I just want to reiterate the point that when we're defining our interface what a repository is agnostic away from implement so when you're designing any interface you should be thinking about not I'm going to be implementing that using a sequel server or an entity block core or I'm going to be implementing that using auto map or I'm going to be implementing that using whatever technology it's agnostic from that and you should really just be thinking about from a theoretical perspective what should this interface provide so logically speaking we're going to be creating update our API is going to be responding to update commands there happens to be using HTTP to do that in this instance doesn't actually need to do that but we're doing updates via our API so it makes kind of sense to think well our repository interface should provide some update functionality so why am I going on why am i labeling this point you'll see in just a minute why I'm laboring this point so it makes sense that in our interface we should be providing the ability to update a command I'm just going to follow base a lot but not exactly same pattern with the exception of the name to the upstairs signature cool so all good so when we come over to our mock repository it's complaining so again you can delete this if you want I'm going to leave it in there because I'm going to post it called up to github thing down below so you can have a look at the cords all they are felice or I'm leaving the mock repository and you can take over if you want now we move on to our sequel command or repo and just to refresh your memory our sequel commander before is an implementation class using ADB context okay so we're starting to think attached they need to think about how do we implement our interface methods now in this case because of the way dbcontext works don't actually really have to have an update method in here but we need to have any because we need to implement interface but we don't need to really put any code in here and I'll show you when we come on to actually doing the update why that is the case so rather than throwing an exception we're not throwing an exception we are going to quote unquote implement it doing nothing okay it may be that if you then switch out this implementation for another one you may need to actually implement your update method okay so it's a little bit counterintuitive but again interface and implementation are divorced from each other when we think about an interface we should be thinking genetically and theoretically about what we should be providing when we come to implementing it well unless instance we don't actually need to implement anything because it's taken care of by the DB context within well you'll see when we come on to doing it trust me on that so basically that's how it's done mixture I'll see the interface as well so we've updated our I command a report cool and we've updated our to implementation classes even though we don't actually need to do anything in there fantastic okay so I think the next thing we wanted to do as per our plan of attack was come on to doing or DTO I think that's right it wasn't it yep yep command update DQ so let's go undo that no all right so we want to update or add a new DT all for update now let's take a quick look at our command create DT all I'm just gonna move that up it's annoying me and basically as pale probably useless mocked up in Postma and we need to supply the entire object we don't need to supply the ID and if you look at our command create DT oh absolutely identical so technically speaking we could just use this and when we come to doing updates as well I'm going to actually create a new file a dedicated file and we're going to duplicate the cord which is very inefficient sort of the homework for you is to figure out yes let's have a command update DT oh so I want to have those separate artifacts with the separate definitions but how can we make the the fact that the command create d2 and command update the D two are going to be absolutely identical aside from their name so I'm just gonna be a bit lazy just copy that in and I'm going to update name update I'm going to leave the validation then as before because they are still of course valid I'll save this as well but yes we've got two absolutely identical classes now which is not maybe it's not good design at all but the interest of time and just to test you a little bit I'm going to set you the challenge to yes maintain those two separate classes but how could we make it a bit more efficient going forward and the reason we're having two separate classes as they want to in the future change how we updating create stuff possibly so well we're building for the future so that's a very simple command update dto is an exact copy of command create DT or if you want to figure out how to make that slightly more efficient chord and language or good object-oriented practices I will leave that with you all right so that stuff is done and now we come on to the more exciting part which is actually to implement our actual result okay so I'm just going to close them some of this stuff just to give us a bit of breathing space and minimize these so we asked we need to move over to our commands controller and add in our new action results so we have three existing we are adding a fourth and now so we are moving definitely moving through the video I'm just going to put in this documentation at the top again just to help contextualize what we are doing so again put verb and this is the URI that we are going to expect our endpoint to be called on so most not surprisingly we are going to decorate our action result with the HTTP put attribute and also as we're expecting an ideal then we need to specify that here as well as part of the road and that should look familiar from one of our previous action results fantastic now as I said we're not returning anything back from the sexual-assault were just returning a two or four more content so as such we besa fire action result without a tongue type alcohol ID command now we are going to expect through the ID that's going to come from our routes that's going to be banned from our route and then we are going to also expect the command update DTO which will come from our body text and we will work with that detail as we move through the action result cool so we are the client is expecting us to update resource the first thing we should check to see is whether that resource actually exists or not so let's do a quick check first so we're going to create a place hold or being able to hold that so command from report next caller can I hear from you just so what you know what it is well it's coming from the people so it's kind of unfair I guess but next thing so we'll use our repository we will call our gait command by ID to return the requested resource from the repository and put it into here and of course we want to check to see whether this is null or not seems to be a bit trigger-happy with the MS command model for me pool if it's equal to null then we just want to return not fund a 404 not found otherwise it means we have something and we want to update it so what we really want to do is we want to take the data from our command update DTO that's been supplied by a client and actually apply on top of our command model from the be pool now in order to do that you're probably going don't we need an auto map or mapping and our profile do you know what you'd be absolutely right we do in fact need that so let's go over to our commands profile and let's create a map and what are we mapping from we're mapping from a command update DT all I'm a mapping to our command so that should hopefully make sense we've become a delta map or I think quite a bit so our source is our update detail from our client and we want to apply that to our command object that we have retrieved from our repository so in order to use that we're going to call our mapper and we're going to call that on that and we're going to call it in a slightly different way but previously we were mapping from data to a new or empty object of some type via d2 or a model so for example when we were performing a retrieve from our repository when we use the mapper we were mapping from model that had contained data to a new empty command read detail for a post command we were using map twice so in the first instance we were mapping from mining that we're going to do here actually from a command create detail that contained data and our mapping into a new quote unquote empty command object here we have a command that the detail that has data and we have a command model from eople the both contain data so we're going to use a slightly different syntax it's actually a bit some plot we specify the source existing source object which in this case is our command update detail that contains the data that we want to map from and for the beam on the map to our command model form a repo fantastic so that basically map's the the profile that we've set up here and because the fields are exactly the same automata does its business no coming back to the fact that we have no implementation in our sequel repository class this mapping exercise here has basically what it's actually done is it's actually updated this model and those changes are actually being tracked via or dbcontext so we don't actually need to do anything else with it other than Save Changes okay so our more data has been updated in our context but we still need to flush it down into our database we don't need to do anything else with our repository however in the interest of maintaining a separate interface from implementation good practice is to still call the update method on our repository and supply and our command model from repo because other implementations may require that you do that so we're still going to call repository we're going to call up the command and we're going to pass in this command here which is intuitively probably something that makes more sense you would expect the update command in the repository to take our new data listing map through and push it down we don't actually need to do that just because of the way entity framework works and what this means is if you then switch out your implementation for something else that does require this you've already got the cord there and you don't need to perform any chord changes within your action result it's a little counterintuitive but it is it is good practice and then what we really do need to do is in Colt save changes on our repository and that will flush down the changes that were actually meet at this step here back down to our database let's just save that and then the last thing we want to do is we ton your content which is a 2 or 4 cool so a little bit possibly come to intuitive you may want to remark this out but that's kind of against the whole point means well just take it out leave it in it's benign it's not really doing anything and then if you do come to change your implementation as I see it's already there ok so I think with that that's our action results for our put request completed all we need to do now is test that to make sure that it works so that's there actually that's before we do anything else let's do don't let it run and that will pick up any bill there so you don't really need to do a dotnet build in addition that all looks pretty good okay fine so what up and running with that let's just minimize this and let's bring our postman back up so before we do that let's just return back all our resources and I need to just excuse me you just play about with this window and yes but there's all of resources we'll pick one that we want to update we've got quite a few diplucate sand here when we were playing with our create last time so let's pick number five currently the how to run a dotnet core app and the line has done it one so let's update that let's send that to put and I've already kind of pre-qualified this with how to ball back migration so it's relatively useful and will supply everything across so let's send that over and we're getting a bad request and let's see what's it seeing bad requests and valid with JSON string string should be incorrect the string should be correctly escaped what have we done wrong here okay there we go string should be correct this games pretty good that isn't it let's try that one more time there we go so we got two or four nor content back which is fine let's we quest that from our other action result and we can see here yes in fact it has updated how to roll back to migration and some dates and delaying as well am I not getting the platform back because we're using the command read DT oh cool so that all looks like that's working and as you saw we haven't formatted the Jason correctly it complained let's just take this out we'll make sure we format the JSON string correctly so basically what we're saying is we want to do an update again on seeing on that same resource but we're not going to supply all the the attributes that it needs so it should definitely complain bad request fantastic and it should tell us that platform field is required so all good again and then let's just test something random we don't have a resource of 45 and it's returning back ok it's interesting it's actually doing the validations Foster let's put that back in and let's send out again and it's giving us the 404 so it's doing the validations first and then once the validations pass the same yeah we're talking the 404 so that's all good that looks like a foot request is working as per our specification so time now to move on to the more interesting patch requests so let me just bring up my amazing PowerPoint slide I know you all love salt so dearly so patch endpoint plan of attack space similar to the last one contradictory the contradicting myself here there's less work but more work that make sense so I'll give you an overview of the patch request how it's called the use of a patch document which is really important and again what we should expect back spoil it all out it's exactly the same as the put request we're going to return to a formal content so that's covered off and we'll then of course create a patch action result and our controller now you'll notice there are those no mention of an update to our repository class and there's no need to create a separate detail which we don't actually need to do so sounds like there's less work to do but there is in fact some groundwork quite signal not significant there's a bit more groundwork we need to do to just get a patch request and stop up and running a bit plumbing required anyway before we do that let's just cover what the Jason patch standard is I'm not going to do this in laborious detail if you want to read the boat it is specified in RFC 619 or 2 but what you should be aware of is that there are six opera now at the high level Patrick race that should maybe take a step back is allows you to do partial updates so before you saw we have to supply the entire object not very efficient what patch allows you to do is pick certain attributes and do certain things with those individual attributes such as adding new attributes to your object now we can't do that because we're using a static object I may do another video on dynamic objects which is definitely who saved the scope of this that's what the add would do it add new attributes to your object some love remove replace is the one you're going to be using the most so basically will be and supplying a path to an attribute will supply a value and then we'll call the replace operator to replace the existing value with a new value copy and move are self-explanatory test is really supplying a path to an attribute and a value and T and the test is basically the test operator is to see whether the existing attribute matches the supplied attribute so quite a useful one so here's an example of a patch docking that we're going to send over so we're not going to send over the Jason that we had sent previously for our update request we're going to send something like this so it begins as an array adjacent array which is square brackets and then we will supply our first operation and you can have multiple operations within a patch document so you can see here the operation is denoted by the key or P and then you tell it what you are wanting to do and that's one of the six operations that I just mentioned the last thing the path is the path to the attribute you want to look at and you can supply a nested attributes if your objects are more complex than our ones and then the value is basically the value you want to in this case replace the existing value with on the example here we are performing a test so we're looking at the line attribute in this case I'm you're testing to see whether the value is donate new or not couple points all operations need to complete successfully in order for the the path request itself to be successful and if they're all not successful then none are successful that's it okay I thought I'd have more not so that's about it so all good so yeah it's just to reiterate Patrick rest is partial updates we supply over it's like a change say or a patch document with all the step changes that you want to perform on your resource as opposed to just put anything into them so much more efficient it's used more widely now and definitely if you're doing API stuff you really should be providing a patch patch action result or API endpoint which is what we are going to do No all right let's clear this though now as I mentioned there's a little bit of pre-work we have to install a couple of packages basically and I'll just get the patch stuff working it doesn't come kind of out the box with what we've already got so let's just kill our web server we'll leave a command line running as we need to install a couple of packages let me just clear this down and let's open up our CS proj file so you can see your existing packages in there so I'm gonna pull up a new get again and I'm going to assure you the first package that we need to install and you'll excuse me if I don't type it and I'm sure you're sick you've seen me do that the first package is Microsoft dot ASP dot dot asp net core decent patch so probably a relatively simple rationale for why we need to include that it's basically asp.net core support for JSON pack so let's not waste any more time about explaining what that's doing is basically giving us the support that we need to include patch requests and an API so as before copy that in and it should be added into CS pod file that's all good that we do actually need another package and this is all to do with how we serialize and deserialize data and we're actually gonna make use of the Newton soft and Jason library but it's not the core library it's one that's specific to NBC so this is the one we want Microsoft ESP net cord NBC Newton soft Jason and yes this is required as well no I'll call out in the cord for you know this is specifically being used and we actually do need to do a little in order to use actually we do need to do a bit setup in our startup class as well so all those just copy that and let's paste that in here too and I'm gonna not use the preview version use the latest version and that's going in as well so just double checking we have got our asp net cord Jason patch and our asp net core NB c-- news and salt Jason packages okay I think we can close our command line and down so and close this done so as I said in order to use that second package we do need to do a little bit of setup within our startup class now the first thing I'm going to do is I'm actually just going to include the the using statement that we need rather than pulling it in automatically just saw that when we contact the rating the core that it can auto populates and it's just a bit of a more seamless experience so we want to include mutant salt Jason serialization so when we come to using it now it just all auto populates I me need to just append some configuration onto the end of our add controllers method here so what we need to do is add mutant soft Jason to this real then going to use a lambda expression to perform some setup curly brackets s goes to serialization serialize or settings contract resolver and we're just going to set up in you I think it's camel case yes camel case property name names contractors Oliver but my mouthful and again I'll call when we are actually making use of this piece of wonderful config it's not it's a bit cumbersome to be honest with you I think we need to make this a little more intuitive and a little more out-of-the-box which I'm sure will do in subsequent editions of the framework but I don't really like this to be really honest with you but nonetheless this is what you need to do to get it all working okay so that's that bit of people done that it's a little bit more pre work than we need to do I mean it's not huge but I will ruin the surprise for you we basically have to add another create map within our commands profile and this time it's a create map the other way from our command to our command update DTO and again I'll call this out when we are making use of this in our in our controller but what I think it's worth pointing out is that just because you set up this map this way so for my command update DTO to a command it doesn't automatically mean that you have this mapping set up the other way you have to explicitly set that up here - okay which kind of makes sense because we're talking about a source and a target so we're saying this is a source this is the target we have today and explicitly do it the other way all good so that that's a bit of pre-work setup we can now move on to our commands controller and actually get on with the business of writing up our patch request a patch action result should I see okay so let's just do our documentation so patch API commands and we are also expecting an ID so that the signature is exactly the same as what put except from the day up that we're using of course and again we're passing back a tool for no no content so we don't our action result doesn't actually need to return any object data or any have it have a return type so public and it might come from public actual result and we'll call this partial command update as we're only gonna be partially updating it hopefully make sense and again we're going to expect the now just forgotten something before we do this let's move back up here we need to decorate this with what you think the HTTP catch attributes of course and we need to supply in an ID a few times I've forgotten to do that actually it's got me into some trouble so yes before we need to create our action result we need to decorate it with HTTP patch fantastic so yes what prompted my memory was we are going to expect the ID to come in from the URI which we get from this and instead of expecting a command update DT all we're going to expect something slightly different we're going to expect a Jason patch document of type command update DQ I will call that patch top okay just put some curly brackets in here now it doesn't know what this namespace is so we will I think I've taped that in long I have document right now again yes so using Microsoft ESP net cord Jason patch so that was the first package that we had added in that was required to get to drive this so yes we're expecting a Jason patch document and you supply the type which is the date command update DT oh cool now in exactly the same way that we did up here we are requesting at the end of the day to update a resource so the first thing we should actually do is check to see whether that resource exists so I'm just going to copy this cord I hope you don't mind at this stage I'm sure you don't and it's exactly the same we just want to make sure that the resource is there cool so just to step back again what we are receiving from our client is this patch document that we want to apply to our ultimately to our command model but we can't apply it directly to our command model because this is an update DT or patch document and this is a command model so what we actually need to do is we need to in order to apply the patch we need to create a new command update DT or effectively so call it command to patch and we will use auto map or to map to our of type command update DT oh and what is the source the source is model here so what that's effectively doing is it's creating a new command up detail with the contents from our repository and it's putting it in command to patch so that is making use of just to step back here is making use of this mapping let me just put in so the commander's our source and we're mapping it into a command update DT okay so that's why we needed to put that in then this is where we actually make use of our patch doc up here patch doc and it has this apply to method on it and what do we want to apply it to we want to apply it to our command to patch that we've just created here and then we need to make use of something called model state and this is where we require that other package to commend because without that this would actually not work at this point in time and all the model state is doing all the names quite self-explanatory it's just making sure that the validations are valid basically but we do need to do a quick check so we need to do a validation check on that so if try validate model command to patch then leave at own you validation problem and we pass back this model state let me use the pump cool so basically we got a patch document from our request we check that we have a resource to update from our repository which is a model we then need to generate a empty or a new command command on the DTO we use the data from our repository model and then we apply the patch to that and we don't that the validation check to see whether it's all good if so cool we progressed on and this should actually start to look really something or no so what they want to do is we basically want to update validations checks we actually want to update no update our loading data and a repository so it's a similar concept at that we had from Rp in fact we can just copy this here the only difference will be that we are not mapping up and I'll update detail by mapping this command patch D teal so our validation is passed on this a command update Lethe view and we're now applying it to our model again our DB context has basically it's tracking the changes of that model so it's changed but we do or we should call our update command again or redundant update command it's not really doing anything at this stage that's our command model for me put this has now been changed and then as before we are calling Save Changes on our repository and as before we're going to a tunnel email content back for successful so hopefully that made sense this is probably the new stuff that compared with the put action result above where we just apply the patch document and validate or otherwise is pretty much the same okay so I think that's looking ok we'll find out if it's not and when we come on to testing it now all right so let's get command line up and running which I select the correct screen and all the dots that run to get our web server up and running cool nor complaints or that looking good so let's move over to postman to test our patch results and the first thing I want to do is just let's just get a list of our existing resources and pick one there's a target how about this one number seven no no no some playing with that one I've been playing with that one that looks like so let's pick number six so run a.net caught up with a line value of dotnet one so to test our patch result let's get rid of that and body text I hope you don't mind I have one off lying here off off to the side here that I'm just going to paste in and you'll remember from the PowerPoint we started with some square brackets to denote JSON array and then we can list any number of operations so here I'm specifying just one operation it's sort of place operation the path is to our how to attribute on what did you say we're going to do six so it's just polite in there so now all the houses are the same but it's going to know this one here it's going to change this how to attribute to some new value and we can change that to a heart's content and yes obviously we need to pass in the resource ID up in our URI and finally we need to set this to hatch I think that's all good I think it's looking good yep let's hit Send and we get to a foreign or content returned which looks good and let's retrieve our resource to see if it's being changed so path should be some new value and indeed it is so well done have done a successful partial update give yourself a round of applause let's just see if we don't supply a value of what happens we should the validation checks should kick in and we should get some nice JSON back saying you've not supplied you know data so let's try that not forgetting to switch that back to patch and indeed we get a bad request for hundreds once or client mistake and one or more datian arrows and tells you what the error is that's all good and of course if we put something and we don't have an ID then we get a 404 not and so that looks like our patch a patch result is working as well so yeah give yourself a round of applause five gold stars and a smileyface sticker it's possibly the most complicated action result of our API so well done we've got that done so we're moving on to our last action result no it's actually our thing arguably one of the simplest ones we don't need a DT or or anything like that it's our action result to delete resources so let's let's wrap up with that last action result so you'll be gutted to hear that I don't actually have a PowerPoint slide for delete action result it's quite simple and I think we are at a stage where you don't need that now it's a fairly self-explanatory actual result so just thinking about our plan of attack and how we would tackle it the first thing we would look to see is whether we need to update our command or repository to include an update update to include a delete command and indeed we do and you'll pass any command that we want to delete all good so sort of repository updated you'll see that our implementation classes are complaining so let's just implement the interface whoops here we go doing anything with more people and then in our sequel commander repo we will implement that amiable actually do something there fantastic so although it's probably not strictly necessary we can check to see whether the command was a real check to see whether the command of the resource is present in a repository from our controller we'll double check again here as well and then we will use our DB context to go to our commands and then remove or command and again people after calling this rebill again have to call Save Changes to watch those changes done so that's all I repository interfaced on their commander people interface done that's our concrete classes updated fantastic there is no need for a DT or as we are just passing in the resource ID in the URI so let's move over not to our commands profile to our commands controller and yes there is no mapping as there's no detours so let's move back over to our commands controller for a final action result now the like with the patch and put requests we're going to Vuitton in your content in the past I have actually retardant the the JSON object of the deleted results back when I've called the delete you can do that if you want but it's not necessary so let's just do a final bit of documentation delete and the URI API commands and we are passing in an ID cool we're going to decorate our action result with the HTTP delete attribute of course and we are again expecting an ID to be passed through cool partial public action result we're not passing back any data so we're just going to call this delete command and all that actually going to expect to come through is an integer over ie you will use to delete so in a very similar way like we have done with put you put patch put put and patch we are going to check to see whether the resource was found or not if not over tunnel fund a record to our client otherwise if it has been found then all we really need to do is go to our repository and we need to call our delete command we just pass in our command object that we want to delete in this case it's our command model from repo cool and then don't forget to save the changes fantastic and then we the tunnel no contents fantastic alright let's save that off and let's move straight or what to testing it bring up a command line and up keep the dotnet run it well good and let's move over to our postman instance let's do we get all of all our resources and it's selected target to terminate how about 6 & 7 because they're looking at John Key ones now so we will select delete I'll put in the ID of 6 we don't need any it's like none they are actually and let's send that cool so we get you know content ritand let's try and get this resource you get a funnel for not fund which is absolutely correct and that's basically it to be honest with you let's just get a list of all our commands and we can see that yes paroled number of 6 resource has disappeared so with that we have completed all our action results let's just take a very quick look at everything we said we were going to implement bring up yep so we have done I think we've done all of that we aren't going to implement this last one period deleted their collection of resources it's probably a bit too powerful there's nothing wrong with doing it putz yes let's leave that they are for today that was a long video much longer than I had anticipated so if you made it this far congratulations and thank you as usual if you get any comments or questions please leave them in the comment section below and I'll be delighted to read those and answer as many as I possibly can if you liked the video please give it a like and if you're not done so already maybe think about subscribing you'll be notified whenever I put anything new out for those of you watching on patreon or who have signed up to my patreon channel massive thank you to you as well and your names will be appearing at the end of the video by way of a special thank you and Shoto until the next time I'm wishing you the very best of health and happiness and to your families as well and until the next time stay safe and I'll see you around [Music] [Music] [Music] [Applause] [Music]
Info
Channel: Les Jackson
Views: 628,362
Rating: 4.9783602 out of 5
Keywords: .net, .net core, 3.1, REST, API, MVC, .NET Core API 3.1, JSON, Data Transfer Objects, DTO, Repository, Dependency Injection, Di, CreatedAtRoute, c#, Les Jackson, course, tutorial, step by step
Id: fmvcAzHpsk8
Channel Id: undefined
Length: 207min 47sec (12467 seconds)
Published: Wed Apr 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.