Skip to content

Commit

Permalink
Fix TypeMappingSource injection/api consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
nmichels committed Dec 12, 2019
1 parent a77d3d5 commit 31495a7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,28 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
using Microsoft.EntityFrameworkCore.Storage;

namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal
{
public class SqlServerDateTimeFromPartsFunctionTranslator : IMethodCallTranslator
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IRelationalTypeMappingSource _typeMappingSource;

private static readonly MethodInfo _methodInfo = typeof(SqlServerDbFunctionsExtensions)
.GetRuntimeMethod(nameof(SqlServerDbFunctionsExtensions.DateTimeFromParts), new[] { typeof(DbFunctions), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int), typeof(int) });

public SqlServerDateTimeFromPartsFunctionTranslator(ISqlExpressionFactory sqlExpressionFactory)
=> _sqlExpressionFactory = sqlExpressionFactory;
public SqlServerDateTimeFromPartsFunctionTranslator(
[NotNull] ISqlExpressionFactory sqlExpressionFactory,
[NotNull] IRelationalTypeMappingSource typeMappingSource)
{
_sqlExpressionFactory = sqlExpressionFactory;
_typeMappingSource = typeMappingSource;
}

public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method, IReadOnlyList<SqlExpression> arguments)
{
Expand All @@ -27,7 +35,7 @@ public virtual SqlExpression Translate(SqlExpression instance, MethodInfo method
"DATETIMEFROMPARTS",
arguments.Skip(1),
_methodInfo.ReturnType,
_sqlExpressionFactory.FindMapping(typeof(DateTime)))
_typeMappingSource.FindMapping(typeof(DateTime)))
: null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;

namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Internal
{
public class SqlServerMethodCallTranslatorProvider : RelationalMethodCallTranslatorProvider
{
public SqlServerMethodCallTranslatorProvider([NotNull] RelationalMethodCallTranslatorProviderDependencies dependencies)
public SqlServerMethodCallTranslatorProvider([NotNull] RelationalMethodCallTranslatorProviderDependencies dependencies, [NotNull] IRelationalTypeMappingSource typeMappingSource)
: base(dependencies)
{
var sqlExpressionFactory = dependencies.SqlExpressionFactory;
AddTranslators(
new IMethodCallTranslator[]
{
new SqlServerByteArrayMethodTranslator(sqlExpressionFactory),
new SqlServerConvertTranslator(sqlExpressionFactory),
new SqlServerByteArrayMethodTranslator(sqlExpressionFactory),
new SqlServerConvertTranslator(sqlExpressionFactory),
new SqlServerDateDiffFunctionsTranslator(sqlExpressionFactory),
new SqlServerDateTimeFromPartsFunctionTranslator(sqlExpressionFactory),
new SqlServerDateTimeFromPartsFunctionTranslator(sqlExpressionFactory, typeMappingSource),
new SqlServerDateTimeMethodTranslator(sqlExpressionFactory),
new SqlServerFullTextSearchFunctionsTranslator(sqlExpressionFactory),
new SqlServerIsDateFunctionTranslator(sqlExpressionFactory),
Expand Down

0 comments on commit 31495a7

Please sign in to comment.