Skip to content

Commit

Permalink
fix: Rework services registration
Browse files Browse the repository at this point in the history
  • Loading branch information
skarllot committed Jul 30, 2023
1 parent d8abb10 commit 00868d0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ To use Raiqub.Expressions in your project, follow these steps:
For Entity Framework Core:

```csharp
// The DbContext must be registered calling AddDbContextFactory<YourDbContext>()
services.AddEntityFrameworkExpressions()
.AddSingleContext<YourDbContext>();
```
Expand Down
12 changes: 6 additions & 6 deletions src/Expressions.EntityFrameworkCore/ExpressionsSessionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public void AddSingleContext<TDbContext>(ChangeTracking? tracking = null)
{
var combinedTracking = tracking ?? _tracking;

_services.AddScoped<EFSessionFactory<TDbContext>>();
_services.AddTransient<ISessionFactory>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddTransient<IQuerySessionFactory>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddSingleton<EFSessionFactory<TDbContext>>();
_services.AddSingleton<ISessionFactory>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddSingleton<IQuerySessionFactory>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddScoped<ISession>(sp => sp.GetRequiredService<ISessionFactory>().Create(combinedTracking));
_services.AddScoped<IQuerySession>(sp => sp.GetRequiredService<IQuerySessionFactory>().Create());
}
Expand All @@ -52,9 +52,9 @@ public ExpressionsSessionBuilder AddContext<TContext, TDbContext>(ChangeTracking
{
var combinedTracking = tracking ?? _tracking;

_services.AddScoped<EFSessionFactory<TDbContext>>();
_services.AddTransient<ISessionFactory<TContext>>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddTransient<IQuerySessionFactory<TContext>>(
_services.AddSingleton<EFSessionFactory<TDbContext>>();
_services.AddSingleton<ISessionFactory<TContext>>(sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddSingleton<IQuerySessionFactory<TContext>>(
sp => sp.GetRequiredService<EFSessionFactory<TDbContext>>());
_services.AddScoped<ISession<TContext>>(
sp => sp.GetRequiredService<ISessionFactory<TContext>>().Create(combinedTracking));
Expand Down
7 changes: 3 additions & 4 deletions src/Expressions.EntityFrameworkCore/Sessions/EFSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected virtual ValueTask DisposeAsyncCore()
protected virtual async ValueTask DisposeAsyncCore()
{
Context.ChangeTracker.Clear();
return new ValueTask();
await Context.DisposeAsync();
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Context.ChangeTracker.Clear();
Context.Dispose();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class EFSessionFactory<TContext>
where TContext : DbContext
{
private readonly ILoggerFactory _loggerFactory;
private readonly TContext _context;
private readonly IDbContextFactory<TContext> _contextFactory;

public EFSessionFactory(ILoggerFactory loggerFactory, TContext context)
public EFSessionFactory(ILoggerFactory loggerFactory, IDbContextFactory<TContext> contextFactory)
{
_loggerFactory = loggerFactory;
_context = context;
_contextFactory = contextFactory;
}

public EFSession<TContext> Create(ChangeTracking? tracking = null) => new(
_loggerFactory.CreateLogger<EFSession<TContext>>(),
_context,
_contextFactory.CreateDbContext(),
tracking ?? ChangeTracking.Default);

public EFSession<TContext> CreateForQuery() => Create(ChangeTracking.Disable);
Expand Down
12 changes: 6 additions & 6 deletions src/Expressions.Marten/ExpressionsSessionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public void AddSingleContext(ChangeTracking? tracking = null)
{
var combinedTracking = tracking ?? _tracking;

_services.AddScoped<MartenSessionFactory>();
_services.AddTransient<ISessionFactory>(sp => sp.GetRequiredService<MartenSessionFactory>());
_services.AddTransient<IQuerySessionFactory>(sp => sp.GetRequiredService<MartenSessionFactory>());
_services.AddSingleton<MartenSessionFactory>();
_services.AddSingleton<ISessionFactory>(sp => sp.GetRequiredService<MartenSessionFactory>());
_services.AddSingleton<IQuerySessionFactory>(sp => sp.GetRequiredService<MartenSessionFactory>());
_services.AddScoped<ISession>(sp => sp.GetRequiredService<ISessionFactory>().Create(combinedTracking));
_services.AddScoped<IQuerySession>(sp => sp.GetRequiredService<IQuerySessionFactory>().Create());
}
Expand All @@ -49,9 +49,9 @@ public ExpressionsSessionBuilder AddContext<TContext, TDbContext>(ChangeTracking
{
var combinedTracking = tracking ?? _tracking;

_services.AddScoped<MartenSessionFactory<TDbContext>>();
_services.AddTransient<ISessionFactory<TContext>>(sp => sp.GetRequiredService<MartenSessionFactory<TDbContext>>());
_services.AddTransient<IQuerySessionFactory<TContext>>(
_services.AddSingleton<MartenSessionFactory<TDbContext>>();
_services.AddSingleton<ISessionFactory<TContext>>(sp => sp.GetRequiredService<MartenSessionFactory<TDbContext>>());
_services.AddSingleton<IQuerySessionFactory<TContext>>(
sp => sp.GetRequiredService<MartenSessionFactory<TDbContext>>());
_services.AddScoped<ISession<TContext>>(
sp => sp.GetRequiredService<ISessionFactory<TContext>>().Create(combinedTracking));
Expand Down
4 changes: 2 additions & 2 deletions src/Expressions.Marten/Sessions/MartenQuerySession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public void Dispose()
GC.SuppressFinalize(this);
}

protected virtual ValueTask DisposeAsyncCore()
protected virtual async ValueTask DisposeAsyncCore()
{
return _session.DisposeAsync();
await _session.DisposeAsync();
}

protected virtual void Dispose(bool disposing)
Expand Down
2 changes: 1 addition & 1 deletion src/Expressions.Marten/Sessions/MartenSessionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal static IDocumentSession CreateSession(IDocumentStore documentStore, Cha
{
return tracking switch
{
ChangeTracking.Default => documentStore.IdentitySession(),
ChangeTracking.Default => documentStore.LightweightSession(),
ChangeTracking.Enable => documentStore.DirtyTrackedSession(),
ChangeTracking.IdentityResolution => documentStore.IdentitySession(),
ChangeTracking.Disable => documentStore.LightweightSession(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static IServiceCollection AddSqliteDbContext<TContext>(this IServiceColle
.UseSqlite(sp.GetRequiredService<SqliteConnection>())
.Options);

services.AddDbContext<TContext>();
services.AddDbContextFactory<TContext>();

return services;
}
Expand Down

0 comments on commit 00868d0

Please sign in to comment.