WEBVTT

00:00.020 --> 00:04.700
So what we're going to take a look at in this lesson is adding Automapper into our application.

00:04.730 --> 00:09.890
Now if you look out on the big bad world of the internet and you search for developer opinions of this

00:09.890 --> 00:12.800
particular product, then you're going to get a mixed bag of reviews.

00:12.800 --> 00:15.230
Some developers hate it, some developers love it.

00:15.230 --> 00:16.400
I'm kind of in the middle.

00:16.400 --> 00:17.690
I find it quite useful.

00:17.690 --> 00:21.830
But like anything that we add to our application, it's just a matter of understanding what it does,

00:21.830 --> 00:26.810
how it does it, and what impact can it have on things like performance?

00:26.840 --> 00:29.060
I think that's why it gets a bad reputation.

00:29.060 --> 00:32.930
Some developers believe it's going to affect our performance.

00:32.930 --> 00:39.200
Between mapping one thing to another thing, it's going to be imperceptible to our application, and

00:39.200 --> 00:41.060
I just want to present this as an option.

00:41.060 --> 00:44.780
We've seen how to map things manually, and that works just fine.

00:44.780 --> 00:47.450
Not the most exciting code to write, but it does work.

00:47.450 --> 00:51.830
And now I'm going to demonstrate how to do it using a tool like Automapper.

00:52.880 --> 00:56.390
So the first thing we need to do is add that into our project.

00:56.390 --> 00:59.990
So I'm going to open up the terminal and go to NuGet.

01:00.120 --> 01:03.930
And we're going to search for Automapper.

01:04.410 --> 01:10.350
And inside here we're just going to get the latest version of this currently on version 13.

01:10.350 --> 01:14.280
And I'm going to add this to the API Csproj.

01:14.850 --> 01:16.350
And that's now available.

01:16.350 --> 01:18.300
And then we can go and configure it.

01:18.300 --> 01:20.250
So we use this as a service.

01:20.250 --> 01:23.010
So we're going to go to our program dot CS.

01:23.010 --> 01:25.740
And inside here let's find a good place to add this.

01:25.740 --> 01:30.750
I'm just going to add it below the add calls and specify builder dot services.

01:30.750 --> 01:35.400
And we should have an extension method inside here to add Automapper.

01:35.880 --> 01:41.130
And we just need to tell it where to find our Automapper configuration code.

01:41.160 --> 01:45.090
Now we've only got a single project which means a single assembly.

01:45.090 --> 01:48.810
So we can just use something like what I'm going to do here.

01:48.810 --> 01:56.220
We can use app domain, current domain, and we can specify get assemblies to go and find Automapper

01:56.250 --> 02:01.580
profiles, which is what we're going to create to tell Automapper how we want to map one thing to another

02:01.580 --> 02:09.800
thing, and then we can create a class which is going to tell Automapper how to handle mapping from,

02:09.800 --> 02:13.280
for example, a create product DTO into a product.

02:13.310 --> 02:18.590
So let's go to Solution Explorer and we'll go to our request helpers.

02:18.590 --> 02:21.620
We'll create a new file that's going to be a class.

02:21.620 --> 02:26.210
And we'll just call it Mapping Profiles and press return.

02:26.240 --> 02:32.420
And inside here we need to derive from a class called profile which we get from Automapper.

02:32.660 --> 02:34.490
So we'll bring that in.

02:34.520 --> 02:39.620
And then inside here we create a constructor that takes no parameters.

02:39.650 --> 02:42.290
And then we create mapping profiles.

02:42.290 --> 02:45.530
So we use create map which we get from Automapper.

02:45.830 --> 02:48.080
And we tell it where we want to map from.

02:48.080 --> 02:53.180
So we're going to specify our create product DTA and where we want to map into.

02:53.210 --> 02:59.030
So that's going to be our product which we get from API entities.

02:59.550 --> 03:00.870
and that's it.

03:00.900 --> 03:03.300
Now we've got a mapping effectively.

03:03.300 --> 03:12.300
And the idea of this, if I go to the product and compare it with the create product DTA and let's get

03:12.300 --> 03:16.830
these into a view side by side with one another.

03:16.860 --> 03:22.920
So on the create product DTO, I've got properties in here, I've got a name property, I've got a description

03:22.920 --> 03:24.810
property a price property.

03:24.810 --> 03:29.250
And these are identical to the names of the properties on the other side.

03:29.580 --> 03:35.610
And the idea being that automapper if the name matches and the type matches, then Automapper is going

03:35.610 --> 03:40.260
to handle this automatically for us to map one thing to another thing.

03:40.290 --> 03:43.800
So it takes away the tedium of writing that kind of code.

03:43.800 --> 03:49.170
So how we use this then is we'll go back to our products controller, and we've got another service

03:49.170 --> 03:53.430
that we can use inside here called Imap4, which we get from Automapper.

