Okay.
In this lesson we're going to create our first class.
We're going to create our own.
Well start creating our own code for our own application.
And the way that we're going to start this is we're going to create a class.
And the type of class we're going to create is often referred to as an entity class, as we're going
to be using this class with a framework called Entity Framework, which we're going to introduce shortly.
So they're often referred to as entities even though they're just typical C-sharp classes.
And when you start a new project or you're sat in front of your computer thinking, right, I'm going
to create this thing, it's quite common to ask, where do I begin?
And the answer often lies in the type of application you're building and what its purpose is.
Well, we're creating an e-commerce store and we're going to be selling products.
The type of product doesn't matter, but it's going to be a ski shop, so it's going to be ski related
products.
And a good starting point for this type of application is going to be to create a class that will hold
properties applicable to a product.
So inside the API folder we'll create a new folder and we will call it entities.
And did I spell that correctly.
Yes I did.
Okay so let's create a new file.
And this is going to be called products.
And when I say create a new file I did that quite quickly.
But inside the solution explorer if we create a new file then we're given a list of different templates
that we can create a C sharp object from.
Now we're interested in creating a class.
So we'll select class.
And then the name of the class is going to be called product.
And when we create the class this way using Solution Explorer and right click the folder and say new
file.
Then we get the template.
And what this gives us is the boilerplate that we need for a typical C sharp class.
Now a couple of things to highlight here.
We've got this using system, but it's kind of grayed out because it's not being used inside this class.
And do we need it.
Well no we can actually remove this.
So we can remove using system.
And we've also got a namespace.
Now this is a logical location.
When we create this it matches the physical name of the folder that we created it in.
But it doesn't have to be that way.
It's a logical namespace.
But what this effectively means is I could create a class called product inside the controllers, let's
say namespace, and it wouldn't conflict with a class with the same name that's located in the entities
or API entities namespace.
So each class we create is going to have a namespace where it logically resides.
And this provides or this prevents us from having conflicts with classes that are in the same name.
Not a big deal in our own small project such as it is, but as we introduce other packages, we'll be
using classes from other packages that we add to our project.
Then there may be a conflict if we don't have this namespace system.
So let's go ahead and create some properties for a product.
And we have snippets that come with the dev kit.
And if I start typing prop and press return.
And this type of property is called an auto implemented property.
And when we create a property in this way, then the C-sharp compiler automatically generates a private
backing field behind the scenes to store the property value.
Another way of creating a property.
If I do the full version, then prop full is the property and backing field.
So we have a private variable and then the name of the property, and we have a getter and setter.
But using the auto implemented property, we get all of that in a single line with a getter and setter.
Now we need this to be public because this is going to be accessed by Entity Framework, and Entity
Framework will need access to get and set properties.
And the first property that we'll create is going to be an ID property.
Now when it comes to Entity Framework that we're going to introduce soon, then we're going to talk
about conventions.
And one convention for Entity Framework is if we call a property ID, then it's going to use that property
as the primary key for our database table when it creates it.
We're also going to have a property for the name of the product.
So that's going to be a string and we'll call it name.
I'm going to ignore warnings just while I populate the different properties that we need and come back
to them.
We're going to have another one called description for the description of the product.
And we'll also have a price.
Now, it might be tempting to use the decimal here, but for a very specific reason.
And the very specific reason is the payment provider that we're going to be using later on.
They use long for the a currency value and we'll just stick to using the same, which saves us converting
it later.
And it just means that we'll need to divide everything by 100 in order to get the decimal value.
And we'll also have another property for the string of the picture you URL of the product.
Another reason for using long, by the way, is we don't need to worry about accuracy.
When we use that as a decimal, we'll need to define the number of decimal places that we'll allow for
accuracy, where as long we don't need to worry about that either.
So we've got the picture URL.
We'll have another few properties.
We're going to have a string for the type of the product, and we'll have a string for the brand of
the product.
And one more property.
We'll just have an int for the quantity in stock.
So that will be enough to get us started.
But we've got some warnings.
And these warnings are related to the nullable reference types that we mentioned briefly earlier on.
Now it's telling us that because we've got this nullable flag enabled, then we need to ensure we either
make this property name optional, as in it's not going to be required.
So our compiler knows that if we try and use the name somewhere or we don't check for its existence,
then it's going to stop us from doing so because we'll run into a problem if we try and access something
or use something that is null.
But we don't want that.
We want our products to have names, so we don't want to make this optional.
Another approach to this is to initialize the value.
We could initialize the name to an empty string, but then that would also not be great because now
we could have properties with empty strings in our database, or products with empty string names in
our database.
We don't want that either.
But since C-sharp seven we have a required modifier.
And what this required modifier means that when we create a new product, a new instance of a product,
then we are required to supply it when we initialize that product with a name property.
And that's the approach we will take here.
So we're going to give all of these string values the required modifier.
And we'll do the same for the picked URL.
And the type accidentally clicked on references there and for the brand as well.
Now we don't need to do that for the quantity in stock or the price.
And the reason for that is if I create a new product and I do not initialize the price or the quantity,
then that's going to give it the default value for that type.
And in the case of long that's going to be zero the int it would be zero.
So it's always going to have a value even if it's the default value.
But what it will not be is null because it's not a reference type.
It's a value type.
So that's our entity created.
And how we use entities is we're going to take a look at Entity Framework.
And we're going to introduce that next.