Skip to content

Commit

Permalink
WIP: Setting up persistence caching example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed Sep 6, 2024
1 parent 2a1071f commit 726941d
Show file tree
Hide file tree
Showing 17 changed files with 132 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\Src\RCommon.EfCore\RCommon.EFCore.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.Persistence.Caching.MemoryCache\RCommon.Persistence.Caching.MemoryCache.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.Persistence.Caching\RCommon.Persistence.Caching.csproj" />
<ProjectReference Include="..\..\..\Src\RCommon.Persistence\RCommon.Persistence.csproj" />
<ProjectReference Include="..\..\..\Tests\RCommon.TestBase.Data\RCommon.TestBase.Data.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using RCommon.TestBase.Entities;

namespace Examples.Caching.PersistenceCaching
{
public interface ITestApplicationService
{
Task<ICollection<Customer>> GetCustomers(object cacheKey);
}
}
56 changes: 48 additions & 8 deletions Examples/Caching/Examples.Caching.PersistenceCaching/Program.cs
Original file line number Diff line number Diff line change
@@ -1,38 +1,78 @@

using Examples.Caching.PersistenceCaching;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RCommon;
using RCommon.Caching;
using RCommon.MemoryCache;
using RCommon.Persistence;
using RCommon.Persistence.Caching;
using RCommon.Persistence.Transactions;
using RCommon.TestBase;
using RCommon.TestBase.Data;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

try
{
/*var host = Host.CreateDefaultBuilder(args)
{

var host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) =>
{
ConfigurationContainer.Configuration = builder
.Build();
})
.ConfigureServices(services =>
{
services.AddSingleton<IConfiguration>(ConfigurationContainer.Configuration);

// Configure RCommon
services.AddRCommon()
.WithJsonSerialization<JsonNetBuilder>();
.WithPersistence<EFCorePerisistenceBuilder>(ef => // Repository/ORM configuration. We could easily swap out to Linq2Db without impact to domain service up through the stack
{
// Add all the DbContexts here
ef.AddDbContext<TestDbContext>("TestDbContext", ef =>
{
ef.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=RCommon_TestDatabase;Trusted_Connection=True;MultipleActiveResultSets=true");
});
ef.SetDefaultDataStore(dataStore =>
{
dataStore.DefaultDataStoreName = "TestDbContext";
});
ef.EnablePersistenceCaching(); // This gives us access to the caching repository interfaces/implementations
})
.WithDistributedCaching<DistributedMemoryCacheBuilder>(cache =>
{
cache.Configure(x =>
{
x.ExpirationScanFrequency = TimeSpan.FromMinutes(1);
});
});

services.AddTransient<ITestApplicationService, TestApplicationService>();

}).Build();

Console.WriteLine("Seeding Data");
var dataStoreFactory = host.Services.GetService<IDataStoreFactory>();
var context = dataStoreFactory.Resolve<TestDbContext>("TestDbContext");
var repo = new TestRepository(context);
repo.Prepare_Can_Find_Async_With_Expression();

Console.WriteLine("Example Starting");
var appService = host.Services.GetRequiredService<ITestApplicationService>();*/
var appService = host.Services.GetRequiredService<ITestApplicationService>();

Console.WriteLine("Hitting the database w/ a query");
var customers = await appService.GetCustomers("my-test-key");

Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Hitting the cache");
customers = await appService.GetCustomers("my-test-key");

Console.WriteLine("Example Complete");
repo.CleanUpSeedData();
Console.ReadLine();
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
using System;
using RCommon.Persistence.Crud;
using RCommon.TestBase.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RCommon.Persistence.Caching.Crud;

namespace Examples.Caching.PersistenceCaching
{
public class TestApplicationService
public class TestApplicationService : ITestApplicationService
{

private readonly ICachingGraphRepository<Customer> _customerRepository;

public TestApplicationService()
public TestApplicationService(ICachingGraphRepository<Customer> customerRepository)
{

_customerRepository = customerRepository;
_customerRepository.DataStoreName = "TestDbContext";
}


public async Task<ICollection<Customer>> GetCustomers(object cacheKey)
{
return await _customerRepository.FindAsync(cacheKey, x => x.LastName == "Potter");
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"ConnectionStrings": {
"TestDbContext": "Server=(localdb)\\mssqllocaldb;Database=RCommon_TestDatabase;Trusted_Connection=True;MultipleActiveResultSets=true",
"TestDbConnection": "Server=(localdb)\\mssqllocaldb;Database=RCommon_TestDatabase;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug"
}
}
}
1 change: 1 addition & 0 deletions Src/RCommon.Dapper/DapperPersistenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public DapperPersistenceBuilder(IServiceCollection services)

}

public IServiceCollection Services => _services;

public IDapperBuilder AddDbConnection<TDbConnection>(string dataStoreName, Action<RDbConnectionOptions> options)
where TDbConnection : IRDbConnection
Expand Down
1 change: 1 addition & 0 deletions Src/RCommon.EfCore/EFCorePerisistenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public EFCorePerisistenceBuilder(IServiceCollection services)
services.AddTransient(typeof(IGraphRepository<>), typeof(EFCoreRepository<>));
}

public IServiceCollection Services => _services;

public IEFCorePersistenceBuilder AddDbContext<TDbContext>(string dataStoreName, Action<DbContextOptionsBuilder>? options = null)
where TDbContext : RCommonDbContext
Expand Down
1 change: 1 addition & 0 deletions Src/RCommon.Linq2Db/Linq2DbPersistenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public Linq2DbPersistenceBuilder(IServiceCollection services)
services.AddTransient(typeof(ILinqRepository<>), typeof(Linq2DbRepository<>));
}

