Skip to content

Commit

Permalink
Merge pull request #13 from furkandeveloper/feature/specification-pat…
Browse files Browse the repository at this point in the history
…tern

✨ [FEATURE] Ardalis.Specification library has been supported
  • Loading branch information
furkandeveloper authored Jul 3, 2022
2 parents fb5c794 + 78483f0 commit cde41cc
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 16 deletions.
15 changes: 15 additions & 0 deletions EasyRepository.EFCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyRepository.Sample", "sa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EasyRepository.EFCore.Abstractions", "src\EasyRepository.EFCore.Abstractions\EasyRepository.EFCore.Abstractions.csproj", "{F3046DA7-D0EA-4A6E-A633-969B13E4EB5B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EasyRepository.EFCore.Ardalis.Specification", "src\EasyRepository.EFCore.Ardalis.Specification\EasyRepository.EFCore.Ardalis.Specification.csproj", "{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -66,6 +68,18 @@ Global
{F3046DA7-D0EA-4A6E-A633-969B13E4EB5B}.Release|x64.Build.0 = Release|Any CPU
{F3046DA7-D0EA-4A6E-A633-969B13E4EB5B}.Release|x86.ActiveCfg = Release|Any CPU
{F3046DA7-D0EA-4A6E-A633-969B13E4EB5B}.Release|x86.Build.0 = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|x64.ActiveCfg = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|x64.Build.0 = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|x86.ActiveCfg = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Debug|x86.Build.0 = Debug|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|Any CPU.Build.0 = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|x64.ActiveCfg = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|x64.Build.0 = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|x86.ActiveCfg = Release|Any CPU
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -74,6 +88,7 @@ Global
{730B0EE2-7048-4F0E-9DA0-7F0DDED22707} = {6ECD69C4-213C-400A-8EFE-DB1B5E33DC29}
{84940E1B-6A42-4485-82DC-4EE105B637AA} = {1670C5F3-D8A7-42DB-A377-5405F1B4BCE6}
{F3046DA7-D0EA-4A6E-A633-969B13E4EB5B} = {6ECD69C4-213C-400A-8EFE-DB1B5E33DC29}
{2DBA6C0F-2C4B-4516-A7C6-AF322B37834B} = {6ECD69C4-213C-400A-8EFE-DB1B5E33DC29}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {82B432B5-EED3-42E9-AFEC-D899479E4CC3}
Expand Down
14 changes: 13 additions & 1 deletion sample/EasyRepository.Sample/Controllers/AuthorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyRepository.EFCore.Ardalis.Specification;
using EasyRepository.EFCore.Generic;
using EasyRepository.Sample.Specs;

namespace EasyRepository.Sample.Controllers
{
Expand All @@ -23,6 +25,16 @@ public AuthorController(IRepository repository, IUnitOfWork unitOfWork)
_unitOfWork = unitOfWork;
}

[HttpGet(Name = "FilterAuthor")]
public async Task<IActionResult> FilterAuthorAsync([FromQuery]string name)
{
var queryable = _unitOfWork.Repository.GetQueryable<Author>();
//var spec = new AuthorByNameSpec(name);
var spec = new AuthorOrderByNameSpec(name);
var data = SpecificationConverter.Convert(queryable, spec);
return Ok(data.ToList());
}

[HttpPost]
public async Task<IActionResult> AddAuthorAsync([FromBody] AuthorRequestDto dto)
{
Expand All @@ -36,7 +48,7 @@ public async Task<IActionResult> AddAuthorAsync([FromBody] AuthorRequestDto dto)
{
Title = "Book 123",
TotalPage = 124,
AuthorId = Guid.NewGuid()
AuthorId = entity.Id
});

await _unitOfWork.Repository.CompleteAsync();
Expand Down
1 change: 1 addition & 0 deletions sample/EasyRepository.Sample/EasyRepository.Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EasyRepository.EFCore.Ardalis.Specification\EasyRepository.EFCore.Ardalis.Specification.csproj" />
<ProjectReference Include="..\..\src\EasyRepository.EFCore.Generic\EasyRepository.EFCore.Generic.csproj" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions sample/EasyRepository.Sample/Entities/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace EasyRepository.Sample.Entities
Expand All @@ -12,6 +13,7 @@ public class Author : EasyBaseEntity<Guid>

public string Surname { get; set; }

[JsonIgnore]
public virtual ICollection<Book> Books { get; set; }
}
}
17 changes: 17 additions & 0 deletions sample/EasyRepository.Sample/Specs/AuthorByNameSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Ardalis.Specification;
using EasyRepository.Sample.Entities;

namespace EasyRepository.Sample.Specs
{
/// <summary>
/// Author By Name specification
/// </summary>
public sealed class AuthorByNameSpec : Specification<Author>
{
/// <inheritdoc />
public AuthorByNameSpec(string name)
{
Query.Where(c => c.Name == name);
}
}
}
36 changes: 36 additions & 0 deletions sample/EasyRepository.Sample/Specs/AuthorOrderByNameSpec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Ardalis.Specification;
using EasyRepository.Sample.Entities;

namespace EasyRepository.Sample.Specs
{
/// <summary>
/// Order by author name specification
/// </summary>
public sealed class AuthorOrderByNameSpec : Specification<Author>
{
/// <inheritdoc />
public AuthorOrderByNameSpec(string name)
{
Query.ApplyBaseRules().ApplyByName(name).OrderBy(o => o.Name);
}
}

public static class AuthorSpecification
{
public static ISpecificationBuilder<Author> ApplyBaseRules(
this ISpecificationBuilder<Author> specificationBuilder)
{
specificationBuilder.Include(x => x.Books);

return specificationBuilder;
}

public static ISpecificationBuilder<Author> ApplyByName(
this ISpecificationBuilder<Author> specificationBuilder, string name)
{
specificationBuilder.Where(a => a.Name.Contains(name));

return specificationBuilder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\common.props"/>
<PropertyGroup>
<PackageIcon>folders.png</PackageIcon>
<PackageIconUrl/>
</PropertyGroup>
<ItemGroup>
<None Include="folders.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Ardalis.Specification.EntityFrameworkCore" Version="6.1.0"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Linq;
using Ardalis.Specification;
using Ardalis.Specification.EntityFrameworkCore;

namespace EasyRepository.EFCore.Ardalis.Specification;

/// <summary>
/// Specification Builder
/// </summary>
public static class SpecificationConverter
{
/// <summary>
/// This method convert specification object to queryable object.
/// </summary>
/// <param name="entity">
/// Entity
/// </param>
/// <param name="specification">
/// Specification object
/// </param>
/// <typeparam name="TEntity">
/// Entity
/// </typeparam>
/// <returns>
/// <see cref="IQueryable{TEntity}"/>
/// </returns>
public static IQueryable<TEntity> Convert<TEntity>(IQueryable<TEntity> entity, ISpecification<TEntity> specification) where TEntity : class, new()
{
return SpecificationEvaluator.Default.GetQuery(entity, specification);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\common.props" />
<PropertyGroup>
<PackageIcon>folders.png</PackageIcon>
<PackageIconUrl />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EasyRepository.EFCore.Abstractions\EasyRepository.EFCore.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="folders.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

<Import Project="..\..\common.props"/>
<PropertyGroup>
<PackageIcon>folders.png</PackageIcon>
<PackageIconUrl/>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\EasyRepository.EFCore.Abstractions\EasyRepository.EFCore.Abstractions.csproj"/>
</ItemGroup>
<ItemGroup>
<None Include="folders.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>

0 comments on commit cde41cc

Please sign in to comment.