如何使Service Fabric可靠集合不区分大小写?

时间:2019-04-12 10:56:46

标签: c# service-fabric-stateful azure-service-fabric reliable-dictionary

我有一个Stateful Service Fabric服务,并使用通过以下代码创建的IReliableDictionary创建,更新或读取数据:

var dictionary = await StateManager.GetOrAddAsync<IReliableDictionary<string, Entry>>(ReliableDictionaryName);

// Read
using (ITransaction tx = StateManager.CreateTransaction())
{
    ConditionalValue<Entry> result = await dictionary.TryGetValueAsync(tx, name);
    return result.HasValue ? result.Value : null;
}

// Create or update
using (ITransaction tx = StateManager.CreateTransaction())
{
    await dictionary.AddOrUpdateAsync(tx, entry.Name, entry, (key, prev) => entry);
    await tx.CommitAsync();
}    

它可以工作,但是区分大小写。 除了对密钥应用.ToLower()之外,还有什么方法可以使大小写敏感的Reliable Collection存储和获取数据?

1 个答案:

答案 0 :(得分:0)

您看到的此行为主要是默认情况下如何在C#中比较字符串的属性。可靠的字典使用IEquatableIComparable的键实现来执行查找。如果string的默认行为不适合您,则可以实现一种以所需方式执行字符串比较的类型。然后,将新类型用作可靠字典的密钥。您可以实现隐式运算符,以在原始字符串和自定义类型之间进行转换,以使使用变得轻松。这是一个示例:

using System.Runtime.Serialization;

    [DataContract]
    public class CaseInsensitiveString : IEquatable<CaseInsensitiveString>,
                                         IComparable<CaseInsensitiveString>
    {
        #region Constructors

        public CaseInsensitiveString(string value)
        {
            this.Value = value;
        }

        #endregion

        #region Instance Properties

        [DataMember]
        public string Value
        {
            get;
            set;
        }

        #endregion

        #region Instance Methods

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null,
                                obj))
            {
                return false;
            }

            if (ReferenceEquals(this,
                                obj))
            {
                return true;
            }

            if (obj.GetType() != this.GetType())
            {
                return false;
            }

            return this.Equals((CaseInsensitiveString)obj);
        }

        public override int GetHashCode()
        {
            return this.Value != null
                       ? this.Value.GetHashCode()
                       : 0;
        }

        public int CompareTo(CaseInsensitiveString other)
        {
            return string.Compare(this.Value,
                                  other?.Value,
                                  StringComparison.OrdinalIgnoreCase);
        }

        public bool Equals(CaseInsensitiveString other)
        {
            if (ReferenceEquals(null,
                                other))
            {
                return false;
            }

            if (ReferenceEquals(this,
                                other))
            {
                return true;
            }

            return string.Equals(this.Value,
                                 other.Value,
                                 StringComparison.OrdinalIgnoreCase);
        }

        #endregion

        #region Class Methods

        public static bool operator ==(CaseInsensitiveString left,
                                       CaseInsensitiveString right)
        {
            return Equals(left,
                          right);
        }

        public static implicit operator CaseInsensitiveString(string value)
        {
            return new CaseInsensitiveString(value);
        }

        public static implicit operator string(CaseInsensitiveString caseInsensitiveString)
        {
            return caseInsensitiveString.Value;
        }

        public static bool operator !=(CaseInsensitiveString left,
                                       CaseInsensitiveString right)
        {
            return !Equals(left,
                           right);
        }

        #endregion
    }