public IServiceCollection Services => _services;

public ILinq2DbPersistenceBuilder AddDataConnection<TDataConnection>(string dataStoreName, Func<IServiceProvider, DataOptions, DataOptions> options)
where TDataConnection : RCommonDataConnection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public class CachingGraphRepository<TEntity> : IGraphRepository<TEntity>, ICachingGraphRepository<TEntity>
public class CachingGraphRepository<TEntity> : ICachingGraphRepository<TEntity>
where TEntity : class, IBusinessEntity
{
private readonly IGraphRepository<TEntity> _repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public class CachingLinqRepository<TEntity> : ILinqRepository<TEntity>, ICachingLinqRepository<TEntity>
public class CachingLinqRepository<TEntity> : ICachingLinqRepository<TEntity>
where TEntity : class, IBusinessEntity
{
private readonly IGraphRepository<TEntity> _repository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public class CachingSqlMapperRepository<TEntity> : ISqlMapperRepository<TEntity>, ICachingSqlMapperRepository<TEntity>
public class CachingSqlMapperRepository<TEntity> : ICachingSqlMapperRepository<TEntity>
where TEntity : class, IBusinessEntity
{
private readonly ISqlMapperRepository<TEntity> _repository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RCommon.Collections;
using RCommon.Entities;
using RCommon.Persistence.Crud;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,7 +11,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public interface ICachingGraphRepository<TEntity>
public interface ICachingGraphRepository<TEntity> : IGraphRepository<TEntity>
where TEntity : class, IBusinessEntity
{
Task<IPaginatedList<TEntity>> FindAsync(object cacheKey, Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, object>> orderByExpression, bool orderByAscending, int pageNumber = 1, int pageSize = 0, CancellationToken token = default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using RCommon.Collections;
using RCommon.Entities;
using RCommon.Persistence.Crud;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -10,7 +11,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public interface ICachingLinqRepository<TEntity>
public interface ICachingLinqRepository<TEntity> : ILinqRepository<TEntity>
where TEntity : class, IBusinessEntity
{
Task<IPaginatedList<TEntity>> FindAsync(object cacheKey, Expression<Func<TEntity, bool>> expression, Expression<Func<TEntity, object>> orderByExpression, bool orderByAscending, int pageNumber = 1, int pageSize = 0, CancellationToken token = default);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using RCommon.Entities;
using RCommon.Persistence.Crud;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -9,7 +10,7 @@

namespace RCommon.Persistence.Caching.Crud
{
public interface ICachingSqlMapperRepository<TEntity>
public interface ICachingSqlMapperRepository<TEntity> : ISqlMapperRepository<TEntity>
where TEntity : class, IBusinessEntity
{
Task<ICollection<TEntity>> FindAsync(object cacheKey, ISpecification<TEntity> specification, CancellationToken token = default);
Expand Down
21 changes: 21 additions & 0 deletions Src/RCommon.Persistence.Caching/IPersistenceBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.Extensions.DependencyInjection;
using RCommon.Persistence.Caching.Crud;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.Persistence.Caching
{
public static class IPersistenceBuilderExtensions
{
public static IPersistenceBuilder EnablePersistenceCaching(this IPersistenceBuilder builder)
{
builder.Services.AddTransient(typeof(ICachingGraphRepository<>), typeof(CachingGraphRepository<>));
builder.Services.AddTransient(typeof(ICachingLinqRepository<>), typeof(CachingLinqRepository<>));
builder.Services.AddTransient(typeof(ICachingSqlMapperRepository<>), typeof(CachingSqlMapperRepository<>));
return builder;
}
}
}
2 changes: 2 additions & 0 deletions Src/RCommon.Persistence/IPersistenceBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace RCommon
Expand All @@ -8,5 +9,6 @@ namespace RCommon
public interface IPersistenceBuilder
{
IPersistenceBuilder SetDefaultDataStore(Action<DefaultDataStoreOptions> options);
IServiceCollection Services { get; }
}
}
5 changes: 3 additions & 2 deletions Tests/RCommon.Persistence.EFCore.Tests/EFCoreTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ protected void InitializeRCommon(IServiceCollection services)
services.AddRCommon()
.WithSequentialGuidGenerator(guid => guid.DefaultSequentialGuidType = SequentialGuidType.SequentialAsString)
.WithDateTimeSystem(dateTime => dateTime.Kind = DateTimeKind.Utc)
.WithPersistence<EFCorePerisistenceBuilder, DefaultUnitOfWorkBuilder>(ef => // Repository/ORM configuration. We could easily swap out to NHibernate without impact to domain service up through the stack
.WithPersistence<EFCorePerisistenceBuilder>(ef => // Repository/ORM configuration. We could easily swap out to Linq2Db without impact to domain service up through the stack
{
// Add all the DbContexts here
ef.AddDbContext<TestDbContext>("TestDbContext", ef =>
Expand All @@ -42,7 +42,8 @@ protected void InitializeRCommon(IServiceCollection services)
{
dataStore.DefaultDataStoreName = "TestDbContext";
});
}, unitOfWork =>
})
.WithUnitOfWork<DefaultUnitOfWorkBuilder>(unitOfWork =>
{
unitOfWork.SetOptions(options =>
{
Expand Down

0 comments on commit 726941d

Please sign in to comment.