>> 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're starting to bring in data.
We haven't actually displayed it
yet because we want to do this right.
We've made a little JSON file
and that's our database for this purpose.
Then, what we're going to do is we're going to add a service,
and ASP.NET has this concept of services.
We're going to try to do that in a very deliberate way as well.
So you're clear on how we're doing.
>> So why even make a service to begin with?
Why can't I just take this code wherever I wan?
>> That's a great question.
So we tried to do this in such a way that
it is going to be maintainable for the future.
>> Makes sense.
>> We could theoretically just make like one or two lines of
code that open this thing up and just split 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 like if you're at
a restaurant and the waiter asks you
what you want and you can request.
>> That's a really great point.
Because a service in the context of the computer
is I need something and I need you to get it done.
I don't even know how you doing it, the waiter.
>> I don't want to do the work.
>> Don't talk to me, just get me that thing that I need.
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.
So where do we want to do?
I remember that in the last video you had
me make a models folder and inside that is product.
>> 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?
>> So we're going to go and say Add and
I'm going to say New Folder,
and you want to call it services.
We'll put all of our future services in there.
Then right now, we're storing our data in this JSON file.
So this is a JSON file service thing.
>> Yeah. We've got to have a spot to
actually extract that data to begin with.
So let's create a file and call it the JsonFileProductsService.
>> JsonFileProductsService. That makes sense.
It's very descriptive and it told me exactly what it does.
So 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 jist to 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.
> I was looking up at guest earlier and they
were using product service, not products.
>> 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 when you spell something slightly differently.
Good call, we'll watch for that.
So I'm going to go and change that,
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 the problem.
>> 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 like Notepad but it's a smart text editor.
>> So that means it can probably identify
what the functions are that we just made.
>> It can. What's really great point
if we go over here look at this,
we can actually zoom in and we try zooming in,
and in the solution editor,
we get like our properties
the ones that are private a little lock,
the message that's cool,
but also the Smart Text Editor will allow me to collapse.
>> What do you know?
>> It's called a collapsing text editor.
>> So much nicer.
>> It is. It's also less,
for lack of a better word, threatening
because it's like yeah , let's get it.
>> 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?
>> Oh, a small world.
>> 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 the host.
>> That's cool. Is telling something else what to do as well.
>> 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 getting it passed in and we're keeping it.
>> 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 WebHostEnvironment
know the path where this file comes from.
>> So that's a great point. In this folder right here,
we have wwwroot, data, products.
I don't want to hard code that C:\\.
>> We're going to mess it up anyway.
>> You know I'll mess it up. So the WebHostEnvironment
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, give me one of these."
What's cool about this is I never have to make one.
There's nowhere in this code where I knew it up.
It's just handed to me.
Then, where is the file located?
Look at this, this is interesting.
We made a property called JsonFileName.
Again, either one of us could have just hard coded C:\|.
But what we did is we said, "Hey,
WebHostEnvironment, where is the web root?"
Your disk and my disk and their disk is different.
They might be on the D drive,
they may be in SQL and Leslie who knows.
>> If you move it somewhere else?
>> You don't want them to break. So this is cool.
We're going to use an 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 passes as you want and they're taking
care of the slashes and the backslashes and this and that.
>> The perk about putting that Products.JsonFile in
the wwwroot folder that we
have is because that web root path
will just automatically go to level.
>> It will be absolutely reliable.
It gets the path to the directory that has webserver application.
Now, we could have put that in a number of different folders and
WebHostEnvironment also has other ones like content route.
So there's multiple places we could have 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.
So the one we're using is web root path.
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.
>> Sharing is caring.
>> Indeed. Here's the part that gets a little intense.
This took me a second to get my head around this.
>> So what's happening here?
We are retrieving the products.
How are we doing that?
>> So I innumerable is not listed because I've been doing this.
>> 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.
>> So let's look at that really briefly, remind ourselves.
Here's what the JSON looks like.
Here's what the product is shaped
like 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 filename we made before.
>> Yeah.
>> As you said,
we're going to de-serialize and this is interesting,
we're making an array of products,
not one product but products.
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, we want it all.
>> Our product circle here.
>> Here, we can pass in
some optional options and these are
not required, they are optional.
We're saying don't really care
about uppercase lowercase, just make it work.
>> Yeah.
>> Because I can't deal with the stress.
>> I agree.
>> Then, this is interesting and IEnumerable, I was using lists.
>> Me too I typically use lists.
>> List of this, list of that.
IEnumerable is the great great grandparent of lists.
It's stuff that you can four
each over, that's how I think about it.
So if take a look at the video that we did with Kendra on C#,
we talked about 'for' loops and 'for eaches',
things that you can 'for' each over
are things that you can enumerate.
Therefore, they are enumerable things.
>> That's nice because I'm not limited
to just having to use lists.
>> Yeah.
>> I can use whatever IEnumerable that 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 whose 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 publicize it?
We have a waiter now, tell
everyone in the restaurant there's a waiter.
>> 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.
>> We need to make sure that ASP.NET
knows that this is an available service.
So I'm going to go over to startup.
In startup, this is a special ASP.NET page.
This is especially as you get on that bit of code rather.
>> So in the past, this is usually one of
those files that I just ignore or glance over.
I think it's not important because there's a bunch of
complicated things on there already.
>> To be honest, it does look like
a bunch of boilerplate code, it is.
With the program part of the program CS,
this is usually like console.writeline.
For us it's CreateHost.
Here, we say "Hey, use Startup."
That's our startup class.
It can be called Fred or Foo or whatever, we call 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.
This is really interesting because we never
actually again make a start-p 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 JsonFileProduct as a service?
>> So it makes sense to stick it in configure surfaces.
>> 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 razor pages
that we're going to talk about in a little bit.
It's a kind of service. So I can say services. Now look at this.
>> I'm liking the stars here in telecode action.
>> Yeah. So that's a great point.
In telecode, is a really smart way to give you
auto-complete and the stars are the ones that are most likely.
>> Yeah.
>> So it's actually got it right. The one I want is transient.
There's two major ones,
singleton which is,
just make it so there's only one of these surfaces.
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.
>> Yeah. So in this case,
the service that we want to connect to
you is our JsonFileProductServices.
>> JsonFileProductService.
Notice that it's not autocompleting.
>> We made up. I think part of that is
because we named our file something else.
>> In fact, I renamed it and
the issue is that it's in a different namespace.
>> Yep, that's it.
>> 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.
It's worth pointing out that if you had named it
differently, it wouldn't allow it up.
>> Yeah.
>> Like if I did this, see 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 just made Is
a member of this collection of
services and it's a Transient service,
it comes and goes as it likes.
>> No one's going to know about your restaurant.
Must you stick it and out?
>> I'm actually publish it.
Exactly and this service collection it'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 your stuff with that thing.
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.
>> Across.
>> So we have such low expectations ourselves.
It'll probably run and it probably still
won't show anything because I'm silly.
Let's find out. They'll have no data. Okay.
>> Awesome.
>> So you might be saying, "Hey,
Scott Leslie you have two videos in and you haven't it show in."
>> It's crafts.
>>'' What's going on? Where are the crafts?
We want the crafts.''
The reason is that we want to do this right.
Here is 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 we're going to be able to get data pretty done soon.
>> I'm so excited.
>> Stick with us for the next video.