我有以下枚举:
[Flags]
public enum PostAssociations
{
None = 0x0,
User = 0x1,
Comments = 0x2,
CommentsUser = 0x3
}
作为开始说明,我不确定这些标志是否正确。
我这样做是为了以一种流畅的方式为实体框架定义“包含”(因为 EF Include 方法采用一个字符串,我不想将其暴露给 UI)。
所以我想要它,以便我的服务层可以接受 PostAssociations,并且在我的服务层中,我利用扩展方法将其转换为字符串[]。 (然后我的存储库将其拆分以执行 .Include)。
我没有对 Flags Enum 做太多事情,所以我为我的无知道歉。 :)
这是我想要的“真值表”(枚举值,转换后的字符串[])
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" }
不能有没有注释的 CommentsUser。
所以我需要三件事的帮助:
- 如何设置枚举以匹配真值表?
- 我如何为这些示例之一调用服务层?
- 如何编写扩展方法来将该枚举转换为字符串数组?
当然,如果你们能想到更好的方法来完成我想做的事情,我也会考虑的。本质上,我试图掩盖枚举后面的 EF Include 的“魔术字符串”,并且考虑到您可以执行多个包含(或不包含),我认为这是 Flags Enum 的一个很好的例子。
谢谢你们。
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.
发布评论
评论(2)
如果您使用标志创建枚举:
那就更有意义了。在您当前的代码中,我不明白您想要实现什么目标。
否则,我认为你根本不需要一个基于标志的枚举......
if you created the enum with flags:
that would make more sense. In your current code i don't understand what you're trying to achieve.
otherwise, i don't think you want a flag based enum at all...
我的坏家伙,我对标志枚举的缺乏经验导致了一个令人困惑的问题。
我的场景对于标志枚举无效。
相反,我选择使用 PostAssociations[]。
我的服务层:
扩展方法:
使用方法:
My bad guys, my inexperience with flags enums has resulted in a confusing question.
My scenario isn't valid for flags enum.
Instead, i have opted to use a PostAssociations[].
My service layer:
The extension method:
Usage: