Skip to content

Added in-memory configuration for LiteDB. #143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions samples/Sample.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public static void Main(string[] args)
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//builder.Services.AddEntityFrameworkCore(true);
builder.Services.AddMongoDB(true);
//builder.Services.AddLiteDB(true);
//builder.Services.AddMongoDB(true);
builder.Services.AddLiteDB(true);
//builder.Services.AddInMemory(true);

//builder.Services.AddEntityFrameworkCore(false);
Expand Down
9 changes: 6 additions & 3 deletions src/Fluxera.Repository.LiteDB/DatabaseProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Fluxera.Repository.LiteDB
{
using System.Collections.Concurrent;
using System.IO;
using Fluxera.Utilities;
using Fluxera.Utilities.Extensions;
using global::LiteDB.Async;
Expand All @@ -22,13 +23,15 @@ public sealed class DatabaseProvider : Disposable
/// </summary>
/// <param name="repositoryName"></param>
/// <param name="databaseName"></param>
/// <param name="isPersistent"></param>
/// <returns></returns>
public LiteDatabaseAsync GetDatabase(RepositoryName repositoryName, string databaseName)
public LiteDatabaseAsync GetDatabase(RepositoryName repositoryName, string databaseName, bool isPersistent)
{
string key = $"{repositoryName}_{databaseName}";

LiteDatabaseAsync database = this.databases.GetOrAdd(key,
_ => new LiteDatabaseAsync(databaseName.EnsureEndsWith(".db")));
LiteDatabaseAsync database = this.databases.GetOrAdd(key, _ => isPersistent
? new LiteDatabaseAsync(databaseName.EnsureEndsWith(".db"))
: new LiteDatabaseAsync(new MemoryStream()));

return database;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fluxera.Repository.LiteDB/LiteContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ internal void Configure(RepositoryName repositoryName, IServiceProvider serviceP
Guard.Against.NullOrWhiteSpace(databaseName);

DatabaseProvider databaseProvider = serviceProvider.GetRequiredService<DatabaseProvider>();
this.database = databaseProvider.GetDatabase(repositoryName, databaseName);
this.database = databaseProvider.GetDatabase(repositoryName, databaseName, options.Persistent);

this.RepositoryName = repositoryName;
this.ServiceProvider = serviceProvider;
Expand Down
6 changes: 6 additions & 0 deletions src/Fluxera.Repository.LiteDB/LiteContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public sealed class LiteContextOptions
public LiteContextOptions(RepositoryName repositoryName)
{
this.RepositoryName = repositoryName;
this.Persistent = true;
}

/// <summary>
Expand All @@ -26,5 +27,10 @@ public LiteContextOptions(RepositoryName repositoryName)
/// Gets or sets the filename of the database to use.
/// </summary>
public string Database { get; set; }

/// <summary>
/// Flag, indicating if the database should be persisted to disk.
/// </summary>
public bool Persistent { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/AddTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class AddTests : AddTestBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public AddTests(bool isUnitOfWorkEnabled)
public AddTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/AggregateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class AggregateTests : AggregateTestBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public AggregateTests(bool isUnitOfWorkEnabled)
public AggregateTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class EnumerationTests : EnumerationTestsBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public EnumerationTests(bool isUnitOfWorkEnabled)
public EnumerationTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/FindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class FindTests : FindTestBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public FindTests(bool isUnitOfWorkEnabled)
public FindTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="NUnit" Version="4.2.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageReference Include="coverlet.collector" Version="6.0.2">
Expand Down
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/GetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class GetTests : GetTestBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public GetTests(bool isUnitOfWorkEnabled)
public GetTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/PagingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@
using global::LiteDB;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class PagingTests : PagingTestBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public PagingTests(bool isUnitOfWorkEnabled)
public PagingTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -34,7 +39,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
20 changes: 16 additions & 4 deletions tests/Fluxera.Repository.LiteDB.IntegrationTests/ReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@
using Fluxera.Repository.UnitTests.Core;
using NUnit.Framework;

[TestFixture(true)]
[TestFixture(false)]
[TestFixture(true, true)]
[TestFixture(true, false)]
[TestFixture(false, true)]
[TestFixture(false, false)]
public class ReferenceTests : ReferenceTestsBase
{
private readonly bool isPersistent;

/// <inheritdoc />
public ReferenceTests(bool isUnitOfWorkEnabled)
public ReferenceTests(bool isUnitOfWorkEnabled, bool isPersistent)
: base(isUnitOfWorkEnabled)
{
this.isPersistent = isPersistent;
}

/// <inheritdoc />
Expand All @@ -24,7 +29,14 @@ protected override void AddRepositoryUnderTest(IRepositoryBuilder repositoryBuil
File.Delete(file);
}

repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
if(this.isPersistent)
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContext>(repositoryName, configureOptions);
}
else
{
repositoryBuilder.AddLiteRepository<RepositoryLiteContextInMemory>(repositoryName, configureOptions);
}
}
}
}
Loading