Domain Layer and Coronavirus API - CORONAVIRUS WPF MVVM IN .NET CORE #1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in part zero of the series I talked about the application that we're gonna be creating and I also put together some UML diagrams that demonstrate the infrastructure that I plan for this application they have now if you didn't watch that part no worries basically the summary from that part is that we're going to be building a WPF mvvm coronavirus application that shows statistics about countries with kuranda virus cases and we're going to be displaying that data in a chart and the goal is to display the countries with the top ten amount of cases and this is gonna be great so I'm gonna have this application I'm gonna be able to track all the coronavirus country statistics I won't have to watch the news anymore I can just have this on my computer and whenever I see you the case is starting to go down I'll be like oh I guess quarantine is over I can go outside so we planned all that out in the last part there's some UML diagrams I'm gonna be showing from that part in just a little bit but for now let's get started and let's create our project so we have a blank Visual Studio right here no project no solution so let's go ahead and create that project it's a new project we're gonna be doing a WPF dotnet core app let's select that and I'm gonna name this application the corona client and I'm gonna have to change this path real quick and there we go so I'm gonna put it in its own folder of course and we'll also just call the solution Corinna client as well I know a lot of people including myself like to split their solution into many projects such as the domain layer the UI layer but for this this is going to be a pretty simple application so I'm just going to keep everything in one project and if I need to split it into multiple projects in the future we will do that refactoring but I'm I'm pretty confident we're just going to be keeping it simple so let's go ahead and create and there we go we have our blank WPF application now the first thing I want to do is implement our domain layer for this application and that is what I have implemented in the last part in our UML diagrams so let me go ahead and bring those up real quick and we'll do a little bit of a split-screen and implement those classes and interfaces so here is the UML that we implemented in the last part again not very advanced not a lot of content but I had people asking about UML so I figured I'd give them a small demonstration so this pretty much makes up the domain layer for application so let's go ahead and implement this layer in our WPF project so the first thing I want to do is create a new folder here and we're gonna call this models and inside here it's gonna hold all of the models of our domain layer so really the only model we have is this coronavirus country it's a model because it basically just holds data for us but it's gonna hold all this data that represents a country with coronavirus let's go ahead and create a class for that I'm just gonna call it corona virus country and we'll make it public and then implement all these properties so the first property of course is just the country name and then we're also gonna have a property for the case count so the amount of coronavirus cases in that country and then lastly I decided to have a property for flag URI so this is just going to be a URI that points to an image of the country's flag and I thought that would be useful to display somewhere on the chart on our WPF application so that's actually all we need to do for our coronavirus country and that's really the only model that I plan on having in this application at this time so now let's go back and look at our domain layer another thing we need is this coronavirus country service so this is gonna be a service class that will give us coronavirus country data so it's gonna make up our service layer it's not going to be a model so let's go ahead and create a new folder for this and we're gonna call it service and let's create a new item here this is gonna be an interface and it's just gonna be our I kiruna virus country service make that public as well and let's implement that method that we talked about now for this method we're gonna return a task of an ienumerable of corona virus countries and I'll explain that return type in just a little bit so let's import all that and we're gonna call this get top cases of course because it's going to give us back the top countries with Korean virus cases and it's going to take an int for the amount of countries that we want back and that is all we need to do so this return type we're making this a task because we want to be able to use asynchronous programming so we want to be able to await this task without blocking our UI when we make the call to get the top amount of cases and then after we await that test it'll give us back basically a list of Korean virus countries and that's really all we have for our domain layer so we can go ahead and close out of our visio go back to full screen and now we're ready to actually implement this current uh virus country service so to do that we're going to be using an API and I'll show you guys that API with postman real quick so let's create a new request let me grab the URL before the request paste it in here and let's send that request so we send the get request to this URL as you see we get back a big ol list of data for countries with coronavirus so here we see China the amount of cases 81,000 and a cool thing you can also do is pass in sort parameters so we can say sort and then we can sort by cases let's go ahead and do that and as you can see now we have the USA at the top with almost a hundred thousand cases Italy right behind them so the sort is working so we're gonna be using this API to get our data that we're going to display in our graph let's let's go ahead and head back to our project and what I'm going to do in our services folder right here is create another folder for API so basically in here it's going to have our implementation of this interface and all the other classes that that implementation needs so let's create a new item here and we're going to call this first of all it's going to be a class and we're gonna call it the API corona virus country service so it implements the service by using the API so let's implement that interface and make this class public and as we recall we made this a task so we can make this method async so what we call it we don't block the UI so now let's go ahead and actually implement this method so the first thing we want to do is create an HTTP client so let's specify that inside of this using statement and we're putting it inside of a using statement so that when we finish up in this method this HTTP client is automatically gonna get cleaned up for us so let's import that and now we're going to use this HTTP client to make a request to that URI that we just showed in postman so let's specify that URI right here we'll call this request URI and it's gonna be a string pointing to the URI let me just grab that endpoint real quick there we go so now that we have the URI we're gonna send the request using the HTTP client so we're gonna use client dot get async and then just pass in the request URI and of course this is an async method so we're gonna have to wait it and it also returns an HTTP response message so let's get that message so we can read the data that comes back call this API response and now that we have this message we have to deserialize the content of the message to get the JSON so let me bring up postman basically once we deserialize the content we're gonna get all of this inside of a string whoops let's go ahead and do that so we're gonna make a string we'll call this JSON response and we're gonna await we're gonna use that API response this HTTP response message we're gonna take a look at the content and we're gonna read it as a big fat string a sink and it's gonna give us back all that data but we have this data we need to get it into our model we need to make it readable so we can actually get coronavirus countries and to do that we're gonna have to deserialize this string and we're gonna have to deserialize into actual objects so those objects that we deserialize into are going to have to match the structure of this json so right off the bat our first object is going to have to be a list as you can see by this bracket that denotes a list of objects and then that list is gonna have to be made up of an object that matches these properties and then say we want to get the country info which we actually will cuz we want to get this flag then this country is going to need an object property inside of it for country info with these properties as well now we don't actually have to specify all of these properties we only need to specify the properties that we need so we only need country we need cases and then we need country info with just flag so let me go ahead and move the server here for now and let's create those objects that we need to deserialize into so in this API folder I'm just going to create a folder called models not to be confused with these models these models solely belong to the API so the first thing I want is just like the route object so that was the big ol list of countries so we'll call this coronavirus country listing and I'm actually going to prefix all these with API so we don't get them confused with our models these are the API models so inside here it's just gonna be a simple class with one property and that property is gonna be the list of the corona virus countries from the API which we haven't made a we haven't made a model for but we'll just call this countries now let's go ahead and make that model so we can actually do this we'll call this API current of our country and then control dot and we'll generate that type in a new file so now we can save that and we should be done here so we have this new coronavirus country from the API and if we look at postman let me bring that up again we're gonna need a country property and a cases property and also a country info property which will be its own object let's go ahead and do that so this string for the country which will contain the country's name and then we're going to need an integer for just cases and then we're also gonna need a country info so this will be API country called API coronavirus country info and then it will generate that class and then you file as well and we need to name this country info so that's an important thing the naming does matter we need to make sure these names all match the names on our object so that when we deserialize the deserialize err knows what to do so let's go to our country info and all we need here is I believe that's string for the flag let me just bring that up yup just flag and Oh give us back this resource URL right here that'll point to the image of the country's flag so that should be everything we need to deserialize let's close out of all of these I think all is good here so now back on our API coronavirus country service we're ready that actually deserialize our JSON into our API models so the root model is going to be an API coronavirus country listing its call this API country listing and to deserialize this we're gonna be using a JSON serializer let's go ahead and import that import our believe we spilled the strong did we Oh Corrine a virus not Corona there we go alright so we're gonna use this JSON serialize ER and we're gonna use deserialize so inside these brackets we can specify what we want to deserialize into which is going to be this api current a virus country listing and then inside here we just need to specify our JSON which we have from our api so there we go and now we'll be able to use this API current virus country listing so what this listing has is our list of countries but now we have a problem because this is an API current of our country we need our current virus country on our domain layer so we need to map these countries to our domain countries so to do that we are just going to use our countries from the API and we're going to use link a select statement to map these API countries to our domain layer so inside the Select statement we specify a lambda so it's gonna take in each of the countries from our API and that's going to be represented by this API country and using that API country we're going to return a new current of iris country in this current of iris country we're gonna specify the properties that we wanted to have so start off with country name which we can get from our API country and as we recall that was just names plain old country that property from the JSON has the country's name and then case count we can use our API country again and that has the cases in this property and last but not least the flag URI we have on our API country as well inside the country info in the flag property and there we go so we're mapping each of our countries from our API into a career out of ours country and then we're simply just going to return that and that's pretty much it so now we're ready to actually test this out so let's head into our app zamel dot CS and inside here we can specify an override function for one startup and inside here we can write some code that will run when our application starts up so all that I'm gonna do here is create our API corona virus country service let's import that I'll call it country service and just create a new instance and now we're going to use that country service at the top cases and we didn't actually specify this parameter for the amount of countries to mean anything yet we just want to test out our API so let's just throw a ten in there and then we're gonna have to make this an async void which is okay in this case and we'll await this and then let's take a look at the results so we'll just put that in a var for now cuz we're just testing this out let's put a breakpoint here and now we're ready to give this a shot so let's run the application and now we actually get an exception here so there was an issue with our D serialization so let's go ahead back in here and let's just make sure that we're getting the right JSON back so let's run this again we have a break point before we deserialize so we get our JSON and it looks good to me so perhaps our JSON d serialization just simply is not working all right so I did some research and I should've known this but if we take a look at our JSON as we see our root object is a list now that list doesn't have a property name it is literally the root object so that means we can't DC relies into this API coronavirus country listing because it's looking for a property named countries on the JSON that has a value of the list but that's not the case our actual JSON is the list so that means on our current anonymous country service we need to deserialize into a list of coronavirus countries or api current of ours countries so let's go ahead and change that and we can call this api countries and then we're just going to straight-up use that list of api countries and map that into our current or RS country on our domain layer so this actually is a lot cleaner and we can even lete this karara's country listing object however that is not the only issue we have so when we deserialize into this api current of our country the property names are actually case sensitive so let's look at our country and as you can see this is an uppercase C country but on our JSON it's a lowercase C country so what we need to do is on our service when we do serialize we can pass in an additional parameter for JSON serialize our options so let's create one of those and then on those options we can specify that we want I believe property named case-insensitive and we want that to be true and there we go we should be able to look at these countries and get the right amount of data let's put a breakpoint there and there we go we don't get any exceptions let's look at our countries and there we go we get them out of cases the country name and the flag URI as well and now get rid of that breakpoint go to our abs Amal if we continue look at our result now we have our coronavirus countries so we have our domain object and one last thing before we wrap up let's actually implement this amount of countries parameter and to do that we're just going to go into our current virus country service and right after we get these api countries we can specify a link method called take so we're going to take the amount of countries and basically what this going to do is only take the first X amount of countries from this list of API countries so if this is 10 it'll only give us back the first 10 and once we have those 10 then we will select them into our domain object so this is a little bit inefficient unfortunately that we have to call the API and get all 200 countries back and then filter them after we get them in the objects but unfortunately this API doesn't allow us to put a limit on how many countries we want back so this is what we are gonna have to do in the meantime now that was actually all the code we're gonna do for this part and before we wrap up let's actually create a git repository so I will get an it right here and then let's look at our source code actually want to create a get ignore file vim dot git ignore and now inside here we're going to specify what we don't want to include in the repository so that's gonna be tough yes so everything in that directory bin obj and I'm thinking that's everything so we can save that now we can add everything look at our git status make sure we don't adding any files that we don't want so yea we ignored our bin file all right BJ file or dub yes file and now we pretty much just have our source code so now we're ready to commit and we're just gonna call this add to mean add to main layer and I'll have to add my username and email so I'll do that off-camera but I'm also going to be posting the source code on github so be sure to check for the link in the description if you're interested in following along from github so that's going to wrap it up thank you guys for watching if you have any questions criticisms or comments be sure to leave them below in the comment section but other than that leave a like or subscribe for more and stay safe out there
Info
Channel: SingletonSean
Views: 2,973
Rating: undefined out of 5
Keywords: wpf, mvvm, easy, hard, difficult, switch, views, microsoft, visual, studio, binding, viewmodel, property, class, interface, learn, how to, architecture, pattern, new, toggle, button, content, main, programming, tutorial, full, stack, entity, framework, model, access, object, core, .net, c#, service, layer, project, development, refactor, clean, update, delete, abstract, view, element, custom, domain, planning, coronavirus, covid, covid-19, china, cases, united states, stats, statistics, api, client, count, chart, graph, http, json, deserialize, uri, postman
Id: f7_CVWsd5MA
Channel Id: undefined
Length: 23min 57sec (1437 seconds)
Published: Sun Mar 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.