Skip to content

Commit

Permalink
Merge pull request #248 from Avanade/feature/structV8
Browse files Browse the repository at this point in the history
feat EntityFrameworkRepository extensions
  • Loading branch information
lucianareginalino authored Jun 27, 2024
2 parents 6e01cb8 + a2262e0 commit ccb5930
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 3 deletions.
34 changes: 34 additions & 0 deletions src/Liquid.Repository.EntityFramework/EntityFrameworkRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,39 @@ public async Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool
return returnValue;
}

/// <summary>
/// Gets the list of entities that matches the where clause,
/// including the related entities.
/// </summary>
/// <param name="whereClause"> The where clause.</param>
/// <param name="include"> The include clause.</param>
/// <returns></returns>
public IEnumerable<TEntity> WhereInclude(Expression<Func<TEntity, bool>> whereClause, Expression<Func<TEntity, object>> include = null)
{
var result = _queryableReadOnly.Where(whereClause).Include(include);
return result.AsEnumerable();
}

/// <summary>
/// Gets the list of entities that matches the where clause,
/// including the related entities.
/// </summary>
/// <param name="whereClause">where clause.</param>
/// <param name="includes">Entities to include.</param>
/// <returns></returns>
public IEnumerable<TEntity> WhereInclude(Expression<Func<TEntity, bool>> whereClause, string[] includes)
{
var query = _queryableReadOnly.Where(whereClause);

if (includes != null)
{
foreach (var include in includes)
{
query = query.Include(include);
}
}

return query.AsEnumerable();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Liquid.Core.Entities;
using Liquid.Core.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;

namespace Liquid.Repository.EntityFramework.Extensions
{
/// <summary>
/// Extension methods for <see cref="ILiquidRepository{TEntity, TIdentifier}"/>.
/// </summary>
[ExcludeFromCodeCoverage]
public static class ILiquidRepositoryExtensions
{
/// <summary>
/// Gets the list of entities that matches the where clause,
/// including the related entities.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TIdentifier"></typeparam>
/// <typeparam name="TContext"></typeparam>
/// <param name="repository"></param>
/// <param name="whereClause"> The where clause.</param>
/// <param name="include"> The include clause.</param>
/// <returns></returns>
public static ILiquidRepository<TEntity, TIdentifier> WhereInclude<TEntity, TIdentifier, TContext>(this ILiquidRepository<TEntity, TIdentifier> repository
, Expression<Func<TEntity, bool>> whereClause, Expression<Func<TEntity, object>> include = null)
where TEntity : LiquidEntity<TIdentifier>, new() where TContext : DbContext
{
var EfRepository = repository as EntityFrameworkRepository<TEntity, TIdentifier,TContext>;

EfRepository.WhereInclude(whereClause, include);

return repository;
}

/// <summary>
/// Gets the list of entities that matches the where clause,
/// including the related entities.
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TIdentifier"></typeparam>
/// <typeparam name="TContext"></typeparam>
/// <param name="repository"></param>
/// <param name="whereClause">where clause.</param>
/// <param name="includes">Entities to include.</param>
/// <returns></returns>
public static ILiquidRepository<TEntity, TIdentifier> WhereInclude<TEntity, TIdentifier, TContext>(this ILiquidRepository<TEntity, TIdentifier> repository
, Expression<Func<TEntity, bool>> whereClause, string[] includes)
where TEntity : LiquidEntity<TIdentifier>, new() where TContext : DbContext
{
var EfRepository = repository as EntityFrameworkRepository<TEntity, TIdentifier, TContext>;

EfRepository.WhereInclude(whereClause, includes);

return repository;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<PackageId>Liquid.Repository.EntityFramework</PackageId>
<Version>8.0.0-alpha-01</Version>
<Version>8.0.0-beta-02</Version>
<Authors>Avanade Brazil</Authors>
<Company>Avanade Inc.</Company>
<Product>Liquid - Modern Application Framework</Product>
Expand All @@ -16,12 +16,10 @@
<DebugType>Full</DebugType>
<NoWarn>1701;1702;1584;1658</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Liquid.Core" Version="8.0.0-alpha-04" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.6" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\logo.png">
<Pack>True</Pack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ public int MockId
/// </value>
public DateTime CreatedDate { get; set; }


public MockSubEntity SubEntity { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Liquid.Repository.EntityFramework.Tests.Entities
{
public class MockSubEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,35 @@ public void EntityFrameworkRepository_WhenCreated_DataContextIsValid()
Assert.NotNull(entityFrameworkDataContext);
Assert.IsAssignableFrom<IEntityFrameworkDataContext<MockDbContext>>(entityFrameworkDataContext);
}

[Fact]
public void WhereInclude_WhenCalled_ReturnsExpected()
{
//Arrange
var mockRepository = GenerateMockRepository();

//Act
var result = mockRepository.WhereInclude(o => o.MockTitle.Equals("TITLE_002"), new string[] {"SubEntity" });

//Assert
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.True(result.All(o => o.MockTitle.Equals("TITLE_002")));
}

[Fact]
public void WhereInclude_WhenCalledExpressionOverload_ReturnsExpected()
{
//Arrange
var mockRepository = GenerateMockRepository();

//Act
var result = mockRepository.WhereInclude(o => o.MockTitle.Equals("TITLE_002"), o => o.SubEntity);

//Assert
Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.True(result.All(o => o.MockTitle.Equals("TITLE_002")));
}
}
}

0 comments on commit ccb5930

Please sign in to comment.