Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmwebb-lv committed Jun 6, 2024
1 parent 193d673 commit 4eb0323
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder)
public static IRCommonBuilder WithEventHandling<T>(this IRCommonBuilder builder, Action<T> actions)
where T : IEventHandlingBuilder
{
Guard.Against<UnsupportedDataStoreException>(dataStoreName.IsNullOrEmpty(), "You must set a name for the Data Store");

this._services.TryAddTransient<IDataStoreFactory, DataStoreFactory>();
this._services.Configure<DataStoreFactoryOptions>(options => options.Register<TDbContext>(dataStoreName));

// Event Handling Configurations
var eventHandlingConfig = (T)Activator.CreateInstance(typeof(T), new object[] { builder });
actions(eventHandlingConfig);
Expand Down
45 changes: 45 additions & 0 deletions Src/RCommon.Core/EventHandling/Subscribers/SubscriberFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.EventHandling.Subscribers
{
public class SubscriberFactory
{
private readonly IServiceProvider _provider;
private readonly IDictionary<string, Type> _types;

public SubscriberFactory(IServiceProvider provider, IOptions<SubscriberFactoryOptions> options)
{
_provider = provider;
_types = options.Value.Types;
}

public ISubscriber<TEvent> Resolve<TEvent>()
where TEvent : class, ISerializableEvent
{
if (_types.TryGetValue(name, out var type))
{
return (ISubscriber<TEvent>)_provider.GetRequiredService(type);
}

throw new SubscriberNotFoundException($"Subscriber with name of {name} not found");
}

public TSubscriber Resolve<TSubscriber, TEvent>()
where TSubscriber : class, ISubscriber<TEvent>
where TEvent : class, ISerializableEvent
{
if (_types.TryGetValue(name, out var type))
{
return (TSubscriber)_provider.GetRequiredService(type);
}

throw new SubscriberNotFoundException($"Subscriber with name of {name} not found");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.EventHandling.Subscribers
{
public class SubscriberFactoryOptions
{
public IDictionary<string, Type> Types { get; } = new Dictionary<string, Type>();

public void Register<TSubscriber, TEvent>()
where TSubscriber : class, ISubscriber<TEvent>
{
Types.Add(typeof(TSubscriber).AssemblyQualifiedName, typeof(TSubscriber));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RCommon.EventHandling.Subscribers
{
public class SubscriberNotFoundException : GeneralException
{
public SubscriberNotFoundException(string message) : base(message)
{

}
}
}
4 changes: 2 additions & 2 deletions Src/RCommon.Persistence/DataStoreFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IDataStore Resolve(string name)
return (IDataStore)_provider.GetRequiredService(type);
}

throw new ArgumentOutOfRangeException(nameof(name));
throw new DataStoreNotFoundException($"DataStore with name of {name} not found");
}

public TDataStore Resolve<TDataStore>(string name)
Expand All @@ -38,7 +38,7 @@ public TDataStore Resolve<TDataStore>(string name)
return (TDataStore)_provider.GetRequiredService(type);
}

throw new ArgumentOutOfRangeException(nameof(name));
throw new DataStoreNotFoundException($"DataStore with name of {name} not found");
}
}
}
16 changes: 0 additions & 16 deletions Src/RCommon.Persistence/PersistenceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
where TObjectAccess : IPersistenceBuilder
where TUnitOfWork : IUnitOfWorkBuilder
{

return WithPersistence<TObjectAccess, TUnitOfWork>(builder, objectAccessActions, x => { });
}

Expand All @@ -37,7 +36,6 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
where TObjectAccess : IPersistenceBuilder
where TUnitOfWork : IUnitOfWorkBuilder
{

return WithPersistence<TObjectAccess, TUnitOfWork>(builder, x => { }, uniOfWorkActions);
}

Expand All @@ -46,20 +44,6 @@ public static IRCommonBuilder WithPersistence<TObjectAccess, TUnitOfWork>(this I
where TObjectAccess : IPersistenceBuilder
where TUnitOfWork : IUnitOfWorkBuilder
{
// Data Store Management
//var dataStore = new ScopedDataStore();
//var dataStoreActions = new Action<ScopedDataStore>(x => {}); ;
//dataStoreActions(dataStore);
//builder.Services.Configure(dataStoreActions);
//builder.Services.AddScoped<IDataStoreRegistry, CachedDataStoreRegistry>();
//builder.Services.AddDistributedMemoryCache();
//builder.Services.AddScoped<IDataStoreFactory, DataStoreFactory>();

// Object Access and Unit of Work Configurations
// Wire up the "out of the box" events/event handlers used in persistence. These are not transactional
//builder.Services.AddScoped<ISubscriber<UnitOfWorkCreatedEvent>, UnitOfWorkCreatedHandler>();
//builder.Services.AddScoped<ISubscriber<UnitOfWorkCommittedEvent>, UnitOfWorkCommittedEventHandler>();

var dataConfiguration = (TObjectAccess)Activator.CreateInstance(typeof(TObjectAccess), new object[] { builder.Services });
objectAccessActions(dataConfiguration);
var unitOfWorkConfiguration = (TUnitOfWork)Activator.CreateInstance(typeof(TUnitOfWork), new object[] { builder.Services });
Expand Down

0 comments on commit 4eb0323

Please sign in to comment.