YoussefMorad1 / DepartmentsHandler

Departments Handler Backend System Using ASP.NET MVC
2 stars 0 forks source link

Desirialize Enum written in a way that standard Parse() won't accept. #8

Open YoussefMorad1 opened 8 months ago

YoussefMorad1 commented 8 months ago

So I'd like to save the EmployeeType in the DataBase as "Part Time", "Full Time". But NOT "PartTime", "FullTime". How to handle this in Serializing & Deserializing?

I'm asking about a more automated way or some library or annotation more than just writing my CustomParse() in fluent api to convert the string into Enum. So I'd like to avoid conditions in serialize/deserialize.

Here is also the current fluent api conversion (that converts from/to "PartTime"/"FullTime"):

builder.Property(e => e.EmployeeType).HasConversion(
  (g) => g.ToString(),
  (str) => (EmplyeeType)Enum.Parse(typeof(EmplyeeType), str, true) 
);
YoussefMorad1 commented 8 months ago

Inquiries

I'm Not sure what's the use [EnumMember(Value = "Full Time")] and if it would actually auto serialize into the written EnumMember? or it will use the standard .ToStirng() written in the fluent api? and if no .ToString() is written what would it serialize to?

I'll make sure to update here after I get my answers when I try with the Create View.

Edit#1 Answers:

YoussefMorad1 commented 8 months ago

After reading your answers, Now another question would be, what is the usage of EnumMember attribute? and do I need to use it? and is it useful to the problem we're facing in this issue?