From 027177a1a8287fd1c9a3691415f81f897736d6fe Mon Sep 17 00:00:00 2001 From: Sergei Pavlov Date: Tue, 17 Sep 2024 17:16:04 -0700 Subject: [PATCH] Make `IndexInfoRef` readonly struct --- Orm/Xtensive.Orm/Orm/Model/IndexInfoRef.cs | 83 ++++------------------ 1 file changed, 15 insertions(+), 68 deletions(-) diff --git a/Orm/Xtensive.Orm/Orm/Model/IndexInfoRef.cs b/Orm/Xtensive.Orm/Orm/Model/IndexInfoRef.cs index 12ad5cd538..c40326692a 100644 --- a/Orm/Xtensive.Orm/Orm/Model/IndexInfoRef.cs +++ b/Orm/Xtensive.Orm/Orm/Model/IndexInfoRef.cs @@ -19,21 +19,19 @@ namespace Xtensive.Orm.Model /// [Serializable] [DebuggerDisplay("IndexName = {IndexName}, TypeName = {TypeName}")] - public sealed class IndexInfoRef + public readonly struct IndexInfoRef(IndexInfo indexInfo) { - private const string ToStringFormat = "Index '{0}' @ {1}"; - /// /// Name of the index. /// - public string IndexName { get; private set; } + public string IndexName { get; } = indexInfo.Name; /// /// Name of the reflecting type. /// - public string TypeName { get; private set; } + public string TypeName { get; } = indexInfo.ReflectedType.Name; - public TupleDescriptor KeyTupleDescriptor { get; private set; } + public TupleDescriptor KeyTupleDescriptor { get; } = indexInfo.KeyTupleDescriptor; /// /// Resolves this instance to object within specified . @@ -41,59 +39,31 @@ public sealed class IndexInfoRef /// Domain model. public IndexInfo Resolve(DomainModel model) { - TypeInfo type; - if (!model.Types.TryGetValue(TypeName, out type)) + if (!model.Types.TryGetValue(TypeName, out var type)) throw new InvalidOperationException(string.Format(Strings.ExCouldNotResolveXYWithinDomain, "type", TypeName)); - IndexInfo index; - if (!type.Indexes.TryGetValue(IndexName, out index)) { - var hierarchy = type.Hierarchy; - if (hierarchy != null && hierarchy.InheritanceSchema == InheritanceSchema.SingleTable && hierarchy.Root.Indexes.TryGetValue(IndexName, out index)) + if (!type.Indexes.TryGetValue(IndexName, out var index)) { + if (type.Hierarchy is { } hierarchy && hierarchy.InheritanceSchema == InheritanceSchema.SingleTable && hierarchy.Root.Indexes.TryGetValue(IndexName, out index)) return index; throw new InvalidOperationException(string.Format(Strings.ExCouldNotResolveXYWithinDomain, "index", IndexName)); } - return index; } /// /// Creates reference for . /// - public static implicit operator IndexInfoRef (IndexInfo indexInfo) - { - return new IndexInfoRef(indexInfo); - } + public static implicit operator IndexInfoRef (IndexInfo indexInfo) => new(indexInfo); #region Equality members, ==, != /// - public bool Equals(IndexInfoRef other) - { - if (other is null) - return false; - if (ReferenceEquals(this, other)) - return true; - return - other.IndexName==IndexName && - other.TypeName==TypeName; - } + public bool Equals(IndexInfoRef other) => IndexName == other.IndexName && TypeName == other.TypeName; /// - public override bool Equals(object obj) - { - if (ReferenceEquals(this, obj)) - return true; - return Equals(obj as IndexInfoRef); - } + public override bool Equals(object obj) => obj is IndexInfo other && Equals(other); /// - public override int GetHashCode() - { - unchecked { - return - ((IndexName!=null ? IndexName.GetHashCode() : 0) * 397) ^ - (TypeName!=null ? TypeName.GetHashCode() : 0); - } - } + public override int GetHashCode() => HashCode.Combine(IndexName,TypeName); /// /// Implements the operator ==. @@ -103,10 +73,7 @@ public override int GetHashCode() /// /// The result of the operator. /// - public static bool operator ==(IndexInfoRef x, IndexInfoRef y) - { - return Equals(x, y); - } + public static bool operator ==(IndexInfoRef x, IndexInfoRef y) => x.Equals(y); /// /// Implements the operator !=. @@ -116,31 +83,11 @@ public override int GetHashCode() /// /// The result of the operator. /// - public static bool operator !=(IndexInfoRef x, IndexInfoRef y) - { - return !Equals(x, y); - } + public static bool operator !=(IndexInfoRef x, IndexInfoRef y) => !x.Equals(y); #endregion /// - public override string ToString() - { - return string.Format(ToStringFormat, IndexName, TypeName); - } - - - // Constructors - - /// - /// Initializes a new instance of this class. - /// - /// object to make reference for. - public IndexInfoRef(IndexInfo indexInfo) - { - IndexName = indexInfo.Name; - TypeName = indexInfo.ReflectedType.Name; - KeyTupleDescriptor = indexInfo.KeyTupleDescriptor; - } + public override string ToString() => $"Index '{IndexName}' @ {TypeName}"; } -} \ No newline at end of file +}