Skip to content

Commit b57d2f7

Browse files
authored
fix: IUnitOfWork injection (#52)
* fix: IUnitOfWork injection * chore: update tests * fix: resolve unitofwork from correct key
1 parent 6e647c8 commit b57d2f7

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

src/Fluss.HotChocolate/AddExtensionMiddleware.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async ValueTask InvokeAsync(IRequestContext context)
2121
{
2222
await _next.Invoke(context);
2323

24-
if (!context.ContextData.TryGetValue(nameof(UnitOfWork), out var unitOfWork))
24+
if (!context.ContextData.TryGetValue(nameof(IUnitOfWork), out var unitOfWork))
2525
{
2626
return;
2727
}
@@ -38,7 +38,7 @@ public async ValueTask InvokeAsync(IRequestContext context)
3838
{
3939
if (context.Result is QueryResult subsequentQueryResult)
4040
{
41-
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(UnitOfWork),
41+
context.Result = QueryResultBuilder.FromResult(subsequentQueryResult).AddContextData(nameof(IUnitOfWork),
4242
unitOfWork).Create();
4343
}
4444

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

8585
while (true)
8686
{
87-
if (contextData == null || !contextData.TryGetValue(nameof(UnitOfWork), out var value))
87+
if (contextData == null || !contextData.TryGetValue(nameof(IUnitOfWork), out var value))
8888
{
8989
break;
9090
}

src/Fluss.HotChocolate/UnitOfWorkParameterExpressionBuilder.cs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class UnitOfWorkParameterExpressionBuilder : IParameterExpressionBuilder
1212
private static readonly MethodInfo GetOrSetGlobalStateUnitOfWorkMethod =
1313
typeof(ResolverContextExtensions).GetMethods()
1414
.First(m => m.Name == nameof(ResolverContextExtensions.GetOrSetGlobalState))
15-
.MakeGenericMethod(typeof(UnitOfWork));
15+
.MakeGenericMethod(typeof(IUnitOfWork));
1616

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

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

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

31-
public bool CanHandle(ParameterInfo parameter) => typeof(UnitOfWork) == parameter.ParameterType
32-
|| typeof(IUnitOfWork) == parameter.ParameterType;
31+
public bool CanHandle(ParameterInfo parameter) =>
32+
typeof(IUnitOfWork) == parameter.ParameterType;
3333

3434
/*
3535
* Produces something like this: context.GetOrSetGlobalState(
36-
* nameof(UnitOfWork.UnitOfWork),
36+
* nameof(IUnitOfWork),
3737
* _ =>
3838
* context
39-
* .Service<UnitOfWork.UnitOfWork>()
39+
* .Service<IUnitOfWork>()
4040
* .WithPrefilledVersion(
4141
* context.GetGlobalState<long>(PrefillUnitOfWorkVersion)
4242
* ))!;
@@ -53,13 +53,14 @@ public Expression Build(ParameterExpressionBuilderContext builderContext)
5353
context,
5454
Expression.Constant(PrefillUnitOfWorkVersion)));
5555

56-
return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context, Expression.Constant(nameof(UnitOfWork)),
57-
Expression.Lambda<Func<string, UnitOfWork>>(
56+
return Expression.Call(null, GetOrSetGlobalStateUnitOfWorkMethod, context,
57+
Expression.Constant(nameof(IUnitOfWork)),
58+
Expression.Lambda<Func<string, IUnitOfWork>>(
5859
getNewUnitOfWork,
5960
Expression.Parameter(typeof(string))));
6061
}
6162

6263
public ArgumentKind Kind => ArgumentKind.Custom;
63-
public bool IsPure => true;
64+
public bool IsPure => false;
6465
public bool IsDefaultHandler => false;
65-
}
66+
}

src/Fluss.UnitTest/HotChocolate/UnitOfWorkParameterExpressionBuilderTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class UnitOfWorkParameterExpressionBuilderTest
1212
private readonly UnitOfWorkParameterExpressionBuilder _builder = new();
1313

1414
[Fact]
15-
public void CanHandle_ShouldReturnTrueForUnitOfWorkParameter()
15+
public void CanHandle_ShouldReturnFalseForUnitOfWorkParameter()
1616
{
1717
var parameter = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithUnitOfWork))!.GetParameters()[0];
18-
Assert.True(_builder.CanHandle(parameter));
18+
Assert.False(_builder.CanHandle(parameter));
1919
}
2020

2121
[Fact]
@@ -39,9 +39,9 @@ public void Kind_ShouldReturnCustom()
3939
}
4040

4141
[Fact]
42-
public void IsPure_ShouldReturnTrue()
42+
public void IsPure_ShouldReturnFalse()
4343
{
44-
Assert.True(_builder.IsPure);
44+
Assert.False(_builder.IsPure);
4545
}
4646

4747
[Fact]

0 commit comments

Comments
 (0)