WEBVTT

00:00.020 --> 00:00.320
Okay.

00:00.320 --> 00:03.500
In this lesson we're going to create our first class.

00:03.590 --> 00:04.820
We're going to create our own.

00:04.850 --> 00:08.090
Well start creating our own code for our own application.

00:08.090 --> 00:12.800
And the way that we're going to start this is we're going to create a class.

00:12.860 --> 00:18.530
And the type of class we're going to create is often referred to as an entity class, as we're going

00:18.560 --> 00:26.660
to be using this class with a framework called Entity Framework, which we're going to introduce shortly.

00:26.660 --> 00:32.990
So they're often referred to as entities even though they're just typical C-sharp classes.

00:33.020 --> 00:38.150
And when you start a new project or you're sat in front of your computer thinking, right, I'm going

00:38.180 --> 00:42.530
to create this thing, it's quite common to ask, where do I begin?

00:42.530 --> 00:47.870
And the answer often lies in the type of application you're building and what its purpose is.

00:47.900 --> 00:52.580
Well, we're creating an e-commerce store and we're going to be selling products.

00:52.580 --> 00:57.650
The type of product doesn't matter, but it's going to be a ski shop, so it's going to be ski related

00:57.650 --> 00:58.550
products.

00:58.550 --> 01:06.450
And a good starting point for this type of application is going to be to create a class that will hold

01:06.450 --> 01:08.880
properties applicable to a product.

01:08.880 --> 01:16.290
So inside the API folder we'll create a new folder and we will call it entities.

01:16.920 --> 01:19.770
And did I spell that correctly.

01:19.800 --> 01:20.850
Yes I did.

01:20.880 --> 01:22.860
Okay so let's create a new file.

01:22.860 --> 01:25.380
And this is going to be called products.

01:25.380 --> 01:28.440
And when I say create a new file I did that quite quickly.

01:28.440 --> 01:34.590
But inside the solution explorer if we create a new file then we're given a list of different templates

01:34.590 --> 01:37.590
that we can create a C sharp object from.

01:37.590 --> 01:40.080
Now we're interested in creating a class.

01:40.080 --> 01:41.610
So we'll select class.

01:41.610 --> 01:45.600
And then the name of the class is going to be called product.

01:46.170 --> 01:53.610
And when we create the class this way using Solution Explorer and right click the folder and say new

01:53.610 --> 01:54.270
file.

01:54.300 --> 01:55.800
Then we get the template.

01:55.800 --> 02:01.350
And what this gives us is the boilerplate that we need for a typical C sharp class.

02:01.350 --> 02:03.200
Now a couple of things to highlight here.

02:03.200 --> 02:09.650
We've got this using system, but it's kind of grayed out because it's not being used inside this class.

02:10.190 --> 02:11.240
And do we need it.

02:11.270 --> 02:13.850
Well no we can actually remove this.

02:13.850 --> 02:16.160
So we can remove using system.

02:16.160 --> 02:18.260
And we've also got a namespace.

02:18.260 --> 02:21.050
Now this is a logical location.

02:21.620 --> 02:26.870
When we create this it matches the physical name of the folder that we created it in.

02:26.870 --> 02:28.490
But it doesn't have to be that way.

02:28.490 --> 02:30.230
It's a logical namespace.

02:30.650 --> 02:36.710
But what this effectively means is I could create a class called product inside the controllers, let's

02:36.710 --> 02:45.800
say namespace, and it wouldn't conflict with a class with the same name that's located in the entities

02:46.100 --> 02:48.920
or API entities namespace.

02:49.220 --> 02:53.360
So each class we create is going to have a namespace where it logically resides.

02:53.360 --> 02:59.000
And this provides or this prevents us from having conflicts with classes that are in the same name.

02:59.000 --> 03:05.000
Not a big deal in our own small project such as it is, but as we introduce other packages, we'll be

03:05.000 --> 03:08.270
using classes from other packages that we add to our project.

03:08.720 --> 03:12.110
Then there may be a conflict if we don't have this namespace system.

03:12.110 --> 03:16.910
So let's go ahead and create some properties for a product.

03:16.910 --> 03:20.690
And we have snippets that come with the dev kit.

03:20.690 --> 03:24.590
And if I start typing prop and press return.

03:24.680 --> 03:30.320
And this type of property is called an auto implemented property.

03:30.320 --> 03:36.590
And when we create a property in this way, then the C-sharp compiler automatically generates a private

03:36.590 --> 03:40.250
backing field behind the scenes to store the property value.

03:40.250 --> 03:42.410
Another way of creating a property.

03:42.410 --> 03:47.810
If I do the full version, then prop full is the property and backing field.

03:47.810 --> 03:53.750
So we have a private variable and then the name of the property, and we have a getter and setter.

03:53.750 --> 04:01.310
But using the auto implemented property, we get all of that in a single line with a getter and setter.

04:01.340 --> 04:06.560
Now we need this to be public because this is going to be accessed by Entity Framework, and Entity

