Hi Guys,
I have the following enum:
[Flags]
public enum PostAssociations
{
None = 0x0,
User = 0x1,
Comments = 0x2,
CommentsUser = 0x3
}
As a starting note, im not sure if those flags are correct.
I'm doing this so that i have a fluent way of defining "Includes" for Entity Framework (as the EF Include method takes a string, which i dont want exposed to the UI).
So i want it so my service layer can accept a PostAssociations, and in my service layer i utilize an extension method to convert this to a string[]. (which my repo then splits up in order to do the .Include).
I haven't done much with Flags Enum's, so i apologize for my ignorance. :)
This is a "truth-table" of what i want (enum value, transformed string[])
None = null
User = new string[] { "User" }
Comments = new string[] { "Comments" }
User, Comments = new string[] { "User", "Comments" }
Comments, CommentsUser = new string[] { "Comments", "Comments.User" }
User, Comments, CommentsUser = new string[] { "User", "Comments", "Comments.User" }
Cannot have CommentsUser without Comments.
So i need help with three things:
- How to setup the enum to match that truth table?
- How do i call the service layer for one of those examples?
- How do i write an extension method to convert that enum to the string array?
Of course, if you guys can think of a better way to do what im trying to do, i'll consider that too. Essentially i'm trying to mask away the "magic strings" of the EF Include behind an Enum, and considering you can do multiple includes (or none), i thought this was a good case for a Flags Enum.
Thanks guys.