In this lesson, we will take a look at constraints for generic interfaces.
So I highly recommend that you.
If you haven't watched the previous lecture about generic interfaces, please go ahead and do that.
First, just a small recap.
We have our program here.
Then we have a product class which contains an ID and a name.
We have a generic interface which contains two uh voids add and remove.
Right.
It's called a repository.
Then we created a class called Product Repository, which implements the generic interface and specifies
the type as product.
So we have already discovered how to use constraints on generic classes.
So let's complete this by exploring how we can use constraints on generic interfaces.
So let's go to our internal interface that we have right here our repository we've got add and remove.
But now let's just assume that we definitely want to make sure that inside here we have a public ID,
but we do not want to specify our not a public ID but just an ID property.
Right.
But we don't want to have that inside of our repository.
Now we are just exploring the boundaries.
Right.
But how you would do that in real world depends on what code design you choose.
You know, you can go really crazy on architecture, but most of the time it's better to keep it a bit
simple, right?
We are just exploring boundaries, as I said.
But if you face a situation that someone says, this is our code design, our guideline here, you have
to do it that way, you should be prepared, right?
So as I said, we don't want to put our ID into this class because someone decided that we have a internal
interface, which is called I entity.
And that entity simply makes sure that we have an ID.
So let's write down int id and it's a get.
There we go.
Now we have that interface.
And now we want to make sure that our I repository that we have right here, our generic repository
has a constraint where we can say where t is I entity.
In that case.
Now if we save it and we scroll down, you can see that our product repository is not working in that
way.
And that's because the product that we submit here as a type is not implementing the I entity.
Right.
Because we submit the type product in our repository for our product repository class right here.
But we are saying that we only allow this if our type T is implementing the I entity.
So what we have to do is to go to our class product to say, oh yeah, this is our entity.
We should have an ID here.
So let's implement the I entity.
And as our product class already implements a public int ID, our interface is implemented successfully.
And we can now start using it.
So in that case we make sure that whatever class we have here for our entity has an ID property.
Right.
So again, just to make it obvious, let's duplicate that class here.
And let's say this is called user.
And we remove our I entity here.
And we say ah we don't need an ID we simply say the user has a name, nothing else.
Right now.
This should not work if we create a repository for that because it's again against our code guidelines.
Right.
And it will not work because we cannot store it to a database because we need an ID property so that
we have a primary key for example.
Right.
So again I will duplicate that class here.
We want to create another repository for our users.
So we say user repository.
We implement our I repository for the user.
And it will say let me just replace that.
It doesn't work.
It doesn't work because the constraint at our I repository is failing because we are not implementing
the I entity at our user.
Right?
And if we hover above that, we will find more information on that.
And in that way a developer that joins the application starts developing, knows, ah, I have to do
something here.
Right?
He will find out.
Okay, I have to go to the class user.
I have to use the I entity interface.
Right.
He can see okay I got a problem here.
What do I have to do?
I click on show potential fixes, click on implement interface and you can see public int ID.
Now we don't want to throw any implementation here.
We can simply say get that's totally fine.
And we are passing our constraint now.
And now he has this user repository.
He's able to store users inside of his repository which is connected to a database, for example, if
you would use Microsoft entities.
Framework, our idea.
And that's the use case of when you would want to use a constraint for a generic interface.
And that's definitely some more advanced stuff.
But you are ready for that, I'm sure.
And still, as I say all the time, there is still more to discover on generics.
And it's a super exciting topic.
Like, I really like and enjoy teaching it because, you know, it opens up the door to so much more
advanced and well designed code.
So it's really fun, uh, working through that, I hope.
Great.
So let's move on to the next lesson.