Skip to content

Commit

Permalink
Merging 0.18.3
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Dec 24, 2019
2 parents 91aee39 + 6f4bb3e commit 1eeaa91
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 20 deletions.
14 changes: 7 additions & 7 deletions src/MongoDM/MongoDM.csproj
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>Digicando.MongoDM</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.18.2</Version>
<Version>0.18.3</Version>
<Authors>Digicando Srl</Authors>
<Description>ODM framework for MongoDB</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DomainHelper" Version="0.18.0-dev" />
<PackageReference Include="Hangfire.Mongo" Version="0.6.3" />
<PackageReference Include="DomainHelper" Version="0.18.1" />
<PackageReference Include="Hangfire.Mongo" Version="0.6.6" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.9.1" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.9.1" />
<PackageReference Include="morelinq" Version="3.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.10.0" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.10.0" />
<PackageReference Include="morelinq" Version="3.3.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/MongoDM/Serialization/DocumentSchemaMemberMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IEnumerable<EntityMember> MemberPathToId

// Methods.
public string MemberPathToString() =>
string.Join('.', MemberPath.Select(member => member.MemberMap.MemberInfo.Name));
string.Join(".", MemberPath.Select(member => member.MemberMap.MemberInfo.Name));

public string FullPathToString() => $"{RootModelType.Name}.{MemberPathToString()}";

