简体   繁体   English

转换字符串值域枚举器的最有效方法是什么?

[英]What is the most efficient way to translate a string value domain enumerator?

I have a public entity contract to expos in my service but internally I have some differences. 我有一份公开服务的公共合同,但在内部却有所不同。 In this specific case I have a string value domain represented as an enumerator. 在这种特定情况下,我有一个表示为枚举数的字符串值域。 The domain is the same in both public and internal scope, but the names used are different and I need to translate from one to another. 公共和内部范围中的域都是相同的,但是使用的名称是不同的,我需要从一个翻译到另一个。 First I thought to keep to the basics and use a simple switch, but then I thought using a mirror enumerator with the internal string values I need and then use it with a ToString() . 首先,我考虑了基础知识,并使用了一个简单的开关,但是随后,我考虑使用带有我需要的内部字符串值的镜像枚举器,然后将其与ToString() Wich one is the most efficient way to do this stuff? 哪一种是最有效的方法?

Enumerator 枚举器

public enum PublicDomain {
    AAA = 0,
    BBB = 1,
    CCC = 2,
    DDD = 3
}

private enum InternalDomain {
    W = 0,
    X = 1,
    Y = 2,
    Z = 3
}

Keep to the basics 保持基础

string stringValue = null;

switch (publicEnumValue)
{
    case PublicDomain.AAA:
        stringValue = "W";
        break;
    case PublicDomain.BBB:
        stringValue = "X";
        break;
    case PublicDomain.CCC:
        stringValue = "Y";
        break;
    case PublicDomain.DDD:
        stringValue = "Z";
        break;
}

foo(stringValue);

Enumerator > Enumerator > ToString() 枚举>枚举> ToString()

private enum InternalDomain {
    W = 0,
    X = 1,
    Y = 2,
    Z = 3
}

foo(((InternalDomain)publicEnumValue).ToString());

I would not be concerned with "efficiency" but with maintainability. 我不会关心“效率”,而是关心可维护性。

I would have an extension to convert public enum to internal enum 我将扩展以将公共enum转换为内部enum

static class PublicDomainExtensions
{
   public InternalDomain ToInternal(this PublicDomain e)
   {
      return (InternalDomain) e;
   }
 }

This way if InternalDomain or PublicDomain ever change, all you need to do is rewrite ToInternal . 这样,如果InternalDomainPublicDomain了变化,您要做的就是重写ToInternal

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

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