SmartEnum | base class | SDK library
kandi X-RAY | SmartEnum Summary
kandi X-RAY | SmartEnum Summary
A base class for quickly and easily creating strongly typed enum replacements in C#.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of SmartEnum
SmartEnum Key Features
SmartEnum Examples and Code Snippets
Community Discussions
Trending Discussions on SmartEnum
QUESTION
I am currently working on a small project with code-first: I have to create a movie database and as usual each movie can have more than one genre (m:n). Since genres are constant, I decided to create an enum genre with all genres in it.
And in the Movie
table I have a list of genres (enum). Obviously this went wrong, because you can't store a list of enums in a database.
So I started looking for answers. I came across many solutions, unfortunately none of them really helped me. So I decided to ask this question. I know this may be a duplicate but the other solutions weren't really helpful.
Some of the solutions I've found are Flags
and SmartEnum
.
I tried both but it didn't really work. Could you please take a look at my code and tell me what I did wrong or if there is another way to convert a list of enums.
Movie:
...ANSWER
Answered 2021-Nov-17 at 21:02This is not really an enum
problem, but rather the fact that you want to model a many-to-many relationship.
If you would need to store a list of int
or string
, it would be the same.
Either you break good practice, and store in your movie table several rows for the same movie (one row for each genre of the movie). In that case you would store directly an enum
in the database. That would be really ugly.
Or you model correctly, meaning you need a table storing your genres. This table could be constructed in a classic way, with a primary key (Id) and value (genre enum value). You could even use the int
representation of the enum
as the primary key, but I don't see any good doing this.
To store enum
s in a database as a property using EF Core, use pre-defined or build-in converters.
You need a genre enum, and a genre class for the mapping:
QUESTION
I am trying to use a custom validator to validate a smart enum (Ardalis.SmartEnum)
Here is the class for the enum:
...ANSWER
Answered 2020-Oct-01 at 12:09Default FV behaviour is to run through all of the rules for the property. If you want to stop when it hits the first failure you'll need to do something like set the cascade mode.
It can be done at a few places depending on how you want the behaviour applied:
- Somewhere early in your app using the static options (
ValidatorOptions.Global.CascadeMode = CascadeMode.Stop;
) - In the validator using
CascadeMode = CascadeMode.Stop;
- On the rule using
RuleFor(r => r.Feeling).Cascade(CascadeMode.Stop).ValidateFeeling();
Your extension bundles 2 rules, you could specify the cascade there but you'd need to use the IRuleBuilderInitial
interface. I suspect that'd mean that you couldn't chain that rule with other rules in the validator for the property unless it was the first rule. Probably for the best anyway as it'd obfuscate the cascade mode from the validator which has a certain smell to it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install SmartEnum
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page