C
C#2y ago
Kiel

✅ HashSet property with List backing field

Posting here instead of in #database because it's not really related to db-specific stuff. For reasons described vaguely here: https://github.com/npgsql/efcore.pg/issues/2301, I am looking to implement a HashSet<T> property with a List<T> backing field which updates appropriately as I add and remove objects from the publicly exposed HashSet<T>. example class:
public sealed class GlobalUser
{
private List<Snowflake> _blacklistedHighlightUsers = null!;

public HashSet<Snowflake> BlacklistedHighlightUsers
{
get { /* TODO */ }
set { /* TODO */ }
}
}
public sealed class GlobalUser
{
private List<Snowflake> _blacklistedHighlightUsers = null!;

public HashSet<Snowflake> BlacklistedHighlightUsers
{
get { /* TODO */ }
set { /* TODO */ }
}
}
I want the underlying list field to be updated every time I modify the public property. Is there a way to accomplish this without any of the following? - never calling Add/Remove/etc methods and instead manually re-assigning the BlacklistedHighlightUsers every time - implementing my own wrapper class which overrides the Add/Remove/etc methods to somehow write to this underlying list - giving up on doing this in the first place and using a public List<T> and tossing on .ToHashSet().ToList() or .Distinct().ToList() everywhere I go all are what I'd consider ugly, hacky solutions that I'm not a fan of as they either mean I am overcomplicating things or there really just is not a solution
3 Replies
Saber
Saber2y ago
whats the point of the list backing field?
Kiel
KielOP2y ago
you can only specify an "array" value converter if your property type is List<T> or T[]. Any other property type will either a) not show the extension method due to constraints, or b) if you force the type conversion manually a error is produced at migration generation time. You can't (by default, at least) specify enumerable value conversions, as using HasPostgresArrayConversion is heavily encouraged instead. I may have found a solution to my problem accidentally while scrolling just a tiny bit further in their issues tab on GitHub searching for HasPostgresArrayConversion, which happens to be someone trying to do the exact same thing as me: https://github.com/npgsql/efcore.pg/issues/1927 so this may be a non issue lol
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server