From the course: Introducing ASP.NET Core

Adding a service to an ASP.NET Core website - ASP.NET Tutorial

From the course: Introducing ASP.NET Core

Adding a service to an ASP.NET Core website

- Hey, friends. We're creating an asp.net website. We're doing it the 101 style, real slow, very deliberately. In the last video, we were starting to bring in data. We haven't actually displayed it yet because we want to do this right. We made a little JSON file, and that's our database for this purpose. And now what we're going to do is we're going to add a service. And asp.net has this concept of services, and we're going to try to do that in a very deliberate way as well, so you're clear on how we're doing it. - [Leslie] So why even make a service to begin with? Why can't I just stick this code wherever I want? - That's a great question. So we try to do this in such a way that it is going to be maintainable for the future. - Makes sense. - We could theoretically just make one or two lines of code that open this thing up and just splat it out to the screen. But other people might want to use this. We might want to be able to reuse it in different places. So we're going to try to be a little bit more deliberate and use some of the cool features that are built into asp.net to make that possible. - So in this context, service is kind of like if you're at a restaurant, and a waiter asking what you want. You can request- - That's a really great point, right, because a service in the context of the computer is a I need something, and I need you to get it done. I don't even know how you do it. If the waiter, I don't want to- - I don't want to do the work. - Don't even talk to me, just get me that thing that I need. - Yep. - Right? So what we could do, of course, is make it so our webpage loads that file up off disk and then just goes blip. But let's separate some concerns. Let's have what's called the single responsibility principle. - Yeah. - Okay? So where do we want to do it? I remember that in the last video, you had me make a Models folder, and inside that is Product. - [Leslie] So why don't we do something similar where we create a Services folder in that same spot, so in the root of the project. - [Scott] Okay, so we're going to go and say Add, and I'm going to say New Folder. And you want to call it Services. 'Kay, we'll put all of our future services in there. And then right now we're storing our data in this JSON file. So this is kind of a JSON file service thing. - Yeah. - Yeah. - [Leslie] And we got to have a spot to actually extract that data to begin with, right? So let's create a file and call it JsonFileProductService. - JsonFileProductsService. All right, that makes sense. It's very descriptive and tells me exactly what it does. Okay, so in the, this JsonFileProductsService is going to be a little complicated. So for the purposes of this, rather than you watch me type the whole thing, I am going to copy/paste this from a GitHub gist. You'll be able to find that in the descriptions or in the GitHub repository for Contoso Crafts, so that if you're following along, you can do that. - Oh, and I was looking up the gist earlier, and they were using product service, not products service. - Okay. So we'll want to watch for that. So I've said products, and you might have a similar thing if you're following along, where you spell something slightly differently. Good call, we'll watch for that. So I'm going to go and change that. I'm going to paste over the top. So I'm taking this, and I'm pasting over here. JsonFileProductService, and that's fine. I could even rename the file to make it match. - [Leslie] No problem. - [Scott] All right, cool. So we have here a lot of code. And suddenly things became very scary. Why don't we do this? When you're in Visual Studio, it is a text editor. It's kind of like Notepad, but it's a smart text editor. - [Leslie] That's true. So that means it can probably identify what the functions are that we just made, right? - [Scott] It can, and that's a really great point. If we go over here, look at this, we can actually zoom in. Let me try zooming in. And in the solution editor, we get our properties, the ones that are private, see the little lock, the methods, that's cool. But also, the smart text editor will allow me to collapse. - Well, what do you know? - Look at that. It's called a collapsing text editor. - [Leslie] So much nicer. - It is, and it's also less kind of, for lack of a better word, threatening 'cause it's like, ah, scary! - Or if you just want to get all the clutter out of the way. - Absolutely, out of sight, out of mind. - Exactly. - So let's see what's going on here. We have a thing called JsonFileProductService. This is our constructor. Well, web applications are hosted. They live in a host. They're actually console apps. I've been making console apps with Kendra. Do you know that an asp.net app is a console app as well? - [Leslie] Oh, small world. - [Scott] So if you go to Program.cs, there is a Main, except it's not a main that says Console.WriteLine. It's a Main that makes a host. - That's cool. - Yeah, so when- - [Leslie] Just tell it something else, what to do as well. - [Scott] Exactly. So I want to know about that environment. What's going on here? We have an IWebHostEnvironment, and then we're keeping it. We're basically, we're getting it passed in, and we're keeping it. - Yeah. So I think how that is going to work is that it will allow us to retrieve that JSON file, so we can let this web host environment know the path where this file comes from. - Right, so that's a - And it can retrieve it. - great point. In this folder right here, we have wwwroot, data, products. I don't want to hard-code that. - No. - C:\scott- - [Leslie] Absolutely not. Chances are, we're going to mess it up anyway. - [Scott] You know I'll mess it up, right? So the web host environment will know where things are located. So what I'm going to do is in my constructor, I'm going to have an argument that asks asp.net, hey, gimme one of these. What's cool about this is I never have to make one. There's nowhere in this code where I new it up. It's just handed to me. And then where is the file located? Look at this, this is interesting. We made a property called JsonFileName. And again, either one of us could have just hard-coded SQL and backslash. But what we did is we said, "Hey, WebHostEnvironment, "where is the web root?" 'Cause your disk and my disk and their disk is different. They might be in the D drive. They may be in SQL, and Leslie, who knows. - [Leslie] And if you move it somewhere else- - you don't want it to break. - Right. - [Scott] Right. So this is kind of cool. We're going to use another library called system.io.path. We're going to combine three parts. So the first one is wwwroot. The next one is data. And you can combine as many paths as you want, and they're taking care of the slashes and the backslashes and the this and the that. - And the perk about putting that products.json file in the wwwroot folder that we had is because that WebRootPath will just automatically go to that folder. - It'll be absolutely reliable. It gets the path to the directory that has web-servable application. Now, we could've put that in a number of different folders. And WebHostEnvironment also has other ones, like ContentRoot. So there's multiple places we could've put it. So maybe if you're an advanced person, and you're watching, you may have an opinion about that. Just know that there's lots of different places here. So the one we're using is WebRootPath. So WebHostEnvironment, we'll talk a little bit about that in a minute, is a service that is being given to our service. So it's a chain of services. - Shared services. - Mm-hmm, yeah, shared services. - Sharing is caring. - Indeed. Here's the part that gets a little intense. And this took me a second to get my head around this. - [Leslie] So what's happening here? We are retrieving the products. How are we doing that? - [Scott] Yeah, so IEnumerable is not list, 'cause I've been doing lists. - Yeah. So in this case, now that we've retrieved that JSON file, we have to convert that text into the actual product that we defined back in the last video. So to do that, we talked before about serializing, in this case, we're de-serializing. So we're taking that JSON text and converting it into the object that we created. - Right, so let's look at that really briefly, remind ourselves, here's what the JSON looks like. 'Kay. Here's what the product is shaped like, right? And we're going to have a list of these things. What we're going to do is we are going to open up that text file with that file name we made before. As you said, we're going to de-serialize, and this is kind of interesting. We're making an array of products, not one product, but products, plural, and that means an array or a list of products. We're going to read that JSON file to the end. You can read just one line, but we want it all. - [Leslie] All products are equal here. - [Scott] Mm-hmm. And here, we can pass in some optional options. So these are not required. They are optional. And we're saying, "Eh, don't really care "about uppercase, lowercase. "Just make it work "'cause I can't deal with the stress." - [Leslie] Yeah, agreed. - [Scott] Mm-hmm. And then this is interesting, an IEnumberable. I was using lists. - [Leslie] Me too, I typically use lists. - List of this, list of that. An IEnumerable is like the great, great grandparent of lists. It's stuff that you can for each over. That's how I think about it. So if you take a look at the video that we did with Kendra on C#, we talk about for loops and for eachs. Things that you can for each over are things that you can enumerate. Therefore, they are enumerable things. - That's nice 'cause I'm not limited to just having to use list. I can use whatever kind of IEnumerable that I want. - Exactly, in this case, I'm using an array. I could use a list. I could use a collection. It's totally up to me. So let's just recap. What we're doing here is we have a service who's got one job, and to give us a list of products or an enumerable of products. But how do we tell the system about that? How do we make it, I don't know, publicize it? We have a waiter now. Tell everyone in the restaurant, there's a waiter. - [Leslie] Yep, so in that case, we have to get the file and then open it up and then convert that text into the product object that we created. - [Scott] Mm-hmm. And we need to make sure that asp.net knows that this is an available service. So I'm going to go over to Startup. - [Leslie] Right. - [Scott] And in Startup, this is a special asp.net page. This is a special asp.net bit of code, rather. Pardon me. - So in the past, this was usually one of those files that I'd just sort of ignore or glance over. I assume it's not important because there's a bunch of complicated things on there already and- - To be honest, it does look like a bunch of boiler plate code. It is. - Yeah. - With the program part of the Program.cs, this is usually Console.WriteLine. For us, it's CreateHost. And here we say, "Hey, use Startup." That's our Startup class. It can be called Fred or FU or whatever. We called it Startup. What it is is it's a special file that has a couple of methods that we want to care about. And the methods are Configure and ConfigureServices. And this is really interesting because we never actually, again, make a Startup class. It gets made for us. And we don't make a JsonFileProductService. It gets made for us. So remember how you said that we were going to make a service. JsonFileProduct was a service. - [Leslie] So it makes sense to stick it in ConfigureServices. - [Scott] Mm-hmm. Exactly right. So what we want to do is we want to go into ConfigureServices, and we're going to tell it about this new service. You can see that RazorPages that we're going to talk about in a little bit. It's a kind of service. So I can say services dot, ah, look at this. - [Leslie] Oh. I'm liking the stars here. - I like stars. - That looks like some IntelliCode action. - [Scott] Yeah, so that's a great point. IntelliCode is a really smart way to give you auto-complete, and the stars are the ones that are most likely. - [Leslie] Yeah. - [Scott] So it's actually got it right. The one I want is Transient. There's two kind of major ones, Singleton, which is just make it so there's only one of these services, and then Transient, a transient is a thing that comes and goes. So you're going to get a new one of these every time you ask for it. - [Leslie] Yeah. So in this case, the service that we want to connect to is our JsonFileProductServices. - [Scott] 'Kay, JsonFileProductService. You'll notice that it's not auto-completing. - [Leslie] And we made, yeah. And I think part of that is because we named our file something else. - [Scott] In fact, I renamed it, and the issue is that it's in a different namespace. - [Leslie] Yep, that too. - [Scott] All right, So I'm going to hit Using, and I'm going to hit Enter. And you'll see that it actually just added that line right there, but you make a really good point. And it's worth pointing out that if you had named it differently, it wouldn't light up. - Yeah. - Right, like if I did this, see how it's not blue? So you definitely want to know about your spellings and make sure that everything is exactly correct. So what we've done is we've told the system about this. So now the service that we've just made is a member of this collection of services, and it's a transit service. It comes and goes as it likes. - [Leslie] It's like no one's going to know about your restaurant unless you stick it in an ad, right? - Unless you publish it. Right, exactly. And the ServiceCollection, that's a great point. It's an advertisement for all of asp.net that this is a thing. And then you can go and do stuff with that thing. And there's lots of services, and we'll add a bunch. We've got routing and endpoints and all kinds of stuff like that. So at this point, it'll probably compile. It will probably run, right? - Fingers crossed. - [Scott] To show we have such low expectations of ourselves. - Right, maybe. - [Scott] Yeah, it'll probably run, and it probably still won't show anything because I'm silly. (Leslie laughs) Let's find out. Oh. - Oh, still there. - We still have no data, okay. - Awesome. - So you might be saying, "Hey, Scott and Leslie, "you're two videos in, and you haven't shown me a thing yet." - There's no crafts! - What's going on, where are the crafts? We want the crafts. The reason is that we want to do this right. Here's some data. This could come from a file. It could come from a database. It could come from lots of places. We want to have a service that's smart about how to retrieve that data. We want you to understand how that data comes out. But now that we've published it, we've told our asp.net application that this exists, I bet you we're going to be able to get data pretty darn soon. - Oh, man, I'm so excited! - All right. Stick with us for the next video. - Sweet.

Contents