Skip to content

Commit

Permalink
fix: IUnitOfWork injection (#52)
Browse files Browse the repository at this point in the history
* fix: IUnitOfWork injection

* chore: update tests

* fix: resolve unitofwork from correct key
  • Loading branch information
mvarendorff2 authored Oct 7, 2024
1 parent 6e647c8 commit b57d2f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Fluss.HotChocolate/AddExtensionMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async ValueTask InvokeAsync(IRequestContext context)
{
await _next.Invoke(context);

if (!context.ContextData.TryGetValue(nameof(UnitOfWork), out var unitOfWork))
if (!context.ContextData.TryGetValue(nameof(IUnitOfWork), out var unitOfWork))
{
return;
}
Expand All @@ -38,7 +38,7 @@ public async ValueTask InvokeAsync(IRequestContext context)
{
if (context.Result is QueryResult subsequentQueryResult)
{
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(UnitOfWork),
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(IUnitOfWork),
unitOfWork).Create();
}

Expand Down Expand Up @@ -84,7 +84,7 @@ private async IAsyncEnumerable<IQueryResult> LiveResults(IReadOnlyDictionary<str

while (true)
{
if (contextData == null || !contextData.TryGetValue(nameof(UnitOfWork), out var value))
if (contextData == null || !contextData.TryGetValue(nameof(IUnitOfWork), out var value))
{
break;
}
Expand Down
27 changes: 14 additions & 13 deletions src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class UnitOfWorkParameterExpressionBuilder : IParameterExpressionBuilder
private static readonly MethodInfo GetOrSetGlobalStateUnitOfWorkMethod =
typeof(ResolverContextExtensions).GetMethods()
.First(m => m.Name == nameof(ResolverContextExtensions.GetOrSetGlobalState))
.MakeGenericMethod(typeof(UnitOfWork));
.MakeGenericMethod(typeof(IUnitOfWork));

private static readonly MethodInfo GetGlobalStateOrDefaultLongMethod =
typeof(ResolverContextExtensions).GetMethods()
Expand All @@ -21,22 +21,22 @@ public class UnitOfWorkParameterExpressionBuilder : IParameterExpressionBuilder

private static readonly MethodInfo ServiceUnitOfWorkMethod =
typeof(IPureResolverContext).GetMethods().First(
method => method is { Name: nameof(IPureResolverContext.Service), IsGenericMethod: true })
.MakeGenericMethod(typeof(UnitOfWork));
method => method is { Name: nameof(IPureResolverContext.Service), IsGenericMethod: true })
.MakeGenericMethod(typeof(IUnitOfWork));

private static readonly MethodInfo WithPrefilledVersionMethod =
typeof(UnitOfWork).GetMethods(BindingFlags.Instance | BindingFlags.Public)
.First(m => m.Name == nameof(UnitOfWork.WithPrefilledVersion));
typeof(IUnitOfWork).GetMethods(BindingFlags.Instance | BindingFlags.Public)
.First(m => m.Name == nameof(IUnitOfWork.WithPrefilledVersion));

public bool CanHandle(ParameterInfo parameter) => typeof(UnitOfWork) == parameter.ParameterType
|| typeof(IUnitOfWork) == parameter.ParameterType;
public bool CanHandle(ParameterInfo parameter) =>
typeof(IUnitOfWork) == parameter.ParameterType;

/*
* Produces something like this: context.GetOrSetGlobalState(
* nameof(UnitOfWork.UnitOfWork),
* nameof(IUnitOfWork),
* _ =>
* context
* .Service<UnitOfWork.UnitOfWork>()
* .Service<IUnitOfWork>()
* .WithPrefilledVersion(
* context.GetGlobalState<long>(PrefillUnitOfWorkVersion)
* ))!;
Expand All @@ -53,13 +53,14 @@ public Expression Build(ParameterExpressionBuilderContext builderContext)
context,
Expression.Constant(PrefillUnitOfWorkVersion)));

return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context, Expression.Constant(nameof(UnitOfWork)),
Expression.Lambda<Func<string, UnitOfWork>>(
return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context,
Expression.Constant(nameof(IUnitOfWork)),
Expression.Lambda<Func<string, IUnitOfWork>>(
getNewUnitOfWork,
Expression.Parameter(typeof(string))));
}

public ArgumentKind Kind => ArgumentKind.Custom;
public bool IsPure => true;
public bool IsPure => false;
public bool IsDefaultHandler => false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public class UnitOfWorkParameterExpressionBuilderTest
private readonly UnitOfWorkParameterExpressionBuilder _builder = new();

[Fact]
public void CanHandle_ShouldReturnTrueForUnitOfWorkParameter()
public void CanHandle_ShouldReturnFalseForUnitOfWorkParameter()
{
var parameter = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithUnitOfWork))!.GetParameters()[0];
Assert.True(_builder.CanHandle(parameter));
Assert.False(_builder.CanHandle(parameter));
}

[Fact]
Expand All @@ -39,9 +39,9 @@ public void Kind_ShouldReturnCustom()
}

[Fact]
public void IsPure_ShouldReturnTrue()
public void IsPure_ShouldReturnFalse()
{
Assert.True(_builder.IsPure);
Assert.False(_builder.IsPure);
}

[Fact]
Expand Down

0 comments on commit b57d2f7

Please sign in to comment.