Skip to content

Commit

Permalink
Merge pull request #23 from Etherna/improve/MODM-78-analyzer-errors
Browse files Browse the repository at this point in the history
Fixed analyzer errors and messages
  • Loading branch information
tmm360 authored May 19, 2021
2 parents 38ea2c8 + 679a868 commit b24edd6
Show file tree
Hide file tree
Showing 28 changed files with 131 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ indent_size = 2
# C# files
[*.cs]

# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = none

# CA1040: Avoid empty interfaces
dotnet_diagnostic.CA1040.severity = none # Sometime they are needed

# CA1812: Avoid uninstantiated internal classes
dotnet_diagnostic.CA1812.severity = none # Doing extensive use of dependency injection
2 changes: 1 addition & 1 deletion src/ExecutionContext/AsyncLocal/AsyncLocalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace Etherna.ExecContext.AsyncLocal
public class AsyncLocalContext : IAsyncLocalContext, IHandledAsyncLocalContext
{
// Fields.
private static readonly AsyncLocal<IDictionary<object, object?>?> asyncLocalContext = new AsyncLocal<IDictionary<object, object?>?>();
private static readonly AsyncLocal<IDictionary<object, object?>?> asyncLocalContext = new();

// Properties.
public IDictionary<object, object?>? Items => asyncLocalContext.Value;
Expand Down
2 changes: 2 additions & 0 deletions src/ExecutionContext/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Runtime.CompilerServices;

[assembly: CLSCompliant(false)]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
[assembly: InternalsVisibleTo("ExecutionContext.Tests")]
3 changes: 3 additions & 0 deletions src/MongODM.AspNetCore.UI/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly: CLSCompliant(false)]
2 changes: 1 addition & 1 deletion src/MongODM.Core/Domain/Models/DbMigrationOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum Status
}

// Fields.
private List<MigrationLogBase> _logs = new List<MigrationLogBase>();
private List<MigrationLogBase> _logs = new();

// Constructors.
public DbMigrationOperation(IDbContext dbContext)
Expand Down
2 changes: 2 additions & 0 deletions src/MongODM.Core/Domain/Models/ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
// limitations under the License.

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Etherna.MongODM.Core.Domain.Models
{
public abstract class ModelBase : IModel
{
[SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "This is needed by MongoDB drivers")]
public virtual IDictionary<string, object>? ExtraElements { get; protected set; }
}
}
4 changes: 2 additions & 2 deletions src/MongODM.Core/Migration/MigrationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ private MigrationResult() { }

// Methods.
public static MigrationResult Failed() =>
new MigrationResult
new()
{
Succeded = false
};