03:53.460 --> 04:02.860
Of course we can use Imap4 and specify mapper and then we can use the mapper in our method.

04:03.370 --> 04:10.990
So when we've got var product equals instead of specifying new product we can use mapper dot map.

04:11.320 --> 04:14.380
And we specify what we want to map into.

04:14.410 --> 04:17.200
So this is going to be mapping into a product.

04:18.400 --> 04:24.310
And we're going to map from our product DTO.

04:24.760 --> 04:29.500
And now if we hover over the product then we can see that this is a type of product.

04:29.560 --> 04:35.620
We're not getting any warnings in our code because Automapper is going to take care of mapping each

04:35.620 --> 04:41.530
property inside our product DTO into our products, and then give us an instance of our product, which

04:41.530 --> 04:44.890
we can then add into our database.

04:44.920 --> 04:46.330
So let's give this a go.

04:46.360 --> 04:48.280
Then we'll probably need to restart.

04:48.310 --> 04:52.900
In fact, we'll definitely need to restart our API after that kind of activity.

04:52.900 --> 04:54.400
And don't believe this.

04:54.430 --> 04:58.090
Where it says no hot reload changes to apply we've added Automapper.

04:58.160 --> 04:59.690
We've updated our controller.

04:59.720 --> 05:03.380
There's not a chance that Hot Reload is going to work for this.

05:03.380 --> 05:11.810
So I'm going to stop and restart my API server and wait for this to finish loading.

05:11.810 --> 05:17.390
And once it has, I'll head back to postman and we've got our login for admin.

05:17.390 --> 05:24.350
So let's just send that and we can see that we've got the admin in the member and admin roles.

05:24.350 --> 05:30.680
But first of all, before we get to creating a product, let's just log in as Bob and get Bob's login.

05:30.680 --> 05:36.560
Because Bob, if we check his user info, he's only a member of the member role.

05:36.560 --> 05:41.030
And if we try and create a product and let's double check the body of this.

05:41.030 --> 05:46.520
What we've got here is just using postman variables to give us random product names, a random product

05:46.520 --> 05:56.000
description, a random product price, a picture URL that goes to https photos 500.

05:56.030 --> 06:02.290
Let's just check to see if this does give me a valid picture, because I'm not 100% sure that URL is

06:02.290 --> 06:07.840
correct, so I'll just paste this into a separate tab in postman and it does give us a random picture.

06:07.870 --> 06:11.560
Okay, I had a feeling that wasn't going to work, but that does.

06:11.560 --> 06:15.280
So we'll just use that as a random picture for our image.

06:15.280 --> 06:19.210
We've got a type and a brand and we'll just specify a quantity in stock.

06:19.240 --> 06:20.770
Now I'm logged in as Bob.

06:20.770 --> 06:28.600
What I would expect to get back from our API for this is a 403 unauthorized.

06:28.600 --> 06:31.840
So I'm going to click or 403 forbidden I should say.

06:31.840 --> 06:38.200
And I'm going to click send and we get the correct and desired response from this.

06:39.130 --> 06:43.510
So let's log in as admin again and I'll just go back to that request I'll click send.

06:43.540 --> 06:45.370
Now I've got the cookie for the admin.

06:45.370 --> 06:50.440
And if we click Create Product again this time we should not get a 403 forbidden.

06:50.440 --> 06:52.240
We should get a 201 created.

06:52.240 --> 07:01.400
And we can see that we have our products, and you might be thinking that ID looks a bit weird.

07:01.430 --> 07:02.210
Why isnt it?

07:02.240 --> 07:04.610
ID number 19.

07:04.610 --> 07:06.200
Because weve got 18 products.

07:06.200 --> 07:08.870
Why is it giving it an ID of 1002?

07:09.140 --> 07:10.640
This is a SQL server thing.

07:10.640 --> 07:15.680
It is configurable, but effectively SQL server reserves blocks of numbers.

07:15.680 --> 07:20.150
So we'll probably find as we add new products we'll go up to 1003 1004.

07:20.150 --> 07:25.310
But if we leave it for some time then it will go up to 2001 or 2002.

07:25.340 --> 07:26.660
I'm not going to worry about that.

07:26.660 --> 07:32.540
We don't really care about our product IDs as long as they're unique and SQL server is happy, I'm happy,

07:32.570 --> 07:36.650
and I'm not going to mess around with database configuration in this course.

07:36.650 --> 07:44.630
But what we do have now is the ability to create a product using our roles as well in our database.

07:44.630 --> 07:45.440
So great.

07:45.440 --> 07:46.010
That's it.

07:46.010 --> 07:48.950
Create parts of Crud operations taken care of.

07:48.980 --> 07:51.440
We've covered the read operations earlier on.

07:51.440 --> 07:56.540
So next we'll move on to the update operation and take a look at how we can edit a product.
