So what we're going to take a look at in this lesson is adding Automapper into our application.
Now if you look out on the big bad world of the internet and you search for developer opinions of this
particular product, then you're going to get a mixed bag of reviews.
Some developers hate it, some developers love it.
I'm kind of in the middle.
I find it quite useful.
But like anything that we add to our application, it's just a matter of understanding what it does,
how it does it, and what impact can it have on things like performance?
I think that's why it gets a bad reputation.
Some developers believe it's going to affect our performance.
Between mapping one thing to another thing, it's going to be imperceptible to our application, and
I just want to present this as an option.
We've seen how to map things manually, and that works just fine.
Not the most exciting code to write, but it does work.
And now I'm going to demonstrate how to do it using a tool like Automapper.
So the first thing we need to do is add that into our project.
So I'm going to open up the terminal and go to NuGet.
And we're going to search for Automapper.
And inside here we're just going to get the latest version of this currently on version 13.
And I'm going to add this to the API Csproj.
And that's now available.
And then we can go and configure it.
So we use this as a service.
So we're going to go to our program dot CS.
And inside here let's find a good place to add this.
I'm just going to add it below the add calls and specify builder dot services.
And we should have an extension method inside here to add Automapper.
And we just need to tell it where to find our Automapper configuration code.
Now we've only got a single project which means a single assembly.
So we can just use something like what I'm going to do here.
We can use app domain, current domain, and we can specify get assemblies to go and find Automapper
profiles, which is what we're going to create to tell Automapper how we want to map one thing to another
thing, and then we can create a class which is going to tell Automapper how to handle mapping from,
for example, a create product DTO into a product.
So let's go to Solution Explorer and we'll go to our request helpers.
We'll create a new file that's going to be a class.
And we'll just call it Mapping Profiles and press return.
And inside here we need to derive from a class called profile which we get from Automapper.
So we'll bring that in.
And then inside here we create a constructor that takes no parameters.
And then we create mapping profiles.
So we use create map which we get from Automapper.
And we tell it where we want to map from.
So we're going to specify our create product DTA and where we want to map into.
So that's going to be our product which we get from API entities.
and that's it.
Now we've got a mapping effectively.
And the idea of this, if I go to the product and compare it with the create product DTA and let's get
these into a view side by side with one another.
So on the create product DTO, I've got properties in here, I've got a name property, I've got a description
property a price property.
And these are identical to the names of the properties on the other side.
And the idea being that automapper if the name matches and the type matches, then Automapper is going
to handle this automatically for us to map one thing to another thing.
So it takes away the tedium of writing that kind of code.
So how we use this then is we'll go back to our products controller, and we've got another service
that we can use inside here called Imap4, which we get from Automapper.
Of course we can use Imap4 and specify mapper and then we can use the mapper in our method.
So when we've got var product equals instead of specifying new product we can use mapper dot map.
And we specify what we want to map into.
So this is going to be mapping into a product.
And we're going to map from our product DTO.
And now if we hover over the product then we can see that this is a type of product.
We're not getting any warnings in our code because Automapper is going to take care of mapping each
property inside our product DTO into our products, and then give us an instance of our product, which
we can then add into our database.
So let's give this a go.
Then we'll probably need to restart.
In fact, we'll definitely need to restart our API after that kind of activity.
And don't believe this.
Where it says no hot reload changes to apply we've added Automapper.
We've updated our controller.
There's not a chance that Hot Reload is going to work for this.
So I'm going to stop and restart my API server and wait for this to finish loading.
And once it has, I'll head back to postman and we've got our login for admin.
So let's just send that and we can see that we've got the admin in the member and admin roles.
But first of all, before we get to creating a product, let's just log in as Bob and get Bob's login.
Because Bob, if we check his user info, he's only a member of the member role.
And if we try and create a product and let's double check the body of this.
What we've got here is just using postman variables to give us random product names, a random product
description, a random product price, a picture URL that goes to https photos 500.
Let's just check to see if this does give me a valid picture, because I'm not 100% sure that URL is
correct, so I'll just paste this into a separate tab in postman and it does give us a random picture.
Okay, I had a feeling that wasn't going to work, but that does.
So we'll just use that as a random picture for our image.
We've got a type and a brand and we'll just specify a quantity in stock.
Now I'm logged in as Bob.
What I would expect to get back from our API for this is a 403 unauthorized.
So I'm going to click or 403 forbidden I should say.
And I'm going to click send and we get the correct and desired response from this.
So let's log in as admin again and I'll just go back to that request I'll click send.
Now I've got the cookie for the admin.
And if we click Create Product again this time we should not get a 403 forbidden.
We should get a 201 created.
And we can see that we have our products, and you might be thinking that ID looks a bit weird.
Why isnt it?
ID number 19.
Because weve got 18 products.
Why is it giving it an ID of 1002?
This is a SQL server thing.
It is configurable, but effectively SQL server reserves blocks of numbers.
So we'll probably find as we add new products we'll go up to 1003 1004.
But if we leave it for some time then it will go up to 2001 or 2002.
I'm not going to worry about that.
We don't really care about our product IDs as long as they're unique and SQL server is happy, I'm happy,
and I'm not going to mess around with database configuration in this course.
But what we do have now is the ability to create a product using our roles as well in our database.
So great.
That's it.
Create parts of Crud operations taken care of.
We've covered the read operations earlier on.
So next we'll move on to the update operation and take a look at how we can edit a product.