Skip to content

Commit

Permalink
IntoItIf.MongoDb.AspNetCore
Browse files Browse the repository at this point in the history
  • Loading branch information
Anggara Suwartana committed Dec 13, 2018
1 parent 8d83195 commit d03922b
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ $revision = "{0:D4}" -f [convert]::ToInt32($revision, 10)
exec { & dotnet pack .\src\IntoItIf.Base -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
exec { & dotnet pack .\src\IntoItIf.Base.AutoMapper -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
exec { & dotnet pack .\src\IntoItIf.Ef -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
exec { & dotnet pack .\src\IntoItIf.MongoDb -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
exec { & dotnet pack .\src\IntoItIf.MongoDb -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
exec { & dotnet pack .\src\IntoItIf.MongoDb.AspNetCore -c Release -o ..\..\artifacts -p:PackageVersion=0.0.$revision }
15 changes: 15 additions & 0 deletions IntoItIf.MongoDb.AspNetCore/IntoItIf.MongoDb.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\IntoItIf.MongoDb\IntoItIf.MongoDb.csproj" />
</ItemGroup>

</Project>
104 changes: 104 additions & 0 deletions IntoItIf.MongoDb.AspNetCore/MongoDbConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
namespace IntoItIf.MongoDb.AspNetCore
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using MongoDb;
using MongoDB.Bson;
using MongoDB.Driver;

internal class MongoDbConfigurationProvider<T> : ConfigurationProvider
where T : class, IMongoEntity
{
private readonly IMongoUow _mongoUow;
private readonly Func<T, string> _keySelector;
private readonly Func<T, string> _elementSelector;
private readonly Action<IMongoRepository<T>> _seedAction;
private bool _firstLoad;

internal MongoDbConfigurationProvider(
IMongoUow mongoUow,
Func<T, string> keySelector,
Func<T, string> elementSelector,
Action<IMongoRepository<T>> seedAction = null)
{
_mongoUow = mongoUow;
_keySelector = keySelector;
_elementSelector = elementSelector;
_seedAction = seedAction;
_mongoUow.RegisterChangesWatchAsync<T>(
x =>
{
if (!_firstLoad && x != null) OnReload();
},
ChangeStreamOperationType.Insert);
_mongoUow.RegisterChangesWatchAsync<T>(
x =>
{
if (!_firstLoad && x != null) OnReload();
},
ChangeStreamOperationType.Update);
_mongoUow.RegisterChangesWatchAsync<T>(
x =>
{
if (!_firstLoad && x != null) OnReload();
},
ChangeStreamOperationType.Replace);
_mongoUow.RegisterChangesWatchAsync<T>(
x =>
{
if (!_firstLoad && x != null) OnReload();
},
ChangeStreamOperationType.Delete);
ChangeToken.OnChange(
GetReloadToken,
() =>
{
Thread.Sleep(250);
Load(false);
});
}


// Load config data from EF DB.
public override void Load()
{
Load(true);
}

private void Load(bool firstLoad)
{
_firstLoad = firstLoad;
var collection = _mongoUow.SetOf<T>();
if (_firstLoad)
{
var isAny = collection.LongCount() > 0;
if (!isAny) _seedAction?.Invoke(collection);
_firstLoad = false;
}
var data = collection
.GetList(x => x, x => x.Id != default(ObjectId))
.ToDictionary(x => _keySelector(x), x => _elementSelector(x));
if (!IsDictionaryEqual(Data, data)) Data = data;
}

private static bool IsDictionaryEqual<TKey, TValue>(IDictionary<TKey, TValue> x, IDictionary<TKey, TValue> y)
{
// early-exit checks
if (null == y)
return null == x;
if (null == x)
return false;
if (ReferenceEquals(x, y))
return true;
if (x.Count != y.Count)
return false;

// check keys are the same
return x.Keys.All(y.ContainsKey) && x.Keys.All(k => x[k].Equals(y[k]));
}
}
}
32 changes: 32 additions & 0 deletions IntoItIf.MongoDb.AspNetCore/MongoDbConfigurationSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace IntoItIf.MongoDb.AspNetCore
{
using System;
using Microsoft.Extensions.Configuration;
using MongoDb;

internal class MongoDbConfigurationSource<T> : IConfigurationSource
where T : class, IMongoEntity
{
private readonly IMongoUow _mongoUow;
private readonly Func<T, string> _keySelector;
private readonly Func<T, string> _elementSelector;
private readonly Action<IMongoRepository<T>> _seedAction;

internal MongoDbConfigurationSource(
IMongoUow mongoUow,
Func<T, string> keySelector,
Func<T, string> elementSelector,
Action<IMongoRepository<T>> seedAction = null)
{
_mongoUow = mongoUow;
_keySelector = keySelector;
_elementSelector = elementSelector;
_seedAction = seedAction;
}

public IConfigurationProvider Build(IConfigurationBuilder builder)
{
return new MongoDbConfigurationProvider<T>(_mongoUow, _keySelector, _elementSelector, _seedAction);
}
}
}
20 changes: 20 additions & 0 deletions IntoItIf.MongoDb.AspNetCore/MongoDbExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace IntoItIf.MongoDb.AspNetCore
{
using System;
using Microsoft.Extensions.Configuration;
using MongoDb;

internal static class MongoDbExtensions
{
internal static IConfigurationBuilder AddMongoDbConfiguration<T>(
this IConfigurationBuilder builder,
IMongoUow mongoUow,
Func<T, string> keySelector,
Func<T, string> elementSelector,
Action<IMongoRepository<T>> seedAction = null)
where T : class, IMongoEntity
{
return builder.Add(new MongoDbConfigurationSource<T>(mongoUow, keySelector, elementSelector, seedAction));
}
}
}
7 changes: 7 additions & 0 deletions IntoItIf.sln
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntoItIf.Base.AutoMapper",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IntoItIf.MongoDb", "src\IntoItIf.MongoDb\IntoItIf.MongoDb.csproj", "{FE9D066C-5E97-4E5D-981B-8A36667686EF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntoItIf.MongoDb.AspNetCore", "IntoItIf.MongoDb.AspNetCore\IntoItIf.MongoDb.AspNetCore.csproj", "{F9B95656-20AC-4D95-A0E9-C025381E21C4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -57,6 +59,10 @@ Global
{FE9D066C-5E97-4E5D-981B-8A36667686EF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE9D066C-5E97-4E5D-981B-8A36667686EF}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{FE9D066C-5E97-4E5D-981B-8A36667686EF}.Release|Any CPU.Build.0 = Debug|Any CPU
{F9B95656-20AC-4D95-A0E9-C025381E21C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9B95656-20AC-4D95-A0E9-C025381E21C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9B95656-20AC-4D95-A0E9-C025381E21C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9B95656-20AC-4D95-A0E9-C025381E21C4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -67,6 +73,7 @@ Global
{D6A90CEA-B137-408A-94DB-6436E4F2CE5D} = {A50C36F9-B54A-4E48-8923-12E6A9197D01}
{90F9A347-B4C5-4ACC-8888-691BC46CDAD4} = {0B3CFC2C-0607-40CA-A2B1-1C473EA60E82}
{FE9D066C-5E97-4E5D-981B-8A36667686EF} = {0B3CFC2C-0607-40CA-A2B1-1C473EA60E82}
{F9B95656-20AC-4D95-A0E9-C025381E21C4} = {0B3CFC2C-0607-40CA-A2B1-1C473EA60E82}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {B14342F1-6A48-42E7-8548-0A11E364A7D8}
Expand Down
2 changes: 1 addition & 1 deletion src/IntoItIf.Base/IntoItIf.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<PackageReference Include="Humanizer.Core" Version="2.5.16" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.9" />
<PackageReference Include="FluentValidation" Version="8.0.101" />
<PackageReference Include="FluentValidation" Version="8.1.1" />
<PackageReference Include="Valit" Version="2.0.0" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion src/IntoItIf.MongoDb/IMongoUow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Base.Domain.Options;
using Base.UnitOfWork;
using MongoDB.Driver;

Expand Down

0 comments on commit d03922b

Please sign in to comment.