Skip to content

Commit

Permalink
Merge pull request #50 from Nexus-Mods/di-entities
Browse files Browse the repository at this point in the history
Add DI support to IConnection
  • Loading branch information
halgari authored May 1, 2024
2 parents cf3d47b + d6cdac1 commit ba4aafe
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/IConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public interface IConnection
/// </summary>
public IObservable<IDb> Revisions { get; }

/// <summary>
/// A service provider that entities can use to resolve their values
/// </summary>
public IServiceProvider ServiceProvider { get; }

/// <summary>
/// Returns a snapshot of the database as of the given transaction id.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/NexusMods.MnemonicDB.Abstractions/Models/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ protected Entity(ITransaction tx, byte partition = (byte)Ids.Partition.Entity)

}

/// <summary>
/// Get the service provider for the current Database connection, use this to
/// resolve dependencies.
/// </summary>
protected IServiceProvider ServiceProvider => Db.Connection.ServiceProvider;

/// <summary>
/// The transaction the entity is currently attached to (if any)
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion src/NexusMods.MnemonicDB/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public class Connection : IConnection
/// <summary>
/// Main connection class, co-ordinates writes and immutable reads
/// </summary>
public Connection(ILogger<Connection> logger, IDatomStore store, IEnumerable<IAttribute> declaredAttributes)
public Connection(ILogger<Connection> logger, IDatomStore store, IServiceProvider provider, IEnumerable<IAttribute> declaredAttributes)
{
ServiceProvider = provider;
_store = store;
// Async startup routines, we'll deref this task when we interact with the store
_startupTask = Task.Run(async () =>
Expand All @@ -47,6 +48,8 @@ public Connection(ILogger<Connection> logger, IDatomStore store, IEnumerable<IAt
});
}

/// <inheritdoc />
public IServiceProvider ServiceProvider { get; set; }

/// <inheritdoc />
public IDb Db
Expand Down
4 changes: 2 additions & 2 deletions tests/NexusMods.MnemonicDB.Tests/AEventSourcingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected AMnemonicDBTest(IServiceProvider provider)
_backend = new Backend(_registry);

_store = new DatomStore(provider.GetRequiredService<ILogger<DatomStore>>(), _registry, Config, _backend);
Connection = new Connection(provider.GetRequiredService<ILogger<Connection>>(), _store, _attributes);
Connection = new Connection(provider.GetRequiredService<ILogger<Connection>>(), _store, provider, _attributes);

Logger = provider.GetRequiredService<ILogger<AMnemonicDBTest>>();
}
Expand Down Expand Up @@ -147,6 +147,6 @@ protected async Task RestartDatomStore()
_registry = new AttributeRegistry(_attributes);
_store = new DatomStore(_provider.GetRequiredService<ILogger<DatomStore>>(), _registry, Config, _backend);

Connection = new Connection(_provider.GetRequiredService<ILogger<Connection>>(), _store, _attributes);
Connection = new Connection(_provider.GetRequiredService<ILogger<Connection>>(), _store, _provider, _attributes);
}
}

0 comments on commit ba4aafe

Please sign in to comment.