Skip to content
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

Feature/correct registering ef core triggers services #31

Merged
Merged
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
19 changes: 9 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ EfCoreTriggers is the library to write native SQL triggers using EFCore model bu
[![latest version](https://img.shields.io/nuget/v/Laraue.EfCoreTriggers.Common)](https://www.nuget.org/packages/Laraue.EfCoreTriggers.Common)

### Installation
EfCoreTriggers common package is available on Nuget. Version 6.x.x is compatible with .NET6, 1.x.x intended for .NET 5 and no more supported. Install the provider package corresponding to your target database. See the list of providers in the docs for additional databases.
EfCoreTriggers common package is available on Nuget. Version 6.x.x is compatible with .NET6, 5.x.x intended for .NET 5. Install the provider package corresponding to your target database. See the list of providers in the docs for additional databases.

### Configuring DB to use triggers

Expand Down Expand Up @@ -138,30 +138,29 @@ var dbContext = new TestDbContext(options);

## Customization

Using custom provider to extend additional functionality
Any service using for generation SQL can be replaced.

```cs
private class MyCustomSqlProvider : PostgreSqlProvider // Or another used provider
private class CustomDbSchemaRetriever : EfCoreDbSchemaRetriever
{
/// Provider will be created via reflection, so constructor only with this argument is allowed
public MySqlProvider(IModel model) : base(model)
public CustomDbSchemaRetriever(IModel model) : base(model)
{
}

protected override string GetColumnName(MemberInfo memberInfo)
protected override string GetColumnName(Type type, MemberInfo memberInfo)
{
// Change strategy of naming some column
return 'c_' + base.GetColumnName(memberInfo);
return 'c_' + base.GetColumnName(type, memberInfo);
}
}
```

Adding this provider to a container
Adding this service to the container

```cs
var options = new DbContextOptionsBuilder<TestDbContext>()
.UseNpgsql("User ID=test;Password=test;Host=localhost;Port=5432;Database=test;")
.UseTriggers<MyCustomSqlProvider>()
.UsePostgreSqlTriggers(services => services.AddSingleton<IDbSchemaRetriever, CustomDbSchemaRetriever>)
.Options;

var dbContext = new TestDbContext(options);
Expand Down Expand Up @@ -213,7 +212,7 @@ All custom converters should be added while setup a database
```cs
var options = new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite("Filename=D://test.db")
.UseSqlLiteTriggers(converters => converters.ExpressionCallConverters.Push(converter))
.UseSqlLiteTriggers(services => services.AddMethodCallConverter(converter))
.Options;

var dbContext = new TestDbContext(options);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using Laraue.EfCoreTriggers.Common.Migrations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;

namespace Laraue.EfCoreTriggers.Common.Extensions
{
Expand All @@ -16,10 +18,17 @@ public static class DbContextOptionsBuilderExtensions
/// which allow to use triggers.
/// </summary>
/// <param name="optionsBuilder"></param>
/// <param name="addDefaultServices"></param>
/// <param name="modifyServices"></param>
/// <returns></returns>
public static DbContextOptionsBuilder ReplaceMigrationsModelDiffer(
this DbContextOptionsBuilder optionsBuilder)
public static DbContextOptionsBuilder UseEfCoreTriggers(
this DbContextOptionsBuilder optionsBuilder,
Action<IServiceCollection> addDefaultServices,
Action<IServiceCollection> modifyServices)
{
((IDbContextOptionsBuilderInfrastructure)optionsBuilder)
.AddOrUpdateExtension(new EfCoreTriggersExtension(addDefaultServices, modifyServices));

return optionsBuilder.ReplaceService<IMigrationsModelDiffer, MigrationsModelDiffer>();
}

Expand All @@ -28,12 +37,19 @@ public static class DbContextOptionsBuilderExtensions
/// which allow to use triggers.
/// </summary>
/// <param name="optionsBuilder"></param>
/// <param name="addDefaultServices"></param>
/// <param name="modifyServices"></param>
/// <typeparam name="TContext"></typeparam>
/// <returns></returns>
public static DbContextOptionsBuilder<TContext> ReplaceMigrationsModelDiffer<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder)
public static DbContextOptionsBuilder<TContext> UseEfCoreTriggers<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder,
Action<IServiceCollection> addDefaultServices,
Action<IServiceCollection> modifyServices)
where TContext : DbContext
{
((IDbContextOptionsBuilderInfrastructure)optionsBuilder)
.AddOrUpdateExtension(new EfCoreTriggersExtension(addDefaultServices, modifyServices));

return optionsBuilder.ReplaceService<IMigrationsModelDiffer, MigrationsModelDiffer>();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Laraue.EfCoreTriggers.Common.Extensions;

/// <inheritdoc />
public class EfCoreTriggersExtension : IDbContextOptionsExtension
{
private readonly Action<IServiceCollection> _modifyServices;

/// <summary>
/// Initializes a new instance of <see cref="EfCoreTriggersExtension"/>.
/// </summary>
/// <param name="addDefaultServices"></param>
/// <param name="modifyServices"></param>
public EfCoreTriggersExtension(Action<IServiceCollection> addDefaultServices, Action<IServiceCollection> modifyServices)
{
_modifyServices = addDefaultServices;

if (modifyServices is not null)
{
_modifyServices += modifyServices;
}

Info = new EfCoreTriggersExtensionInfo(this);
}

/// <inheritdoc />
public void ApplyServices(IServiceCollection services)
{
_modifyServices(services);
}

/// <inheritdoc />
public void Validate(IDbContextOptions options)
{
}

/// <inheritdoc />
public DbContextOptionsExtensionInfo Info { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Laraue.EfCoreTriggers.Common.Extensions;

/// <inheritdoc />
public class EfCoreTriggersExtensionInfo : DbContextOptionsExtensionInfo
{
/// <summary>
/// Initializes a new instance of <see cref="EfCoreTriggersExtensionInfo"/>.
/// </summary>
/// <param name="extension"></param>
public EfCoreTriggersExtensionInfo(IDbContextOptionsExtension extension)
: base(extension)
{
}

/// <inheritdoc />
public override int GetServiceProviderHashCode()
{
return 0;
}

/// <inheritdoc />
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
{
return string.Equals(LogFragment, other.LogFragment, StringComparison.Ordinal);
}

/// <inheritdoc />
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
{
}

/// <inheritdoc />
public override bool IsDatabaseProvider => false;

/// <inheritdoc />
public override string LogFragment => "EfCoreTriggersExtension";
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static class ServiceCollectionExtensions
where TVisitorType : ITriggerAction
where TImpl : class, ITriggerActionVisitor<TVisitorType>
{
return services.AddSingleton<ITriggerActionVisitor<TVisitorType>, TImpl>();
return services.AddScoped<ITriggerActionVisitor<TVisitorType>, TImpl>();
}

/// <summary>
Expand All @@ -40,7 +40,7 @@ public static class ServiceCollectionExtensions
public static IServiceCollection AddMethodCallConverter<TImpl>(this IServiceCollection services)
where TImpl : class, IMethodCallVisitor
{
return services.AddSingleton<IMethodCallVisitor, TImpl>();
return services.AddScoped<IMethodCallVisitor, TImpl>();
}

/// <summary>
Expand All @@ -55,7 +55,7 @@ public static IServiceCollection AddMethodCallConverter<TImpl>(this IServiceColl
where TImpl : class, IExpressionVisitor<TVisitorType>
where TVisitorType : Expression
{
return services.AddSingleton<IExpressionVisitor<TVisitorType>, TImpl>();
return services.AddScoped<IExpressionVisitor<TVisitorType>, TImpl>();
}

/// <summary>
Expand All @@ -65,28 +65,28 @@ public static IServiceCollection AddMethodCallConverter<TImpl>(this IServiceColl
/// <returns></returns>
public static IServiceCollection AddDefaultServices(this IServiceCollection services)
{
return services.AddSingleton<ITriggerActionVisitorFactory, TriggerActionVisitorFactory>()
.AddSingleton<IExpressionVisitorFactory, ExpressionVisitorFactory>()
.AddSingleton<IMemberInfoVisitorFactory, MemberInfoVisitorFactory>()
return services.AddScoped<ITriggerActionVisitorFactory, TriggerActionVisitorFactory>()
.AddScoped<IExpressionVisitorFactory, ExpressionVisitorFactory>()
.AddScoped<IMemberInfoVisitorFactory, MemberInfoVisitorFactory>()

.AddTriggerActionVisitor<TriggerDeleteAction, TriggerDeleteActionVisitor>()
.AddTriggerActionVisitor<TriggerInsertAction, TriggerInsertActionVisitor>()
.AddTriggerActionVisitor<TriggerRawAction, TriggerRawActionVisitor>()
.AddTriggerActionVisitor<TriggerUpdateAction, TriggerUpdateActionVisitor>()
.AddTriggerActionVisitor<TriggerCondition, TriggerConditionVisitor>()

.AddSingleton<IMemberInfoVisitor<LambdaExpression>, SetLambdaExpressionVisitor>()
.AddSingleton<IMemberInfoVisitor<MemberInitExpression>, SetMemberInitExpressionVisitor>()
.AddSingleton<IMemberInfoVisitor<NewExpression>, SetNewExpressionVisitor>()
.AddScoped<IMemberInfoVisitor<LambdaExpression>, SetLambdaExpressionVisitor>()
.AddScoped<IMemberInfoVisitor<MemberInitExpression>, SetMemberInitExpressionVisitor>()
.AddScoped<IMemberInfoVisitor<NewExpression>, SetNewExpressionVisitor>()

.AddSingleton<IDbSchemaRetriever, EfCoreDbSchemaRetriever>()
.AddScoped<IDbSchemaRetriever, EfCoreDbSchemaRetriever>()

.AddExpressionVisitor<BinaryExpression, BinaryExpressionVisitor>()
.AddExpressionVisitor<UnaryExpression, UnaryExpressionVisitor>()
.AddExpressionVisitor<MemberExpression, MemberExpressionVisitor>()
.AddExpressionVisitor<ConstantExpression, ConstantExpressionVisitor>()
.AddExpressionVisitor<MethodCallExpression, MethodCallExpressionVisitor>()

.AddSingleton<IUpdateExpressionVisitor, UpdateExpressionVisitor>();
.AddScoped<IUpdateExpressionVisitor, UpdateExpressionVisitor>();
}
}
32 changes: 0 additions & 32 deletions src/Laraue.EfCoreTriggers.Common/Extensions/TriggerExtensions.cs

This file was deleted.

Loading