简体   繁体   English

C#-[Flags]枚举和扩展方法帮助

[英]C# - Help with [Flags] Enum & Extension Method

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). 我这样做是为了让我能流畅地为实体框架定义“包含”(因为EF Include方法采用字符串,我不想向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[]. 所以我想要它,以便我的服务层可以接受PostAssociations ,并且在我的服务层中,我利用扩展方法将其转换为string []。 (which my repo then splits up in order to do the .Include). (然后我的存储库拆分以执行.include)。

I haven't done much with Flags Enum's, so i apologize for my ignorance. 我对Flags Enum的处理不多,因此对我的无知表示歉意。 :) :)

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. 没有评论就不能有CommentUser。

So i need help with three things: 所以我需要三件事的帮助:

  1. How to setup the enum to match that truth table? 如何设置枚举以匹配该真值表?
  2. How do i call the service layer for one of those examples? 对于这些示例之一,我怎么称呼服务层?
  3. 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. 本质上,我试图掩盖Enum后面的EF Include的“魔术字符串”,并且考虑到您可以进行多个include(或不包括),我认为这对于Flags Enum是一个很好的例子。

Thanks guys. 多谢你们。

if you created the enum with flags: 如果您创建带有标志的枚举:

[Flags]
public enum PostAssociations
{    
    None = 0x0,
    User = 0x1,
    Comments = 0x2,
    CommentsUser = User|Comments,
}

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... 否则,我不认为您根本不需要基于标志的枚举...

My bad guys, my inexperience with flags enums has resulted in a confusing question. 我的坏家伙,我对flags枚举的经验不足,导致了一个令人困惑的问题。

My scenario isn't valid for flags enum. 我的情况不适用于标志枚举。

Instead, i have opted to use a PostAssociations[]. 相反,我选择使用PostAssociations []。

My service layer: 我的服务层:

public Post FindSingle(int postId, PostAssociations[] postAssociations)
{
   return repo.Find(postAssocations.ToEfInclude()).WithId(postId);
}

The extension method: 扩展方法:

public static string[] ToEfInclude(this PostAssociations[] postAssocations)
{
    if (postAssocations == null) return null;

    List<string> includeAssociations = new List<string>();

    if (postAssocations.Contains(PostAssociations.User))
        includeAssociations.Add("User");
    if (postAssocations.Contains(PostAssociations.Comments))
    {
        if (postAssocations.Contains(PostAssociations.CommentsUser))
        {
            includeAssociations.Add("Comments.User");   
        }

        includeAssociations.Add("Comments");
    }

    return includeAssociations.ToArray();
}

Usage: 用法:

PostAssociations[] associations = new[] { PostAssociations.Comments, PostAssociations.User, PostAssociations.CommentsUser };    
return service.Find(1, associations);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM