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

Add plugin sample #3644

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions samples/core/SqlServer/Plugin/AugmentationTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

namespace SqlServer.Plugin;
internal class AugmentationTranslator : IMethodCallTranslator
{
public SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments, IDiagnosticsLogger<DbLoggerCategory.Query> logger)
{
switch (method.Name)
{
case nameof(DbFunctionsExtensions.Augment):
var argument = arguments[1];
return new SqlBinaryExpression(ExpressionType.Add, argument, new SqlConstantExpression(Expression.Constant(1), null), argument.Type, null);
default:
throw new InvalidOperationException($"Unexpected method '{method.Name}' in '{nameof(RelationalDbFunctionsExtensions)}'.");
}
}
}
9 changes: 9 additions & 0 deletions samples/core/SqlServer/Plugin/Blog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace SqlServer.Plugin;
public class Blog
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int BlogId { get; set; }
public string Url { get; set; }
}
13 changes: 13 additions & 0 deletions samples/core/SqlServer/Plugin/BloggingContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Plugin;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=EFSaving.Basics;Trusted_Connection=True"
, o => o.UseAugmentation()
);
}
26 changes: 26 additions & 0 deletions samples/core/SqlServer/Plugin/CustomPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace SqlServer.Plugin;

public class CustomPlugin
{
public static void Run()
{
using (var context = new BloggingContext())
{
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
}

using (var context = new BloggingContext())
{
context.Blogs.Add(new Blog { BlogId = 100, Url = "http://blog1.somesite.com" });

context.SaveChanges();

var simple = context.Blogs.Select(b => EF.Functions.Augment(b.BlogId)).First();
var withCast = context.Blogs.Select(b => EF.Functions.Augment((long)b.BlogId)).First();
}
}
}
10 changes: 10 additions & 0 deletions samples/core/SqlServer/Plugin/DbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

namespace SqlServer.Plugin;
public static class DbFunctionsExtensions
virzak marked this conversation as resolved.
Show resolved Hide resolved
{
public static T Augment<T>(this DbFunctions _, T number)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Augment)));
}
83 changes: 83 additions & 0 deletions samples/core/SqlServer/Plugin/PluginSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.Extensions.DependencyInjection;

namespace SqlServer.Plugin;
public class AugmentationExtension : IDbContextOptionsExtension
{
private ExtensionInfo _info;

public virtual DbContextOptionsExtensionInfo Info
=> _info ??= new ExtensionInfo(this);

public void ApplyServices(IServiceCollection services) => services.AddAugmentationExtension();

public void Validate(IDbContextOptions options)
{
}

private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
public ExtensionInfo(IDbContextOptionsExtension extension)
: base(extension)
{
}

private new AugmentationExtension Extension
=> (AugmentationExtension)base.Extension;

public override bool IsDatabaseProvider
=> false;

public override int GetServiceProviderHashCode()
=> 0;

public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
=> other is ExtensionInfo;

public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
=> debugInfo["SqlServer:" + nameof(SqlServerExtensions.UseAugmentation)] = "1";

public override string LogFragment
=> "using Augmentation";
}
}

public static class SqlServerNetTopologySuiteServiceCollectionExtensions
virzak marked this conversation as resolved.
Show resolved Hide resolved
{
public static IServiceCollection AddAugmentationExtension(
this IServiceCollection serviceCollection)
{
new EntityFrameworkRelationalServicesBuilder(serviceCollection)
.TryAdd<IMethodCallTranslatorPlugin, AugmentationTranslatorPlugin>();

return serviceCollection;
}
}

public class AugmentationTranslatorPlugin : IMethodCallTranslatorPlugin
{
public IEnumerable<IMethodCallTranslator> Translators { get; }

public AugmentationTranslatorPlugin()
{
var list = new List<IMethodCallTranslator>
{
new AugmentationTranslator()
};
Translators = list;
}
}

public static class SqlServerExtensions
{
public static SqlServerDbContextOptionsBuilder UseAugmentation(this SqlServerDbContextOptionsBuilder optionsBuilder)
{
var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;
var extension = coreOptionsBuilder.Options.FindExtension<AugmentationExtension>() ?? new AugmentationExtension();
((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension);

return optionsBuilder;
}
}
1 change: 1 addition & 0 deletions samples/core/SqlServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class Program
{
private static void Main()
{
Plugin.CustomPlugin.Run();
}
}
}