C
C#•10mo ago
dima

Generics question

So guys, I am having a little bit of fun with generics. I need some help though. Lets say I have a class such as this
public abstract class BaseEntity<T>
{
public T Id { get; set; }
}
public abstract class BaseEntity<T>
{
public T Id { get; set; }
}
And I define a Country entity like this
public class Country : BaseEntity<Guid>
{
// Additional properties and methods specific to Country class
}
public class Country : BaseEntity<Guid>
{
// Additional properties and methods specific to Country class
}
Country.Id now is Guid. If I were to implement Currency entity that references Country, I would have to define it like this
public class Currency : BaseEntity<int>
{
public Guid CountryId { get; set; }
}
public class Currency : BaseEntity<int>
{
public Guid CountryId { get; set; }
}
Yada-yada-yada, some changes occur. I have X entities that reference Guid CountryId. Now, I decide its better to make Country have an int Id, so I change it to inherit BaseEntity<int>. I now also have to replicate that change to X other entities that have CountryId as a reference. The question: Is there a way to reference a type to achieve something along these lines:
public class Currency : BaseEntity<int>
{
public typeof(Country.Id) CountryId { get; set; }
}
public class Currency : BaseEntity<int>
{
public typeof(Country.Id) CountryId { get; set; }
}
This way, when I impact the Country.Id type, it's automatically updated everywhere else 🙂 From what I understand, since all of this is known compile-time, there isn't really a reason why I shouldn't be able to do this, no? All help appreciated ^_^
4 Replies
Pobiega
Pobiega•10mo ago
There is no way to do this, no not as such at least. A better solution is to use something like https://github.com/SteveDunn/Vogen to generate the actual key type so for example, you could do
[ValueObject<int>]
public partial struct CountryId
{
}
[ValueObject<int>]
public partial struct CountryId
{
}
and if you wanted to change, you'd just have to change the type in the attribute everything else in your codebase would be using the CountryId type, so there will be no issues there
dima
dimaOP•10mo ago
I mean... I understand that this would work... And that library seems to handle a lot of cases and at first glance seems to be well executed. But I still don't understand what exactly is the limiting factor to do this native/OOTB without 3rd party libraries... The type for the Id prop is inherently defined in the Country definition. I guess the question I'm asking is; why isn't this natively supported / why can't we do this?
Angius
Angius•10mo ago
Generics can go from parent to child, not the other way around Just like parameters of methods, constructors, whatever You can't pass them up the chain
Pobiega
Pobiega•10mo ago
If your question is "Why wasn't the language designed this way" you should ask the language design team Most people here can't answer as to why C# was designed as it is, we can only explain how the version that was implemented works

Did you find this page helpful?