So let's just talk about the last part of generic interfaces.
In this lesson you will learn how to combine generic classes and generic interfaces.
So I've just modified the code from the last lecture.
Let's just go through it real quick.
We have an internal class program here as usual.
Then we have our internal interface I repository.
It's generic with type T, and in remove we have no constraint or whatever simple interface.
Then we have our product class which has an ID in the name done.
And then we have a class called Product Repository that implements our generic repository with a type
set to product.
Now let's just assume that our guide, our design guidelines, say that we have to make sure that our
repository is generic too, because it doesn't matter if we have a product, if we have a user or whatever,
the repository always behaves the same.
It takes and loads data and saves it no matter what entity type we're talking about.
Right?
So no matter if user or product, it always behaves the same in that way.
Having it like this way would be bad practice, because we would have to duplicate the class and duplicate
all of the code inside.
So it would be better practice to say that this class is called repository and make it a generic one.
So say type T in that case.
Now it doesn't matter what we submit here, we will add and remove whatever type T is.
So let's switch every product that we have to type T and finally go to our repository and also say that
this is now using the generic type T of our repository here.
Right.
So now we have made use of a generic interface right.
Our AI repository which says no matter what data type you submit, just make sure that it has an add
void and a remove void.
So two methods here.
And our class itself is also generic and says great.
Whatever you submit here, whatever type, I will handle it exactly the same way with that kind of methods
for sure.
You could say like uh, if for example, entity and then you say get type, you can check the type and
you can say, hey, is it type product for example.
Right.
You have to use a type of um, you do it that way.
I just want to show it to you type.
Um, sorry.
Do it that way type of right.
You can see that here.
The keyword you can add parentheses and you can say is the entity type type of product.
Alrighty.
Now that's just something that I wanted to show.
You can do it that way.
But the main idea here just wanted to show it.
The main idea here is that you have your generic repository and whatever data type you submit, no matter
if it's a user or product they both run in that add and remove here, and the logic basically stays
the same.
So our generic interface makes sure that you have the contract and that you have the methods.
And our generic, uh, class here makes sure that no matter what data type you submit, they all get
handled, uh, well by this single repository class without duplicating any code.
Alrighty.
And that brings it together generic classes and generic interfaces.
And again, I want to I want to mention, I really want to mention that chances that you will face that
situation when you develop your own applications are really rare, right?
But if you are working as a developer or if you are a front end developer and you want to land a back
end developer position, or you are like starting your first job as a junior developer in dotnet back
end or whatever in just in dotnet.
If you are working with a really professional team that are strictly following design patterns, chances
are that you will stumble across generics and chances are that you will stumble across generic interfaces
that are implemented in generic classes.
And in that case, you want to be prepared, right?
It doesn't mean that you do all of that from scratch.
It just means that if you see that code inside of a project that you're working on, you should be familiar
with that, and you should know what's going on under the hood.
Right.
And you should be able to see the pattern that they are using in the code.
So even right now, if you think like, why would I use that?
The thing is that if you stumble across this and chances are that you will, you should know what's
going on.
So let's move on to the next lesson.