-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anggara Suwartana
committed
Dec 13, 2018
1 parent
8d83195
commit d03922b
Showing
8 changed files
with
181 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
IntoItIf.MongoDb.AspNetCore/IntoItIf.MongoDb.AspNetCore.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
104
IntoItIf.MongoDb.AspNetCore/MongoDbConfigurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters