Skip to content

Commit 2cb4d1b

Browse files
committed
Experiment with Eff runtime
1 parent 99fd061 commit 2cb4d1b

File tree

14 files changed

+148
-47
lines changed

14 files changed

+148
-47
lines changed

src/Codehard.Functional/CodeHard.Function.FSharp.Tests.Types/CodeHard.Functional.FSharp.Tests.Types.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
14+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1515
<PackageReference Update="FSharp.Core" Version="8.0.400" />
1616
</ItemGroup>
1717

src/Codehard.Functional/Codehard.Functional.AspNetCore/ControllerExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ public static IActionResult RunToResult<T>(
111111
public static async Task<IActionResult> RunToResultAsync<T>(
112112
this Eff<T> eff,
113113
HttpStatusCode successStatusCode = HttpStatusCode.OK,
114-
ILogger? logger = default)
114+
ILogger? logger = default,
115+
CancellationToken cancellationToken = default)
115116
{
116-
var fin = await eff.RunAsync();
117+
var fin = await eff.RunAsync(EnvIO.New(token: cancellationToken));
117118

118119
return
119120
fin.MatchToResult(

src/Codehard.Functional/Codehard.Functional.EntityFramework.Tests/Codehard.Functional.EntityFramework.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
12+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1313
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
1414
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
1515
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />

src/Codehard.Functional/Codehard.Functional.EntityFramework.Tests/QueryableExtensionsTests.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using Codehard.Functional.EntityFramework.Tests.Entities;
77
using Codehard.Infrastructure.EntityFramework.Extensions;
8+
using static Codehard.Functional.EntityFramework.Extensions.QueryablePrelude;
89

910
namespace Codehard.Functional.EntityFramework.Tests;
1011

@@ -18,7 +19,7 @@ private static SqliteConnection CreateInMemoryDatabase()
1819
}
1920

2021
[Fact]
21-
public async Task ToListEffRt_ShouldReturnList()
22+
public async Task ToListEff_ShouldReturnList()
2223
{
2324
// Arrange
2425
var assembly = Assembly.GetExecutingAssembly();
@@ -29,6 +30,7 @@ public async Task ToListEffRt_ShouldReturnList()
2930
await using var context = new TestDbContext(
3031
options,
3132
builder => builder.ApplyConfigurationsFromAssemblyFor<TestDbContext>(assembly));
33+
3234
await context.Database.EnsureCreatedAsync();
3335

3436
var entityId = Guid.NewGuid();
@@ -56,4 +58,44 @@ public async Task ToListEffRt_ShouldReturnList()
5658
Assert.NotNull(resultEntity);
5759
Assert.Single(resultEntity);
5860
}
61+
62+
[Fact]
63+
public async Task FilterRtAndToListEff_ShouldReturnList()
64+
{
65+
// Arrange
66+
var assembly = Assembly.GetExecutingAssembly();
67+
var options = new DbContextOptionsBuilder<TestDbContext>()
68+
.UseSqlite(CreateInMemoryDatabase())
69+
.Options;
70+
71+
await using var context = new TestDbContext(
72+
options,
73+
builder => builder.ApplyConfigurationsFromAssemblyFor<TestDbContext>(assembly));
74+
75+
await context.Database.EnsureCreatedAsync();
76+
77+
var entityId = Guid.NewGuid();
78+
var entity = new EntityA
79+
{
80+
Id = entityId,
81+
};
82+
83+
context.As.Add(entity);
84+
await context.SaveChangesAsync();
85+
86+
// Act
87+
var workflow =
88+
from @as in From<EntityA>()
89+
from list in
90+
@as.Where(a => a.Id == entityId)
91+
.ToListEff()
92+
select list;
93+
94+
// Assert
95+
var listResult = await workflow.RunAsync(context);
96+
var resultEntity = listResult.ThrowIfFail();
97+
98+
Assert.NotNull(resultEntity);
99+
Assert.Single(resultEntity);
100+
}
59101
}

src/Codehard.Functional/Codehard.Functional.EntityFramework/Codehard.Functional.EntityFramework.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
17+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1818
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
1919
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
2020
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />

src/Codehard.Functional/Codehard.Functional.EntityFramework/Extensions/QueryableExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Linq.Expressions;
22
using LanguageExt;
3-
43
using static LanguageExt.Prelude;
54

65
// ReSharper disable once CheckNamespace
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System.Linq.Expressions;
2+
using LanguageExt;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
using Codehard.Common.DomainModel;
6+
7+
using static LanguageExt.Prelude;
8+
9+
namespace Codehard.Functional.EntityFramework.Extensions;
10+
11+
public static class QueryablePrelude
12+
{
13+
/// <summary>
14+
/// Asynchronously converts a sequence to a list within an Eff monad.
15+
/// </summary>
16+
/// <typeparam name="T">The type of the elements of the source sequence.</typeparam>
17+
/// <returns>
18+
/// An Eff monad that represents the asynchronous operation. The Eff monad wraps a <see cref="System.Collections.Generic.List{T}"/> that contains elements from the input sequence.
19+
/// </returns>
20+
public static Eff<IQueryable<T>, List<T>> ToListEff<T>()
21+
{
22+
return
23+
LanguageExt.Eff<IQueryable<T>, List<T>>.LiftIO(
24+
static source =>
25+
liftIO(env => source.ToListAsync(env.Token)));
26+
}
27+
28+
/// <summary>
29+
/// Asynchronously converts a sequence to an array within an Eff monad.
30+
/// </summary>
31+
/// <typeparam name="T">The type of the elements of the source sequence.</typeparam>
32+
/// <returns>
33+
/// An Eff monad that represents the asynchronous operation. The Eff monad wraps an array that contains elements from the input sequence.
34+
/// </returns>
35+
public static Eff<IQueryable<T>, T[]> ToArrayEff<T>()
36+
{
37+
return
38+
LanguageExt.Eff<IQueryable<T>, T[]>.LiftIO(
39+
static source =>
40+
liftIO(env => source.ToArrayAsync(env.Token)));
41+
}
42+
43+
public static Eff<DbContext, IQueryable<T>> From<T>()
44+
where T : class
45+
{
46+
return
47+
LanguageExt.Eff<DbContext, IQueryable<T>>.Lift(
48+
dbContext => dbContext.Set<T>().AsQueryable());
49+
}
50+
51+
public static Eff<DbSet<TEntity>, Option<TEntity>> FindByKey<TEntity, TKey>(
52+
TKey key)
53+
where TEntity : class, IEntity<TKey>
54+
where TKey : struct
55+
{
56+
return
57+
LanguageExt.Eff<DbSet<TEntity>, Option<TEntity>>.LiftIO(
58+
source =>
59+
liftIO(async env => Optional(await source.FindAsync(
60+
new object[] { key },
61+
cancellationToken: env.Token))));
62+
}
63+
}

src/Codehard.Functional/Codehard.Functional.FSharp/Codehard.Functional.FSharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<ItemGroup>
1717
<PackageReference Include="FSharp.Core" Version="8.0.400" />
18-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
18+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1919
</ItemGroup>
2020

2121
</Project>

src/Codehard.Functional/Codehard.Functional.Marten/Codehard.Functional.Marten.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</PropertyGroup>
1414

1515
<ItemGroup>
16-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
16+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1717
<PackageReference Include="Marten" Version="7.26.3" />
1818
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
1919
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />

src/Codehard.Functional/Codehard.Functional.MediatR/Codehard.Functional.MediatR.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
17+
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
1818
<PackageReference Include="MediatR" Version="12.4.0" />
1919
</ItemGroup>
2020

0 commit comments

Comments
 (0)