ardalis / SmartEnum

A base class for quickly and easily creating strongly typed enum replacements in C#.
MIT License
2.2k stars 170 forks source link

Can SmartEnum support collections of enums? #133

Open rich0726 opened 3 years ago

rich0726 commented 3 years ago

I have a situation where I have a property in the base object of my model that is a collection of enums.

Example: Base object is Person. A person has a collection of Sports objects, for sports they play. They can play more than one sport. Let's assume it's an open enum, with several "known" sports, but with the ability to expand to additional sports that one might play. And just for completion, sports can have more than one person that plays them, so it's a true many-to-many scenario.

How would I use SmartEnum to model this and also persist to database through EFCore?

Thanks!

nhwilly commented 2 years ago

Exact same. Did you ever figure it out?

hassanrazakhalid commented 2 years ago

Yes i am stuck with same problem

ghhv commented 2 years ago

Me four..

ardalis commented 2 years ago

Why are you wanting to use an enum (smart or otherwise) for this instead of just an entity? An enum generally has a known set of options that doesn't change, and a smart enum just extends that with more options for behavior and properties. Is it the programming experience of seeing the set of options when you hit Sports. ? You can get a similar behavior with entities and using constants for the sports IDs.

ghhv commented 2 years ago

Hi Steve, in one of my examples, I have a Smart enum for address types. So an address can be say a billing address and a residential address.. I'm using Smartenum properties to determine different address types for a business as compared to a person for example. So rather then duplicating an address entity, I use the enum collection to assign multiple purposes.

I seem to be stuck with the conversion process. I'm using this technique for the enum collection which works great but when using a Smartenum, I start getting this error again:

System.NotSupportedException: Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'LocationPurpose'. Path: $.person.locations[0].purposes[0]

I've added a public constructor like so:

        public LocationPurpose(string name, int value) : base(name, value) //was private
        {
        }

and have the class decorated with [JsonConverter(typeof(SmartEnumNameConverter<LocationPurpose, int>))]

Any thoughts?

ghhv commented 2 years ago

Well, how about that.. thinking out loud above, I've fixed it.. I think possibly it was my mistake with confusion between systemtextjson and newtonsoftjson.. Being lazy I had not converted the enum collection code above to systemtextjson.. just added the newtonsoft namespace which was a mistake I think. Anyway, the quick and dirty fix is add systemtextjson.serialization to your smartenum class for the constructor decorator and in the enum collection code, reference newtonsoft.json directly.

If that doesn't make sense to anyone who needs it, let me know.

nhwilly commented 2 years ago

I gave up on System.Text.Json.

I figure I'll come back to out in a year or so. Can't find my notes at the moment.

But I ran into the same thing you did.