From f85038030153bc18a36f68c1a652e54e3fdfb6ab Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 29 Dec 2023 09:22:36 -0800 Subject: [PATCH] Simplify and Cleanup --- README.md | 22 ----------------- .../Indexes/DescribeContext.cs | 19 +++++++-------- src/YesSql.Core/Commands/IndexCommand.cs | 24 +++++++------------ test/YesSql.Tests/Indexes/PropertyIndex.cs | 3 +-- 4 files changed, 19 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 3dc04d91..4d4868e4 100644 --- a/README.md +++ b/README.md @@ -7,28 +7,6 @@ A .NET document database interface for relational databases, because in SQL we ( [![NuGet](https://img.shields.io/nuget/v/YesSql.svg)](https://www.nuget.org/packages/YesSql) [![MyGet](https://img.shields.io/myget/yessql/vpre/yessql.svg?label=MyGet)](https://www.myget.org/feed/yessql/package/nuget/yessql) -Dynamic index type support -------------------- - -```c# - public class PropertyDynamicIndexProvider : IndexProvider - { - public override void Describe(DescribeContext context) - { - var propertyType = typeof(PropertyIndex); - context - .For(propertyType) //Specify a type directly - .Map(property => new PropertyIndex - { - Name = property.Name, - ForRent = property.ForRent, - IsOccupied = property.IsOccupied, - Location = property.Location - }); - } - } -``` - How does it work ? ------------------- diff --git a/src/YesSql.Abstractions/Indexes/DescribeContext.cs b/src/YesSql.Abstractions/Indexes/DescribeContext.cs index c38531f9..4d05b406 100644 --- a/src/YesSql.Abstractions/Indexes/DescribeContext.cs +++ b/src/YesSql.Abstractions/Indexes/DescribeContext.cs @@ -6,7 +6,7 @@ namespace YesSql.Indexes { public class DescribeContext : IDescriptor { - private readonly Dictionary> _describes = new Dictionary>(); + private readonly Dictionary> _describes = []; public IEnumerable Describe(params Type[] types) { @@ -32,30 +32,29 @@ public IMapFor For(Type indexType) public IMapFor For() where TIndex : IIndex { - return For(); + return For(typeof(TIndex)); } public IMapFor For() where TIndex : IIndex { - return For(null); + return For(typeof(TIndex)); } public IMapFor For(Type indexType) where TIndex : IIndex { + ArgumentNullException.ThrowIfNull(indexType, nameof(indexType)); + List descriptors; if (!_describes.TryGetValue(typeof(T), out descriptors)) { - descriptors = _describes[typeof(T)] = new List(); + descriptors = _describes[typeof(T)] = []; } - var describeFor = new IndexDescriptor(); - - // if indexType is null , use default value :typeof(TIndex) - if (indexType != null) + var describeFor = new IndexDescriptor() { - describeFor.IndexType = indexType; - } + IndexType = indexType, + }; descriptors.Add(describeFor); diff --git a/src/YesSql.Core/Commands/IndexCommand.cs b/src/YesSql.Core/Commands/IndexCommand.cs index 1dc700e1..d8889439 100644 --- a/src/YesSql.Core/Commands/IndexCommand.cs +++ b/src/YesSql.Core/Commands/IndexCommand.cs @@ -16,15 +16,16 @@ namespace YesSql.Commands public abstract class IndexCommand : IIndexCommand { protected const string ParameterSuffix = "_$$$"; + private const string _separator = ", "; protected readonly IStore _store; private static readonly ConcurrentDictionary PropertyAccessors = new(); - private static readonly ConcurrentDictionary TypeProperties = new(); + private static readonly ConcurrentDictionary TypeProperties = new(); private static readonly ConcurrentDictionary InsertsList = new(); private static readonly ConcurrentDictionary UpdatesList = new(); - protected static PropertyInfo[] KeysProperties = new[] { typeof(IIndex).GetProperty("Id") }; + protected static PropertyInfo[] KeysProperties = [typeof(IIndex).GetProperty("Id")]; public abstract int ExecutionOrder { get; } @@ -67,20 +68,13 @@ protected static void GetProperties(DbCommand command, object item, string suffi protected static PropertyInfo[] TypePropertiesCache(Type type) { - if (TypeProperties.TryGetValue(type, out var pis)) + if (TypeProperties.TryGetValue(type.FullName, out var pis)) { return pis; } - // Clean up cache entries with the same name and different type - var exists = TypeProperties.Keys.FirstOrDefault(x => x.FullName == type.FullName); - if (exists != null) - { - TypeProperties.Remove(exists, out _); - } - var properties = type.GetProperties().Where(IsWriteable).ToArray(); - TypeProperties[type] = properties; + TypeProperties[type.FullName] = properties; return properties; } @@ -109,7 +103,7 @@ protected string Inserts(Type type, ISqlDialect dialect) sbColumnList.Append(dialect.QuoteForColumnName(property.Name)); if (i < allProperties.Length - 1) { - sbColumnList.Append(", "); + sbColumnList.Append(_separator); } } @@ -120,14 +114,14 @@ protected string Inserts(Type type, ISqlDialect dialect) sbParameterList.Append("@").Append(property.Name).Append(ParameterSuffix); if (i < allProperties.Length - 1) { - sbParameterList.Append(", "); + sbParameterList.Append(_separator); } } if (typeof(MapIndex).IsAssignableFrom(type)) { // We can set the document id - sbColumnList.Append(", ").Append(dialect.QuoteForColumnName("DocumentId")); + sbColumnList.Append(_separator).Append(dialect.QuoteForColumnName("DocumentId")); sbParameterList.Append(", @DocumentId").Append(ParameterSuffix); } @@ -171,7 +165,7 @@ protected string Updates(Type type, ISqlDialect dialect) values.Append(dialect.QuoteForColumnName(property.Name) + " = @" + property.Name + ParameterSuffix); if (i < allProperties.Length - 1) { - values.Append(", "); + values.Append(_separator); } } diff --git a/test/YesSql.Tests/Indexes/PropertyIndex.cs b/test/YesSql.Tests/Indexes/PropertyIndex.cs index b0e0e566..9c06e3d3 100644 --- a/test/YesSql.Tests/Indexes/PropertyIndex.cs +++ b/test/YesSql.Tests/Indexes/PropertyIndex.cs @@ -31,9 +31,8 @@ public class PropertyDynamicIndexProvider : IndexProvider { public override void Describe(DescribeContext context) { - var propertyType = typeof(PropertyIndex); context - .For(propertyType) + .For(typeof(PropertyIndex)) .Map(property => new PropertyIndex { Name = property.Name,