Interfaces vs. Classes (Angular Question)

preview_player
Показать описание
In this video, I cover if you should use interfaces or classes in your Angular apps for data representation.
Рекомендации по теме
Комментарии
Автор

Imagine you want to make a cake, but you want to make sure it has certain ingredients and decorations.
An interface is like a recipe that tells you exactly what ingredients (like sugar, flour, eggs, etc.) and decorations (like frosting and sprinkles) your cake needs to have. The recipe doesn't tell you how to make the cake, but it makes sure that the cake has the right ingredients and decorations.
A class, on the other hand, is like a chef who knows how to make a cake. The chef knows all the steps to make the cake and has all the right ingredients and decorations, but the chef can make the cake in different ways or add extra things to make it special.
So if i get this right, you would use an interface when you just want to make sure that your cake has the right ingredients and decorations. You would use a class when you want to have a specific way of making the cake, and also make sure it has the right ingredients and decorations.

sidokouki
Автор

Code font is too small. Please enlarge in future videos.

DedicatedManagers
Автор

I like the idea of using DTO to mark it as a Data Transfer Object is that common practice? Also sometimes you can generate objects from swagger, openAPI 3 etc which allows for one source of truth for front-end/backend code bases. One thing I find difficult is trying to decide if objects on the front-end should be similar to objects on the backend. Often the database deals with extra fields e.g. UpdatedAt, UpdatedBy, DeletedAt etc, and the front-end deals with extra state e.g. item.isSelected etc

RobertKing