- Add interfaces
- Use linq queries
Interfaces are critical to modern programming. There are multiple concepts and programming patterns that rely on interfaces. While we aren't going to explore any of those in this course, we are going to learn how to use interfaces.
- We have an existing class that would be great to have an interface for:
ProductLogic
. - Create a new Interface and call it
IProductLogic
. You do it the same way you do a class, you just select Interface isntead of Class. If you create a class by accident, you can just change the keywordclass
tointerface
. - In this interface, we're going to be saying what functions need to be implemented, not how they are.
- So look at the
ProductLogic
class and add the already existing methods to the interface. Here's the first one:public void AddProduct(Product product);
- Once all the existing methods have been added to the interface, head over to the
ProductLogic
class again and let's have the class implement the interface. You do this using the same syntax you use to have a class inherit from another:
. - Once we have
ProductLogic implementing
IProductLogic`, let's add a new method to our interface. - Add a method called
GetOnlyInStockProducts
. It doesn't need to take any arguments and it will returnList<Product>
. - Heading back to
ProductLogic
you should see a build error. This is because ourProductLogic
class isn't fully implementing the interface. We'll be doing that in the next section.
- In the
ProductLogic
class, in the constructor where you are creating a new list of products, instead of having that list be empty, go ahead and add 2 or 3 products to it. One of those products should have a quantity of 0. - Add the method from the interface that hasn't yet been implemented.
- In that method body, we are going to filter our product list to only return products with quantity greater than 0.
- To normally do this, we would probably use a
for
loop to iterate through the whole list checking each one to see if its quantity is greater than 0. If it is, we would add it to a new list then return that list. We're going to use linq for this though which will greatly simplify our method. - In the method body, type of this:
return _products.Where(x => x.Quantity > 0).ToList();
- Let's take this a step further. Let's say we would only want to return the names of the products. We would do this by using the
Select
method. This one would sit betweenWhere
andToList
. Go ahead and add.Select(x=>x.Name)
. You will have build errors from this, so go ahead and fix that. You'll need to make changes inProductLogic
andIProductLogic
. - Finally, add a UI option to view in stock products only.