Expand Down
2 changes: 2 additions & 0 deletions src/MongoDM/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static void AddMongoDM<TDBContext, TDBContexImpl, TProxyGenerator>(this I
services.AddSingleton<IDBMaintainer, DBMaintainer>();
services.AddSingleton<IDocumentSchemaRegister, DocumentSchemaRegister>();
services.AddSingleton<IHangfireContextAccessor, HangfireContextAccessor>();
services.AddSingleton<ILocalContextAccessor, LocalContextAccessor>();
services.AddSingleton<ILocalContextFactory, LocalContextFactory>();
services.AddSingleton<ISerializerModifierAccessor, SerializerModifierAccessor>();

// Proxy generator.
Expand Down
25 changes: 17 additions & 8 deletions src/MongoDM/Utility/ContextAccessorFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class ContextAccessorFacade : IContextAccessorFacade
{
// Fields.
private readonly IHttpContextAccessor httpContextAccessor;
private readonly ILocalContextAccessor localContextAccessor;
private readonly IHangfireContextAccessor performContextAccessor;

// Constructors.
public ContextAccessorFacade(
IHttpContextAccessor httpContextAccessor,
ILocalContextAccessor localContextAccessor,
IHangfireContextAccessor performContextAccessor)
{
this.httpContextAccessor = httpContextAccessor; // Provided by Asp.Net, available only on client call.
this.localContextAccessor = localContextAccessor; // Optional context created by user on local async method stack.
this.performContextAccessor = performContextAccessor; // Provided by HangFire, available only during task execution.
}

Expand All @@ -25,32 +28,36 @@ public IReadOnlyDictionary<object, object> Items
{
get
{
if (localContextAccessor.Context != null)
return localContextAccessor.Context.Items.ToDictionary(pair => pair.Key as object, pair => pair.Value);
if (httpContextAccessor.HttpContext != null)
return httpContextAccessor.HttpContext.Items.ToDictionary(pair => pair.Key, pair => pair.Value);
else if (performContextAccessor.PerformContext != null)
if (performContextAccessor.PerformContext != null)
return performContextAccessor.PerformContext.Items.ToDictionary(pair => pair.Key as object, pair => pair.Value);
else
throw new InvalidOperationException();
throw new InvalidOperationException();
}
}

public object SyncRoot
{
get
{
if (localContextAccessor.Context != null)
return localContextAccessor.Context.Items;
if (httpContextAccessor.HttpContext != null)
return httpContextAccessor.HttpContext.Items;
else if (performContextAccessor.PerformContext != null)
if (performContextAccessor.PerformContext != null)
return performContextAccessor.PerformContext.Items;
else
throw new InvalidOperationException();
throw new InvalidOperationException();
}
}

// Methods.
public void AddItem(string key, object value)
{
if (httpContextAccessor.HttpContext != null)
if (localContextAccessor.Context != null)
localContextAccessor.Context.Items.Add(key, value);
else if (httpContextAccessor.HttpContext != null)
httpContextAccessor.HttpContext.Items.Add(key, value);
else if (performContextAccessor.PerformContext != null)
performContextAccessor.PerformContext.Items.Add(key, value);
Expand All @@ -60,7 +67,9 @@ public void AddItem(string key, object value)

public bool RemoveItem(string key)
{
if (httpContextAccessor.HttpContext != null)
if (localContextAccessor.Context != null)
return localContextAccessor.Context.Items.Remove(key);
else if (httpContextAccessor.HttpContext != null)
return httpContextAccessor.HttpContext.Items.Remove(key);
else if (performContextAccessor.PerformContext != null)
return performContextAccessor.PerformContext.Items.Remove(key);
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDM/Utility/DBMaintainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void OnUpdatedModel<TKey>(IAuditable updatedModel, TKey modelId)
foreach (var dependencyGroup in dependencies.GroupBy(d => d.RootModelType))
{
var idPaths = dependencyGroup
.Select(dependency => string.Join('.', dependency.MemberPathToId.Select(member => member.MemberMap.MemberInfo.Name)))
.Select(dependency => string.Join(".", dependency.MemberPathToId.Select(member => member.MemberMap.MemberInfo.Name)))
.Distinct();

// Enqueue call for background job.
Expand Down
11 changes: 11 additions & 0 deletions src/MongoDM/Utility/ILocalContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Digicando.MongoDM.Utility
{
internal interface ILocalContextAccessor
{
LocalContext Context { get; }

void OnCreated(LocalContext context);

void OnDisposed(LocalContext context);
}
}
9 changes: 9 additions & 0 deletions src/MongoDM/Utility/ILocalContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Digicando.MongoDM.Utility
{
public interface ILocalContextFactory
{
IDisposable CreateNewLocalContext();
}
}
24 changes: 24 additions & 0 deletions src/MongoDM/Utility/LocalContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;

namespace Digicando.MongoDM.Utility
{
public class LocalContext : IDisposable
{
// Fields.
private readonly ILocalContextAccessor localContextAccessor;

// Constructors.
internal LocalContext(ILocalContextAccessor localContextAccessor)
{
this.localContextAccessor = localContextAccessor;
localContextAccessor.OnCreated(this);
}

// Properties.
public IDictionary<string, object> Items { get; } = new Dictionary<string, object>();

public void Dispose() =>
localContextAccessor.OnDisposed(this);
}
}
20 changes: 20 additions & 0 deletions src/MongoDM/Utility/LocalContextAccessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Threading;

namespace Digicando.MongoDM.Utility
{
class LocalContextAccessor : ILocalContextAccessor
{
// Fields.
private static readonly AsyncLocal<LocalContext> localContextCurrent = new AsyncLocal<LocalContext>();

// Properties.
public LocalContext Context => localContextCurrent.Value;

// Methods.
public void OnCreated(LocalContext context) =>
localContextCurrent.Value = context;

public void OnDisposed(LocalContext context) =>
localContextCurrent.Value = null;
}
}
18 changes: 18 additions & 0 deletions src/MongoDM/Utility/LocalContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Digicando.MongoDM.Utility
{
class LocalContextFactory : ILocalContextFactory
{
private readonly ILocalContextAccessor localContextAccessor;

public LocalContextFactory(
ILocalContextAccessor localContextAccessor)
{
this.localContextAccessor = localContextAccessor;
}

public IDisposable CreateNewLocalContext() =>
new LocalContext(localContextAccessor);
}
}
6 changes: 3 additions & 3 deletions test/MongoDM.Tests/MongoDM.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<RootNamespace>Digicando.MongoDM</RootNamespace>
Expand All @@ -12,12 +12,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

Expand Down

0 comments on commit 1eeaa91

Please sign in to comment.