public static MigrationResult Succeeded(long migratedDocuments) =>
new MigrationResult
new()
{
Succeded = true,
MigratedDocuments = migratedDocuments
Expand Down
20 changes: 16 additions & 4 deletions src/MongODM.Core/MongODMConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,28 @@ namespace Etherna.MongODM.Core
public abstract class MongODMConfiguration : IMongODMConfiguration, IDisposable
{
// Fields.
private readonly ReaderWriterLockSlim configLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly List<Type> _dbContextTypes = new List<Type>();
private readonly ReaderWriterLockSlim configLock = new(LockRecursionPolicy.SupportsRecursion);
private bool disposed;
private readonly List<Type> _dbContextTypes = new();

// Constructor and dispose.
// Dispose.
public void Dispose()
{
configLock.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposed) return;

// Dispose managed resources.
if (disposing)
configLock.Dispose();

disposed = true;
}

// Properties.
public IEnumerable<Type> DbContextTypes
{
Expand Down
3 changes: 3 additions & 0 deletions src/MongODM.Core/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly: CLSCompliant(false)]
2 changes: 1 addition & 1 deletion src/MongODM.Core/ProxyModels/AuditableInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class AuditableInterceptor<TModel> : ModelInterceptorBase<TModel>
{
// Fields.
private bool isAuditingEnabled;
private readonly HashSet<MemberInfo> changedMembers = new HashSet<MemberInfo>();
private readonly HashSet<MemberInfo> changedMembers = new();

// Constructors.
public AuditableInterceptor(IEnumerable<Type> additionalInterfaces)
Expand Down
38 changes: 26 additions & 12 deletions src/MongODM.Core/ProxyModels/ProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ namespace Etherna.MongODM.Core.ProxyModels
public class ProxyGenerator : IProxyGenerator, IDisposable
{
// Fields.
private bool disposed;
private readonly Castle.DynamicProxy.IProxyGenerator proxyGeneratorCore;

private readonly Dictionary<Type,
(Type[] AdditionalInterfaces, Func<IDbContext, IInterceptor[]> InterceptorInstancerSelector)> modelConfigurationDictionary =
new Dictionary<Type, (Type[] AdditionalInterfaces, Func<IDbContext, IInterceptor[]> InterceptorInstancerSelector)>();
private readonly ReaderWriterLockSlim modelConfigurationDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
(Type[] AdditionalInterfaces, Func<IDbContext, IInterceptor[]> InterceptorInstancerSelector)> modelConfigurationDictionary = new();
private readonly ReaderWriterLockSlim modelConfigurationDictionaryLock = new(LockRecursionPolicy.SupportsRecursion);

private readonly Dictionary<Type, Type> proxyTypeDictionary = new Dictionary<Type, Type>();
private readonly ReaderWriterLockSlim proxyTypeDictionaryLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly Dictionary<Type, Type> proxyTypeDictionary = new();
private readonly ReaderWriterLockSlim proxyTypeDictionaryLock = new(LockRecursionPolicy.SupportsRecursion);

// Constructors.
public ProxyGenerator(
Expand All @@ -41,6 +41,27 @@ public ProxyGenerator(
this.proxyGeneratorCore = proxyGeneratorCore;
}

// Dispose.
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposed) return;

// Dispose managed resources.
if (disposing)
{
modelConfigurationDictionaryLock.Dispose();
proxyTypeDictionaryLock.Dispose();
}

disposed = true;
}

// Methods.
public object CreateInstance(
Type type,
Expand Down Expand Up @@ -133,13 +154,6 @@ public object CreateInstance(
public TModel CreateInstance<TModel>(IDbContext dbContext, params object[] constructorArguments) =>
(TModel)CreateInstance(typeof(TModel), dbContext, constructorArguments);

public void Dispose()
{
modelConfigurationDictionaryLock.Dispose();
proxyTypeDictionaryLock.Dispose();
GC.SuppressFinalize(this);
}

public bool IsProxyType(Type type)
{
proxyTypeDictionaryLock.EnterReadLock();
Expand Down
2 changes: 1 addition & 1 deletion src/MongODM.Core/ProxyModels/ReferenceableInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ReferenceableInterceptor<TModel, TKey> : ModelInterceptorBase<TMode
{
// Fields.
private bool isSummary;
private readonly Dictionary<string, bool> settedMemberNames = new Dictionary<string, bool>(); //<memberName, isFromSummary>
private readonly Dictionary<string, bool> settedMemberNames = new(); //<memberName, isFromSummary>
private readonly IRepository repository;

// Constructors.
Expand Down
4 changes: 2 additions & 2 deletions src/MongODM.Core/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace Etherna.MongODM.Core
{
public static class ReflectionHelper
{
private static readonly Dictionary<Type, IEnumerable<PropertyInfo>> propertyRegister = new Dictionary<Type, IEnumerable<PropertyInfo>>();
private static readonly ReaderWriterLockSlim propertyRegisterLock = new ReaderWriterLockSlim();
private static readonly Dictionary<Type, IEnumerable<PropertyInfo>> propertyRegister = new();
private static readonly ReaderWriterLockSlim propertyRegisterLock = new();

public static MemberInfo FindProperty(LambdaExpression lambdaExpression)
{
Expand Down
2 changes: 1 addition & 1 deletion src/MongODM.Core/Serialization/Mapping/MemberDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public string MemberPathToString() =>

public override string ToString()
{
StringBuilder strBuilder = new StringBuilder();
StringBuilder strBuilder = new();

strBuilder.AppendLine(FullPathToString());
strBuilder.AppendLine($" modelMapId: {RootModelMap.Id}");
Expand Down
8 changes: 6 additions & 2 deletions src/MongODM.Core/Serialization/Mapping/SchemaRegister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using MongoDB.Bson.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -66,6 +67,7 @@ public ICustomSerializerSchemaBuilder<TModel> AddCustomSerializerSchema<TModel>(
return modelSchemaConfiguration;
});

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")]
public IModelMapsSchemaBuilder<TModel> AddModelMapsSchema<TModel>(
string activeModelMapId,
Action<BsonClassMap<TModel>>? activeModelMapInitializer = null,
Expand Down Expand Up @@ -158,7 +160,7 @@ public IModelMapsSchema GetModelMapsSchema(Type modelType)

public override string ToString()
{
StringBuilder strBuilder = new StringBuilder();
StringBuilder strBuilder = new();

// Member dependencies.
//memberInfoToMemberMapsDictionary
Expand Down Expand Up @@ -308,7 +310,9 @@ private void CompileDependencyRegisters(
//model maps schema serializers
if (memberSerializer is IModelMapsContainerSerializer schemaSerializer)
{
var useCascadeDelete = (memberSerializer as IReferenceContainerSerializer)?.UseCascadeDelete;
#pragma warning disable CA1508 // Avoid dead conditional code. Here code analyzer is wrong
bool? useCascadeDelete = (memberSerializer as IReferenceContainerSerializer)?.UseCascadeDelete;
#pragma warning restore CA1508 // Avoid dead conditional code
foreach (var childClassMap in schemaSerializer.AllChildClassMaps)
CompileDependencyRegisters(modelMap, childClassMap, lastEntityClassMap, currentMemberPath, useCascadeDelete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using MongoDB.Bson.Serialization;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Etherna.MongODM.Core.Serialization.Mapping.Schemas
{
Expand All @@ -34,6 +35,7 @@ public IModelMapsSchemaBuilder<TModel> AddFallbackCustomSerializer(IBsonSerializ
return this;
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")]
public IModelMapsSchemaBuilder<TModel> AddSecondaryMap(
string id,
Action<BsonClassMap<TModel>>? modelMapInitializer = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ abstract class ModelMapsSchemaBase : SchemaBase, IModelMapsSchema
{
// Fields.
private Dictionary<string, IModelMap> _allMapsDictionary = default!;
protected readonly List<IModelMap> _secondaryMaps = new List<IModelMap>();
protected readonly List<IModelMap> _secondaryMaps = new();

// Constructor.
protected ModelMapsSchemaBase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

using MongoDB.Bson.Serialization;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Etherna.MongODM.Core.Serialization.Mapping.Schemas
{
Expand All @@ -33,6 +34,7 @@ public IReferenceModelMapsSchemaBuilder<TModel> AddFallbackCustomSerializer(IBso
return this;
}

[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")]
public IReferenceModelMapsSchemaBuilder<TModel> AddSecondaryMap(
string id,
string? baseModelMapId = null,
Expand Down
4 changes: 3 additions & 1 deletion src/MongODM.Core/Serialization/SemanticVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public override string ToString()

public static bool operator >=(SemanticVersion x, SemanticVersion y) => y <= x;

public static implicit operator SemanticVersion(string version) => new SemanticVersion(version);
public static implicit operator SemanticVersion(string version) => new(version);

public static SemanticVersion FromString(string version) => new(version);
}
}
25 changes: 21 additions & 4 deletions src/MongODM.Core/Serialization/Serializers/ReferenceSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ public class ReferenceSerializer<TModelBase, TKey> :
// Fields.
private IDiscriminatorConvention _discriminatorConvention = default!;

private readonly ReaderWriterLockSlim configLockAdapters = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
private readonly ReaderWriterLockSlim configLockAdapters = new(LockRecursionPolicy.SupportsRecursion);
private readonly IDbContext dbContext;
private bool disposed;
private readonly ReferenceSerializerConfiguration configuration;
private readonly IDictionary<Type, IBsonSerializer> registeredAdapters = new Dictionary<Type, IBsonSerializer>();

// Constructor and dispose.
// Constructor.
public ReferenceSerializer(
IDbContext dbContext,
Action<ReferenceSerializerConfiguration> configure)
Expand All @@ -58,13 +59,27 @@ public ReferenceSerializer(
configuration.Freeze();
}

// Dispose.
public void Dispose()
{
configLockAdapters.Dispose();
configuration.Dispose();
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposed) return;

// Dispose managed resources.
if (disposing)
{
configLockAdapters.Dispose();
configuration.Dispose();
}

disposed = true;
}

// Properties.
public IEnumerable<BsonClassMap> AllChildClassMaps => configuration.Schemas.Values
.SelectMany(schema => schema.AllMapsDictionary.Values
Expand Down Expand Up @@ -138,8 +153,10 @@ public override TModelBase Deserialize(BsonDeserializationContext context, BsonD
if (model != null)
{
var id = model.Id;
#pragma warning disable CA1508 // Avoid dead conditional code
if (id == null) //ignore refered instances without id
return null!;
#pragma warning restore CA1508 // Avoid dead conditional code

// Check if model as been loaded in cache.
if (dbContext.DbCache.LoadedModels.ContainsKey(id) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using MongoDB.Bson.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Etherna.MongODM.Core.Serialization.Serializers
Expand Down Expand Up @@ -48,6 +49,7 @@ public bool UseCascadeDelete
}

// Methods.
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "The new model map instance can't be disposed")]
public IReferenceModelMapsSchemaBuilder<TModel> AddModelMapsSchema<TModel>(
string activeModelMapId,
Action<BsonClassMap<TModel>>? activeModelMapInitializer = null,
Expand Down
2 changes: 1 addition & 1 deletion src/MongODM.Core/Tasks/UpdateDocDependenciesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async Task RunAsync<TDbContext, TModel, TKey>(
var repository = (ICollectionRepository<TModel, TKey>)dbContext.RepositoryRegister.ModelCollectionRepositoryMap[typeof(TModel)];

// Update models.
HashSet<TKey> upgradedDocumentsId = new HashSet<TKey>();
HashSet<TKey> upgradedDocumentsId = new();
using (serializerModifierAccessor.EnableReferenceSerializerModifier(true))
using (serializerModifierAccessor.EnableCacheSerializerModifier(true))
{
Expand Down
2 changes: 2 additions & 0 deletions src/MongODM.Core/Utility/DbDependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public DbDependencies(
ISchemaRegister schemaRegister,
ISerializerModifierAccessor serializerModifierAccessor,
#pragma warning disable IDE0060 // Remove unused parameter. It's needed for run static configurations
#pragma warning disable CA1801 // Review unused parameters
IStaticConfigurationBuilder staticConfigurationBuilder)
#pragma warning restore CA1801 // Review unused parameters
#pragma warning restore IDE0060 // Remove unused parameter
{
DbCache = dbCache;
Expand Down
Loading

0 comments on commit b24edd6

Please sign in to comment.