Skip to content

Commit 94c187c

Browse files
VCST-2846: Use *IgnoreCase() extension methods (#2892)
1 parent 0e53122 commit 94c187c

35 files changed

+221
-225
lines changed

src/VirtoCommerce.Platform.Caching/Redis/RedisPlatformMemoryCache.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace VirtoCommerce.Platform.Redis
1313
{
1414
public class RedisPlatformMemoryCache : PlatformMemoryCache
1515
{
16-
private static string _instanceId { get; } = $"{Environment.MachineName}_{Guid.NewGuid():N}";
16+
private static readonly string _instanceId = $"{Environment.MachineName}_{Guid.NewGuid():N}";
1717
private bool _isSubscribed;
1818
private readonly ISubscriber _bus;
1919
private readonly RedisCachingOptions _redisCachingOptions;
@@ -41,7 +41,7 @@ public RedisPlatformMemoryCache(IMemoryCache memoryCache
4141

4242
private void CacheCancellableTokensRegistry_OnTokenCancelled(TokenCancelledEventArgs e)
4343
{
44-
var message = new RedisCachingMessage { InstanceId = _instanceId, IsToken = true, CacheKeys = new[] { e.TokenKey } };
44+
var message = new RedisCachingMessage { InstanceId = _instanceId, IsToken = true, CacheKeys = [e.TokenKey] };
4545
Publish(message);
4646
_log.LogTrace("Published token cancellation message {Message}", message.ToString());
4747
}
@@ -65,7 +65,7 @@ protected virtual void OnMessage(RedisChannel channel, RedisValue redisValue)
6565
{
6666
var message = JsonConvert.DeserializeObject<RedisCachingMessage>(redisValue);
6767

68-
if (!string.IsNullOrEmpty(message.InstanceId) && !message.InstanceId.EqualsInvariant(_instanceId))
68+
if (!string.IsNullOrEmpty(message.InstanceId) && !message.InstanceId.EqualsIgnoreCase(_instanceId))
6969
{
7070
_log.LogTrace("Received message {Message}", message.ToString());
7171

@@ -95,7 +95,7 @@ public override bool TryGetValue(object key, out object value)
9595

9696
protected override void EvictionCallback(object key, object value, EvictionReason reason, object state)
9797
{
98-
var message = new RedisCachingMessage { InstanceId = _instanceId, CacheKeys = new[] { key } };
98+
var message = new RedisCachingMessage { InstanceId = _instanceId, CacheKeys = [key] };
9999
Publish(message);
100100
_log.LogTrace("Published message {Message} to the Redis backplane", message);
101101

src/VirtoCommerce.Platform.Core/Common/EnumUtility.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static T SafeParseFlags<T>(string value, T defaultValue, string separator
3636

3737
if (!string.IsNullOrEmpty(value))
3838
{
39-
var parts = value.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries);
39+
var parts = value.Split([separator], StringSplitOptions.RemoveEmptyEntries);
4040

4141
foreach (var part in parts)
4242
{
@@ -78,7 +78,8 @@ public static string SafeRemoveFlagFromEnumString<T>(string value, T flag, char
7878
else
7979
{
8080
var parts = value.Split(separator, StringSplitOptions.RemoveEmptyEntries);
81-
result = string.Join(separator, parts.Where(x => !x.EqualsInvariant(flag.ToString())));
81+
var flagString = flag.ToString();
82+
result = string.Join(separator, parts.Where(x => !x.EqualsIgnoreCase(flagString)));
8283
}
8384
}
8485
return result;

src/VirtoCommerce.Platform.Core/Common/SemanticVersion.cs

+30-13
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,16 @@ public class SemanticVersion : IComparable
1010
{
1111
private readonly Version _version;
1212

13-
public static readonly char[] Delimiters = { '.', '-' };
13+
public static readonly char[] Delimiters = ['.', '-'];
1414

15-
public static readonly Regex SemanticVersionStrictRegex = new Regex(
15+
public static readonly Regex SemanticVersionStrictRegex = new(
1616
@"^(?<Version>([0-9]|[1-9][0-9]*)(\.([0-9]|[1-9][0-9]*)){2,3})" +
1717
@"(?>\-(?<Prerelease>[0-9A-Za-z\-\.]+))?(?<Metadata>\+[0-9A-Za-z-]+)?$",
1818
RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
1919

2020
public SemanticVersion(Version version)
2121
{
22-
if (version == null)
23-
{
24-
throw new ArgumentNullException(nameof(version));
25-
}
22+
ArgumentNullException.ThrowIfNull(version);
2623

2724
_version = NormalizeVersionValue(version);
2825
}
@@ -46,8 +43,7 @@ public SemanticVersion(Version version)
4643

4744
public bool IsCompatibleWithBySemVer(SemanticVersion other)
4845
{
49-
if (other == null)
50-
throw new ArgumentNullException(nameof(other));
46+
ArgumentNullException.ThrowIfNull(other);
5147

5248
//MAJOR version when you make incompatible API changes,
5349
var retVal = Major == other.Major;
@@ -61,8 +57,7 @@ public bool IsCompatibleWithBySemVer(SemanticVersion other)
6157

6258
public bool IsCompatibleWith(SemanticVersion other)
6359
{
64-
if (other == null)
65-
throw new ArgumentNullException(nameof(other));
60+
ArgumentNullException.ThrowIfNull(other);
6661

6762
var comparisonResult = CompareTo(other);
6863
return comparisonResult <= 0;
@@ -81,7 +76,7 @@ public static SemanticVersion Parse(string value)
8176
{
8277
var normalizedVersion = NormalizeVersionValue(versionValue);
8378
var result = new SemanticVersion(normalizedVersion);
84-
if (((ICollection<Group>)match.Groups).Any(x => x.Name.EqualsInvariant("Prerelease")))
79+
if (((ICollection<Group>)match.Groups).Any(x => x.Name.EqualsIgnoreCase("Prerelease")))
8580
{
8681
result.Prerelease = match.Groups["Prerelease"].Value;
8782
}
@@ -180,22 +175,27 @@ public override int GetHashCode()
180175

181176
public int CompareTo(object obj)
182177
{
183-
if (obj == null)
184-
throw new ArgumentNullException(nameof(obj));
178+
ArgumentNullException.ThrowIfNull(obj);
185179

186180
var other = (SemanticVersion)obj;
187181

188182
var result = Major.CompareTo(other.Major);
189183
if (result != 0)
184+
{
190185
return result;
186+
}
191187

192188
result = Minor.CompareTo(other.Minor);
193189
if (result != 0)
190+
{
194191
return result;
192+
}
195193

196194
result = Patch.CompareTo(other.Patch);
197195
if (result != 0)
196+
{
198197
return result;
198+
}
199199

200200
return CompareComponent(Prerelease, other.Prerelease, true);
201201
}
@@ -205,12 +205,19 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
205205
var aEmpty = string.IsNullOrEmpty(a);
206206
var bEmpty = string.IsNullOrEmpty(b);
207207
if (aEmpty && bEmpty)
208+
{
208209
return 0;
210+
}
209211

210212
if (aEmpty)
213+
{
211214
return nonemptyIsLower ? 1 : -1;
215+
}
216+
212217
if (bEmpty)
218+
{
213219
return nonemptyIsLower ? -1 : 1;
220+
}
214221

215222
var aComps = a.Split(Delimiters);
216223
var bComps = b.Split(Delimiters);
@@ -227,17 +234,27 @@ private static int CompareComponent(string a, string b, bool nonemptyIsLower = f
227234
{
228235
r = aNum.CompareTo(bNum);
229236
if (r != 0)
237+
{
230238
return r;
239+
}
231240
}
232241
else
233242
{
234243
if (aIsNum)
244+
{
235245
return -1;
246+
}
247+
236248
if (bIsNum)
249+
{
237250
return 1;
251+
}
252+
238253
r = string.CompareOrdinal(ac, bc);
239254
if (r != 0)
255+
{
240256
return r;
257+
}
241258
}
242259
}
243260

src/VirtoCommerce.Platform.Core/Common/SortInfo.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ public enum SortDirection
2121

2222
public class SortInfo : IEquatable<SortInfo>
2323
{
24+
private static readonly char[] _columnSeparators = [';'];
25+
private static readonly char[] _directionSeparators = [':', '-'];
26+
2427
public string SortColumn { get; set; }
2528
public SortDirection SortDirection { get; set; }
2629

2730
public override string ToString()
2831
{
29-
return SortColumn + ":" + (SortDirection == SortDirection.Descending ? "desc" : "asc");
32+
return SortColumn + (SortDirection == SortDirection.Descending ? ":desc" : string.Empty);
3033
}
3134

3235
public static string ToString(IEnumerable<SortInfo> sortInfos)
@@ -37,13 +40,16 @@ public static string ToString(IEnumerable<SortInfo> sortInfos)
3740
public static IEnumerable<SortInfo> Parse(string sortExpr)
3841
{
3942
var retVal = new List<SortInfo>();
43+
4044
if (string.IsNullOrEmpty(sortExpr))
45+
{
4146
return retVal;
47+
}
4248

43-
var sortInfoStrings = sortExpr.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
49+
var sortInfoStrings = sortExpr.Split(_columnSeparators, StringSplitOptions.RemoveEmptyEntries);
4450
foreach (var sortInfoString in sortInfoStrings)
4551
{
46-
var parts = sortInfoString.Split(new[] { ':', '-' }, StringSplitOptions.RemoveEmptyEntries);
52+
var parts = sortInfoString.Split(_directionSeparators, StringSplitOptions.RemoveEmptyEntries);
4753
if (parts.Any())
4854
{
4955
var sortInfo = new SortInfo
@@ -53,7 +59,7 @@ public static IEnumerable<SortInfo> Parse(string sortExpr)
5359
};
5460
if (parts.Length > 1)
5561
{
56-
sortInfo.SortDirection = parts[1].StartsWith("desc", StringComparison.InvariantCultureIgnoreCase) ? SortDirection.Descending : SortDirection.Ascending;
62+
sortInfo.SortDirection = parts[1].StartsWithIgnoreCase("desc") ? SortDirection.Descending : SortDirection.Ascending;
5763
}
5864
retVal.Add(sortInfo);
5965
}
@@ -64,7 +70,7 @@ public static IEnumerable<SortInfo> Parse(string sortExpr)
6470
public bool Equals(SortInfo other)
6571
{
6672
return other != null
67-
&& string.Equals(SortColumn, other.SortColumn, StringComparison.OrdinalIgnoreCase)
73+
&& SortColumn.EqualsIgnoreCase(other.SortColumn)
6874
&& SortDirection == other.SortDirection;
6975
}
7076

src/VirtoCommerce.Platform.Core/Domain/AbstractTypeFactory.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,10 @@ public static BaseType TryCreateInstance(string typeName, params object[] args)
254254
/// <returns>The TypeInfo instance for the specified type name.</returns>
255255
public static TypeInfo<BaseType> FindTypeInfoByName(string typeName)
256256
{
257-
//Try find first direct type match from registered types
258-
var result = _typeInfos.FirstOrDefault(x => x.TypeName.EqualsInvariant(typeName));
257+
//Try to find first direct type match from registered types
259258
//Then need to find in inheritance chain from registered types
260-
if (result == null)
261-
{
262-
result = _typeInfos.FirstOrDefault(x => x.IsAssignableTo(typeName));
263-
}
259+
var result = _typeInfos.FirstOrDefault(x => x.TypeName.EqualsIgnoreCase(typeName)) ??
260+
_typeInfos.FirstOrDefault(x => x.IsAssignableTo(typeName));
264261
return result;
265262
}
266263
}
@@ -378,7 +375,7 @@ public TypeInfo<BaseType> WithTypeName(string name)
378375
/// <returns>true if the associated type is assignable to the specified type name; otherwise, false.</returns>
379376
public bool IsAssignableTo(string typeName)
380377
{
381-
return Type.GetTypeInheritanceChainTo(typeof(BaseType)).Concat(new[] { typeof(BaseType) }).Any(t => typeName.EqualsInvariant(t.Name));
378+
return Type.GetTypeInheritanceChainTo(typeof(BaseType)).Concat([typeof(BaseType)]).Any(t => typeName.EqualsIgnoreCase(t.Name));
382379
}
383380

384381
/// <summary>

src/VirtoCommerce.Platform.Core/Extensions/ModuleCatalogExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static bool IsModuleInstalled(this IModuleCatalog moduleCatalog, string m
1010
{
1111
return moduleCatalog.Modules
1212
.OfType<ManifestModuleInfo>()
13-
.Any(x => x.Id.EqualsInvariant(moduleId) && x.IsInstalled);
13+
.Any(x => x.Id.EqualsIgnoreCase(moduleId) && x.IsInstalled);
1414
}
1515
}
1616
}

0 commit comments

Comments
 (0)