04:06.590 --> 04:10.430
Framework will need access to get and set properties.

04:10.490 --> 04:14.840
And the first property that we'll create is going to be an ID property.

04:15.050 --> 04:19.460
Now when it comes to Entity Framework that we're going to introduce soon, then we're going to talk

04:19.460 --> 04:20.900
about conventions.

04:20.930 --> 04:28.790
And one convention for Entity Framework is if we call a property ID, then it's going to use that property

04:28.790 --> 04:33.110
as the primary key for our database table when it creates it.

04:33.410 --> 04:35.780
We're also going to have a property for the name of the product.

04:35.780 --> 04:38.750
So that's going to be a string and we'll call it name.

04:38.750 --> 04:42.980
I'm going to ignore warnings just while I populate the different properties that we need and come back

04:43.010 --> 04:43.550
to them.

04:43.610 --> 04:49.070
We're going to have another one called description for the description of the product.

04:49.070 --> 04:52.070
And we'll also have a price.

04:52.100 --> 04:56.750
Now, it might be tempting to use the decimal here, but for a very specific reason.

04:56.990 --> 05:01.880
And the very specific reason is the payment provider that we're going to be using later on.

05:01.880 --> 05:09.200
They use long for the a currency value and we'll just stick to using the same, which saves us converting

05:09.200 --> 05:10.040
it later.

05:10.040 --> 05:16.310
And it just means that we'll need to divide everything by 100 in order to get the decimal value.

05:16.310 --> 05:23.630
And we'll also have another property for the string of the picture you URL of the product.

05:23.630 --> 05:27.890
Another reason for using long, by the way, is we don't need to worry about accuracy.

05:28.370 --> 05:36.440
When we use that as a decimal, we'll need to define the number of decimal places that we'll allow for

05:36.440 --> 05:40.490
accuracy, where as long we don't need to worry about that either.

05:40.490 --> 05:41.900
So we've got the picture URL.

05:41.900 --> 05:43.190
We'll have another few properties.

05:43.190 --> 05:48.950
We're going to have a string for the type of the product, and we'll have a string for the brand of

05:48.950 --> 05:49.700
the product.

05:49.700 --> 05:51.260
And one more property.

05:51.290 --> 05:56.930
We'll just have an int for the quantity in stock.

05:56.930 --> 05:59.060
So that will be enough to get us started.

05:59.060 --> 06:00.140
But we've got some warnings.

06:00.140 --> 06:05.960
And these warnings are related to the nullable reference types that we mentioned briefly earlier on.

06:05.990 --> 06:12.440
Now it's telling us that because we've got this nullable flag enabled, then we need to ensure we either

06:12.440 --> 06:16.940
make this property name optional, as in it's not going to be required.

06:16.940 --> 06:25.040
So our compiler knows that if we try and use the name somewhere or we don't check for its existence,

06:25.040 --> 06:30.200
then it's going to stop us from doing so because we'll run into a problem if we try and access something

06:30.200 --> 06:32.090
or use something that is null.

06:32.120 --> 06:33.080
But we don't want that.

06:33.080 --> 06:36.740
We want our products to have names, so we don't want to make this optional.

06:37.040 --> 06:39.230
Another approach to this is to initialize the value.

06:39.260 --> 06:45.500
We could initialize the name to an empty string, but then that would also not be great because now

06:45.500 --> 06:50.090
we could have properties with empty strings in our database, or products with empty string names in

06:50.090 --> 06:50.690
our database.

06:50.690 --> 06:52.100
We don't want that either.

06:52.550 --> 06:56.120
But since C-sharp seven we have a required modifier.

06:56.120 --> 07:02.450
And what this required modifier means that when we create a new product, a new instance of a product,

07:02.930 --> 07:08.630
then we are required to supply it when we initialize that product with a name property.

07:08.660 --> 07:11.420
And that's the approach we will take here.

07:11.420 --> 07:15.710
So we're going to give all of these string values the required modifier.

07:16.220 --> 07:18.740
And we'll do the same for the picked URL.

07:18.890 --> 07:27.830
And the type accidentally clicked on references there and for the brand as well.

07:27.860 --> 07:31.940
Now we don't need to do that for the quantity in stock or the price.

07:31.940 --> 07:39.140
And the reason for that is if I create a new product and I do not initialize the price or the quantity,

07:39.530 --> 07:42.680
then that's going to give it the default value for that type.

07:42.800 --> 07:47.330
And in the case of long that's going to be zero the int it would be zero.

07:47.360 --> 07:50.930
So it's always going to have a value even if it's the default value.

07:50.930 --> 07:55.190
But what it will not be is null because it's not a reference type.

07:55.190 --> 07:56.780
It's a value type.

07:57.320 --> 07:59.330
So that's our entity created.

07:59.330 --> 08:03.620
And how we use entities is we're going to take a look at Entity Framework.

08:03.620 --> 08:06.920
And we're going to introduce that next.
