In this lesson, we will talk about one of the more advanced C sharp generic topics, which is generic
interfaces.
Now don't get confused.
In earlier lessons we have used generic classes which implement default interfaces, but this time we
will use a default class which implements a generic interface.
So we basically just switch it around from a generic class to a generic interface instead.
And for the sake of this video, I will put all of the classes and interfaces that we need into this
file right here.
For sure, in a real world application you will put that into separate files.
So let's create an internal interface.
You know it could also be public or just nothing right.
So internal interface and we will call it I repository.
As we have discovered earlier, repository is basically just a term for something where you can store
data, right.
Usually you take your data and you store it inside of a repository for example.
That's the term especially a pretty commonly used in web development.
Now we turn this interface into a generic one by using the angle brackets and then specifying the type.
Let's call it t.
Now let's open up our interface here and let's just add some more functionality.
Let's say void add.
And then we add t and a t for example.
Or what whatever that could be.
Right.
But we don't want to make it too specific.
Let's say we would like to create a product repository in the end.
But since this interface here that we will use in that class is generic, we don't want to call the
parameter here a product, right?
Because it's generic.
We don't want to be too specific if it's not mandatory, if it's not needed for our application.
Right.
So void remove the same.
We want to remove a specific entity or a specific instance.
Right.
Instance would also be a word that you could use.
But most of the time you will find the term entity or the word entity because entity is pretty generic,
right?
Awesome, great.
As I've said, we want to create a product repository.
So a repository where we can store products, or a repository where we can store fruits, or a repository
where we could store person information, whatever.
Right.
We want to do that with a product repository because that's again something you will face pretty often
in real world applications.
So let's go ahead and let's create an internal class.
Let's call it product.
And inside here we will have an int ID right.
So we can do it just that way public int id.
And we can also have a prop.
Let's just uh give it a string.
Let's call it and let's call it name.
Cool.
So in that way we now have a product.
And now we can create our product repository.
So go ahead and let's create our internal class product repository.
This time we are implementing an interface our AI repository.
There we go.
It's generic right.
We now specify what class we like to store inside that repository what type our product.
Right.
So this time we are not saying our product repository class is generic.
Instead the generic part is specified in the interface already.
Right.
So our repository let's create our voids right here.
So let's simply say public void.
And we can go ahead and say add product entity.
Right.
We are referring to our product that we got right here.
And get our public void remove product entity.
We got that one right here.
All right.
So we have now created an AI repository interface which is generic.
We can put in any type basically.
And then we take our class product repository here and create a contract so that we have to implement
those methods right here.
So it's not that crazy at all.
Right.
The key question is why would you choose a generic repository over a generic class or the other way
around where you would go with a generic interface like this one here, if you want to have flexible
contracts across multiple classes, right, so that you would have like a, um, a shoo repository or
user repository or whatever.
And inside there you want to have exactly the same methods.
You know, this is more a question of, um, of code design and not really about functionality in the
first place.
All right.
And on the other side, you would pick a generic class over a generic interface if you want to share
the same functionality across multiple classes.
So usually when you build real world software, you will have a design pattern, or your team will follow
a design pattern.
And one very commonly used design pattern is the repository design pattern, which is basically what
we.
Do it right here.
We have a generic interface.
That one has a generic type T for us right here it's product.
And then inside that interface you have specific methods, specific contracts that all of the classes
which are implementing the generic interface have to implement.
So as you can see, creating a generic interface isn't that hard.
And it really opens up a door to next level code.
Right now we can take it even further.
Let's move on to the next lesson.