It seems to me that an academic is not a user, although he seems to be. If he is not, there is no inheritance. You can force that to be true, and some will say it is, but I wouldn’t go that way.
An academic circumstantially assumes the ability to be a user. It seems to me that they are not papers directly related one as the child of the other, but rather that stand in the way at a given time, they only associate.
People tend to look at relationships in ways that find things in common, but they’re not the same thing, they’re two different things that work together. In this case the association is more interesting.
What’s best for this case I can’t say because I don’t know everything about the problem. There are some options.
The most obvious is to associate the academic with the user creating a property or even a field that indicates which user profile he should adopt. This way you make that composition in place of inheritance. So when you need to use an academic you adopt this type. Already needs the user information he has will use the type of user, with the ease of having a direct access to it.
Eventually you may want to make a bi-directional association and make it easier for the user to access the academic. I don’t like it and may bring future problems if you change some concepts.
Ever thought that a user could be an academic and something else at the same time? Even if that’s not true today, it will never happen?
It may be that the correct thing would be to have another object that associates the various roles he assumes.
That’s why modeling correctly at first is important and should give you the flexibility to change it any way you want. You don’t control the world. You can risk that the day will always have 24 hours, even that can change, even if almost impossible, but one day someone can invent that they should have a decimal measure for the day and be accepted like that. But paper relationships can change easily. If you change, do you have to reshape the whole system? Pretty much rewrite everything that involves it?
On the other hand I may be wrong, I have little information about the problem, and even if I had enough I could still be wrong. Some degree of risk we always have.
One of the things that many people don’t understand about object orientation is that the paradigm should facilitate system modeling by giving the chance to change any part without further trauma. Only conceptualizing very well comes to that, the code is just a detail.
Nor am I saying that every system deserves this effort. It doesn’t always fit all preciousness.
If I make inheritance in this case it seems strange, but it is not absurdly wrong as occurs in many examples that we see around where the person uses inheritance to join car with banana because at a certain point he needs to use both in the same place. If that happens I think one of Id
s becomes unnecessary.
So I guess it would be something like this:
public class Academico {
public int Id { get; set; }
public Professor Professor { get; set; }
public Usuario Usuario { get; set; }
}
or
public class Academico {
public int Id { get; set; }
public Professor Professor { get; set; }
public int UsuarioId { get; set; }
}
I put in the Github for future reference.
Note that user looks more like a composition and the teacher is an association. See more on What is the difference between Association, Aggregation and Composition in OOP?.
Association is usually better than inheritance.
– Victor Stafusa
Do you have a practical example for me ?
– Alerf Morais
In C# I do not have of readiness. But soon appears someone who has.
– Victor Stafusa
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.
– Maniero