Skip to content

Commit

Permalink
Experiment with Eff runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
rungwiroon committed Oct 23, 2024
1 parent 99fd061 commit 2cb4d1b
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
<PackageReference Update="FSharp.Core" Version="8.0.400" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ public static IActionResult RunToResult<T>(
public static async Task<IActionResult> RunToResultAsync<T>(
this Eff<T> eff,
HttpStatusCode successStatusCode = HttpStatusCode.OK,
ILogger? logger = default)
ILogger? logger = default,
CancellationToken cancellationToken = default)
{
var fin = await eff.RunAsync();
var fin = await eff.RunAsync(EnvIO.New(token: cancellationToken));

return
fin.MatchToResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using Codehard.Functional.EntityFramework.Tests.Entities;
using Codehard.Infrastructure.EntityFramework.Extensions;
using static Codehard.Functional.EntityFramework.Extensions.QueryablePrelude;

namespace Codehard.Functional.EntityFramework.Tests;

Expand All @@ -18,7 +19,7 @@ private static SqliteConnection CreateInMemoryDatabase()
}

[Fact]
public async Task ToListEffRt_ShouldReturnList()
public async Task ToListEff_ShouldReturnList()
{
// Arrange
var assembly = Assembly.GetExecutingAssembly();
Expand All @@ -29,6 +30,7 @@ public async Task ToListEffRt_ShouldReturnList()
await using var context = new TestDbContext(
options,
builder => builder.ApplyConfigurationsFromAssemblyFor<TestDbContext>(assembly));

await context.Database.EnsureCreatedAsync();

var entityId = Guid.NewGuid();
Expand Down Expand Up @@ -56,4 +58,44 @@ public async Task ToListEffRt_ShouldReturnList()
Assert.NotNull(resultEntity);
Assert.Single(resultEntity);
}

[Fact]
public async Task FilterRtAndToListEff_ShouldReturnList()
{
// Arrange
var assembly = Assembly.GetExecutingAssembly();
var options = new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(CreateInMemoryDatabase())
.Options;

await using var context = new TestDbContext(
options,
builder => builder.ApplyConfigurationsFromAssemblyFor<TestDbContext>(assembly));

await context.Database.EnsureCreatedAsync();

var entityId = Guid.NewGuid();
var entity = new EntityA
{
Id = entityId,
};

context.As.Add(entity);
await context.SaveChangesAsync();

// Act
var workflow =
from @as in From<EntityA>()
from list in
@as.Where(a => a.Id == entityId)
.ToListEff()
select list;

// Assert
var listResult = await workflow.RunAsync(context);
var resultEntity = listResult.ThrowIfFail();

Assert.NotNull(resultEntity);
Assert.Single(resultEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;
using LanguageExt;

using static LanguageExt.Prelude;

// ReSharper disable once CheckNamespace
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Linq.Expressions;
using LanguageExt;
using Microsoft.EntityFrameworkCore;

using Codehard.Common.DomainModel;

using static LanguageExt.Prelude;

namespace Codehard.Functional.EntityFramework.Extensions;

public static class QueryablePrelude
{
/// <summary>
/// Asynchronously converts a sequence to a list within an Eff monad.
/// </summary>
/// <typeparam name="T">The type of the elements of the source sequence.</typeparam>
/// <returns>
/// 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.
/// </returns>
public static Eff<IQueryable<T>, List<T>> ToListEff<T>()
{
return
LanguageExt.Eff<IQueryable<T>, List<T>>.LiftIO(
static source =>
liftIO(env => source.ToListAsync(env.Token)));
}

/// <summary>
/// Asynchronously converts a sequence to an array within an Eff monad.
/// </summary>
/// <typeparam name="T">The type of the elements of the source sequence.</typeparam>
/// <returns>
/// An Eff monad that represents the asynchronous operation. The Eff monad wraps an array that contains elements from the input sequence.
/// </returns>
public static Eff<IQueryable<T>, T[]> ToArrayEff<T>()
{
return
LanguageExt.Eff<IQueryable<T>, T[]>.LiftIO(
static source =>
liftIO(env => source.ToArrayAsync(env.Token)));
}

public static Eff<DbContext, IQueryable<T>> From<T>()
where T : class
{
return
LanguageExt.Eff<DbContext, IQueryable<T>>.Lift(
dbContext => dbContext.Set<T>().AsQueryable());
}

public static Eff<DbSet<TEntity>, Option<TEntity>> FindByKey<TEntity, TKey>(
TKey key)
where TEntity : class, IEntity<TKey>
where TKey : struct
{
return
LanguageExt.Eff<DbSet<TEntity>, Option<TEntity>>.LiftIO(
source =>
liftIO(async env => Optional(await source.FindAsync(
new object[] { key },
cancellationToken: env.Token))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="FSharp.Core" Version="8.0.400" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
<PackageReference Include="Marten" Version="7.26.3" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.8.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
<PackageReference Include="MediatR" Version="12.4.0" />
</ItemGroup>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,34 @@ public void WhenGuardNoneOnNullValue_ShouldGoToFailCase()
// Assert
Assert.False(fin.IsSucc);
}

[Fact]
public async Task WhenGuardNoneOnSomeValueAsync_ShouldStayOnSuccCase()
{
// Act
var eff =
EffOption(() => ValueTask.FromResult((int?)1))
.GuardNotNone("There is something wrong");

var fin = await eff.RunAsync();

// Assert
var a = fin.ThrowIfFail();

Assert.Equal(1, a);
}

[Fact]
public async Task WhenGuardNoneOnNullValueAsync_ShouldGoToFailCase()
{
// Act
var eff =
EffOption(() => ValueTask.FromResult((int?)null))
.GuardNotNone("There is no value");

var fin = await eff.RunAsync();

// Assert
Assert.False(fin.IsSucc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-24" />
<PackageReference Include="LanguageExt.Core" Version="5.0.0-beta-27" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void WhenUseApplyConfigurationsFromAssemblyForSpecificContext_ShouldApply

// +2 because of the Money type and the Nullable Money type
Assert.Equal(expectedEntityTypes + 3, actualEntityTypes);

return;

static SqliteConnection CreateInMemoryDatabase()
{
Expand Down

0 comments on commit 2cb4d1b

Please sign in to comment.