diff --git a/.editorconfig b/.editorconfig index 0ee58191..147fc4ef 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,9 +5,22 @@ # All files [*] indent_style = space + +# Standard properties +end_of_line = crlf + +# XML project files +[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj}] +indent_size = 2 + +# XML config files +[*.{props,targets,ruleset,config,nuspec,resx,vsixmanifest,vsct}] +indent_size = 2 + # Code files [*.{cs,csx,vb,vbx}] -indent_size =2 +indent_size = 4 +tab_width = 4 insert_final_newline = true charset = utf-8-bom ############################### @@ -15,22 +28,22 @@ charset = utf-8-bom ############################### [*.{cs,vb}] # Organize usings -dotnet_sort_system_directives_first = true +dotnet_sort_system_directives_first = false # this. preferences -dotnet_style_qualification_for_field = false:silent -dotnet_style_qualification_for_property = false:silent -dotnet_style_qualification_for_method = false:silent -dotnet_style_qualification_for_event = false:silent +dotnet_style_qualification_for_field = false:warning +dotnet_style_qualification_for_property = false:warning +dotnet_style_qualification_for_method = false:warning +dotnet_style_qualification_for_event = false:warning # Language keywords vs BCL types preferences -dotnet_style_predefined_type_for_locals_parameters_members = true:silent -dotnet_style_predefined_type_for_member_access = true:silent +dotnet_style_predefined_type_for_locals_parameters_members = true:warning +dotnet_style_predefined_type_for_member_access = true:warning # Parentheses preferences dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent # Modifier preferences -dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent +dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning dotnet_style_readonly_field = true:suggestion # Expression-level preferences dotnet_style_object_initializer = true:suggestion @@ -38,35 +51,94 @@ dotnet_style_collection_initializer = true:suggestion dotnet_style_explicit_tuple_names = true:suggestion dotnet_style_null_propagation = true:suggestion dotnet_style_coalesce_expression = true:suggestion -dotnet_style_prefer_is_null_check_over_reference_equality_method = true:silent +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning dotnet_style_prefer_inferred_tuple_names = true:suggestion dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion -dotnet_style_prefer_auto_properties = true:silent -dotnet_style_prefer_conditional_expression_over_assignment = true:silent -dotnet_style_prefer_conditional_expression_over_return = true:silent -# Namespace preferences -csharp_style_namespace_declarations = file_scoped:warning +dotnet_style_prefer_auto_properties = true:suggestion +dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_return = true:suggestion ############################### # Naming Conventions # ############################### -# Style Definitions -dotnet_naming_style.pascal_case_style.capitalization = pascal_case -# Use PascalCase for constant fields -dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion -dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields -dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style -dotnet_naming_symbols.constant_fields.applicable_kinds = field -dotnet_naming_symbols.constant_fields.applicable_accessibilities = * -dotnet_naming_symbols.constant_fields.required_modifiers = const -tab_width=2 +# Non-private static fields are PascalCase +dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.severity = error +dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.symbols = non_private_static_fields +dotnet_naming_rule.non_private_static_fields_should_be_pascal_case.style = non_private_static_field_style +dotnet_naming_symbols.non_private_static_fields.applicable_kinds = field +dotnet_naming_symbols.non_private_static_fields.applicable_accessibilities = public, protected, internal, protected internal, private protected +dotnet_naming_symbols.non_private_static_fields.required_modifiers = static +dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case +# Constants are PascalCase +dotnet_naming_rule.constants_should_be_pascal_case.severity = error +dotnet_naming_rule.constants_should_be_pascal_case.symbols = constants +dotnet_naming_rule.constants_should_be_pascal_case.style = camel_case_underscore_style +dotnet_naming_symbols.constants.applicable_kinds = field, local +dotnet_naming_symbols.constants.required_modifiers = const +dotnet_naming_style.constant_style.capitalization = pascal_case +# Public fields are Pascal case +dotnet_naming_symbols.public_fields.applicable_kinds = field +dotnet_naming_symbols.public_fields.applicable_accessibilities = public +dotnet_naming_rule.public_fields_should_be_pascal_case.severity = error +dotnet_naming_rule.public_fields_should_be_pascal_case.symbols = public_fields +dotnet_naming_rule.public_fields_should_be_pascal_case.style = pascal_case +# Static fields are camelCase +dotnet_naming_rule.static_fields_should_be_camel_case.severity = error +dotnet_naming_rule.static_fields_should_be_camel_case.symbols = static_fields +dotnet_naming_rule.static_fields_should_be_camel_case.style = camel_case_style +dotnet_naming_symbols.static_fields.applicable_kinds = field +dotnet_naming_symbols.static_fields.required_modifiers = static +dotnet_naming_symbols.static_fields.required_modifiers = none +dotnet_naming_style.static_field_style.capitalization = camel_case +# Instance fields are camelCase and start with _ +dotnet_naming_rule.camel_case_for_private_internal_fields.severity = error +dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields +dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style +dotnet_naming_symbols.private_internal_fields.applicable_kinds = field +dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal +dotnet_naming_style.camel_case_underscore_style.required_prefix = _ +dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case +# Locals and parameters are camelCase +dotnet_naming_rule.locals_should_be_camel_case.severity = error +dotnet_naming_rule.locals_should_be_camel_case.symbols = locals_and_parameters +dotnet_naming_rule.locals_should_be_camel_case.style = camel_case_style +dotnet_naming_symbols.locals_and_parameters.applicable_kinds = parameter, local +dotnet_naming_style.camel_case_style.capitalization = camel_case +# Local functions are PascalCase +dotnet_naming_rule.local_functions_should_be_pascal_case.severity = error +dotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions +dotnet_naming_rule.local_functions_should_be_pascal_case.style = non_private_static_field_style +dotnet_naming_symbols.local_functions.applicable_kinds = local_function +dotnet_naming_style.local_function_style.capitalization = pascal_case +# Type Parameters +dotnet_naming_style.type_parameter_style.capitalization = pascal_case +dotnet_naming_style.type_parameter_style.required_prefix = T +dotnet_naming_rule.type_parameter_naming.symbols = type_parameter_symbol +dotnet_naming_rule.type_parameter_naming.style = type_parameter_style +dotnet_naming_rule.type_parameter_naming.severity = error +dotnet_naming_symbols.type_parameter_symbol.applicable_kinds = type_parameter +dotnet_naming_symbols.type_parameter_symbol.applicable_accessibilities = * +# By default, name items with PascalCase +dotnet_naming_rule.members_should_be_pascal_case.severity = error +dotnet_naming_rule.members_should_be_pascal_case.symbols = all_members +dotnet_naming_rule.members_should_be_pascal_case.style = non_private_static_field_style +dotnet_naming_symbols.all_members.applicable_kinds = * +dotnet_naming_style.pascal_case_style.capitalization = pascal_case +dotnet_style_operator_placement_when_wrapping = beginning_of_line +dotnet_style_prefer_simplified_boolean_expressions = true:suggestion +dotnet_style_prefer_compound_assignment = true:warning +dotnet_style_prefer_simplified_interpolation = true:warning +dotnet_style_namespace_match_folder = true:suggestion +dotnet_style_allow_multiple_blank_lines_experimental = true:silent +dotnet_style_allow_statement_immediately_after_block_experimental = true:silent +dotnet_code_quality_unused_parameters = non_public:warning ############################### # C# Coding Conventions # ############################### [*.cs] # var preferences -csharp_style_var_for_built_in_types = true:silent -csharp_style_var_when_type_is_apparent = true:silent -csharp_style_var_elsewhere = true:silent +csharp_style_var_for_built_in_types = true:warning +csharp_style_var_when_type_is_apparent = true:warning +csharp_style_var_elsewhere = true:suggestion # Expression-bodied members csharp_style_expression_bodied_methods = false:silent csharp_style_expression_bodied_constructors = false:silent @@ -87,7 +159,7 @@ csharp_prefer_braces = true:silent csharp_style_deconstructed_variable_declaration = true:suggestion csharp_prefer_simple_default_expression = true:suggestion csharp_style_pattern_local_over_anonymous_function = true:suggestion -csharp_style_inlined_variable_declaration = true:suggestion +csharp_style_inlined_variable_declaration = true:warning ############################### # C# Formatting Rules # ############################### @@ -118,9 +190,28 @@ csharp_space_between_method_call_empty_parameter_list_parentheses = false # Wrapping preferences csharp_preserve_single_line_statements = true csharp_preserve_single_line_blocks = true -############################### -# VB Coding Conventions # -############################### -[*.vb] -# Modifier preferences -visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion +csharp_using_directive_placement = outside_namespace:warning +csharp_style_namespace_declarations = file_scoped:warning +csharp_prefer_simple_using_statement = true:silent +csharp_style_expression_bodied_lambdas = true:silent +csharp_style_expression_bodied_local_functions = false:silent +csharp_style_prefer_null_check_over_type_check = true:suggestion +csharp_style_prefer_local_over_anonymous_function = true:suggestion +csharp_style_prefer_index_operator = true:suggestion +csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion +csharp_style_prefer_range_operator = true:suggestion +csharp_style_prefer_tuple_swap = true:suggestion +csharp_style_unused_value_assignment_preference = discard_variable:warning +csharp_style_unused_value_expression_statement_preference = discard_variable:silent +csharp_prefer_static_local_function = true:suggestion +csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent +csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent +csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent +csharp_style_prefer_switch_expression = true:suggestion +csharp_style_prefer_pattern_matching = true:silent +csharp_style_prefer_not_pattern = true:suggestion +csharp_style_prefer_extended_property_pattern = true:suggestion +csharp_style_prefer_method_group_conversion = true:silent +csharp_style_prefer_parameter_null_checking = true:suggestion +csharp_style_prefer_top_level_statements = true:silent +csharp_style_prefer_primary_constructors = true:suggestion diff --git a/.github/workflows/build-test-ef6.yml b/.github/workflows/build-test-ef6.yml index 053f6a95..c985e634 100644 --- a/.github/workflows/build-test-ef6.yml +++ b/.github/workflows/build-test-ef6.yml @@ -24,7 +24,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: 6.0.x + dotnet-version: 7.0.x - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.2 - name: Build with dotnet diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index fb5920b5..260fa202 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -25,8 +25,8 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: 6.0.x - - name: Build with dotnet 6 + dotnet-version: 7.0.x + - name: Build with dotnet 7 run: | dotnet build Specification/src/Ardalis.Specification/Ardalis.Specification.csproj --configuration Release dotnet build Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj --configuration Release diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9af6b4e0..e956c3ab 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -17,7 +17,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: 6.0.x + dotnet-version: 7.0.x # Publish - name: publish on version change diff --git a/.github/workflows/publishef6.yml b/.github/workflows/publishef6.yml index 907d83b8..d7cc7fcd 100644 --- a/.github/workflows/publishef6.yml +++ b/.github/workflows/publishef6.yml @@ -17,7 +17,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: 6.0.x + dotnet-version: 7.0.x - name: Setup MSBuild.exe uses: microsoft/setup-msbuild@v1.0.2 diff --git a/.github/workflows/publishefcore.yml b/.github/workflows/publishefcore.yml index 7d5d394e..1c38e166 100644 --- a/.github/workflows/publishefcore.yml +++ b/.github/workflows/publishefcore.yml @@ -17,7 +17,7 @@ jobs: - name: Setup dotnet uses: actions/setup-dotnet@v1 with: - dotnet-version: 6.0.x + dotnet-version: 7.0.x # Publish - name: publish on version change diff --git a/Ardalis.Specification.sln b/Ardalis.Specification.sln index 96d0d522..c70637d7 100644 --- a/Ardalis.Specification.sln +++ b/Ardalis.Specification.sln @@ -29,6 +29,7 @@ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_Solution Items", "_Solution Items", "{C443291A-7311-455F-9AC6-995EB29DD6D2}" ProjectSection(SolutionItems) = preProject .editorconfig = .editorconfig + .gitignore = .gitignore README.md = README.md EndProjectSection EndProject @@ -44,8 +45,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Specification.EntityFramewo EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Specification.EntityFramework6", "Specification.EntityFramework6", "{327AEBD6-C8A6-4851-BA42-632F8014CFC5}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Specification.EntityFrameworkCore.UnitTests", "Specification.EntityFrameworkCore\tests\Ardalis.Specification.EntityFrameworkCore.UnitTests\Ardalis.Specification.EntityFrameworkCore.UnitTests.csproj", "{53E4FFB4-CAC0-482D-B714-FA657C3244C9}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -88,10 +87,6 @@ Global {4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A}.Debug|Any CPU.Build.0 = Debug|Any CPU {4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A}.Release|Any CPU.ActiveCfg = Release|Any CPU {4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A}.Release|Any CPU.Build.0 = Release|Any CPU - {53E4FFB4-CAC0-482D-B714-FA657C3244C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {53E4FFB4-CAC0-482D-B714-FA657C3244C9}.Debug|Any CPU.Build.0 = Debug|Any CPU - {53E4FFB4-CAC0-482D-B714-FA657C3244C9}.Release|Any CPU.ActiveCfg = Release|Any CPU - {53E4FFB4-CAC0-482D-B714-FA657C3244C9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -106,7 +101,6 @@ Global {5AFD1454-E625-451D-A615-CEB7BB09AA65} = {B19F2F64-4B22-48C2-B2F8-7672F84F758D} {37EC09C7-702D-4539-B98D-F67B15E1E6CE} = {327AEBD6-C8A6-4851-BA42-632F8014CFC5} {4BEB4DC4-DE33-4DF1-8A2F-CE76C1D72A4A} = {327AEBD6-C8A6-4851-BA42-632F8014CFC5} - {53E4FFB4-CAC0-482D-B714-FA657C3244C9} = {B19F2F64-4B22-48C2-B2F8-7672F84F758D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {C153A625-42F7-49A7-B99A-6A78B4B866B2} diff --git a/Dockerfile b/Dockerfile index 31687362..9587d62b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build WORKDIR / COPY . ./ @@ -7,6 +7,6 @@ ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.8.0/wait RUN /bin/bash -c 'ls -la /wait; chmod +x /wait; ls -la /wait' # install the report generator tool -RUN dotnet tool install dotnet-reportgenerator-globaltool --version 4.8.7 --tool-path /tools +RUN dotnet tool install dotnet-reportgenerator-globaltool --version 5.1.23 --tool-path /tools -CMD /wait && dotnet test -f net6.0 Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj --logger trx --results-directory /var/temp /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura && mv /Specification/tests/Ardalis.Specification.UnitTests/coverage.net6.0.cobertura.xml /var/temp/coverage.unit.cobertura.xml && dotnet test Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj --logger trx --results-directory /var/temp /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura && mv /Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/coverage.cobertura.xml /var/temp/coverage.ef.integration.cobertura.xml && tools/reportgenerator -reports:/var/temp/coverage.*.cobertura.xml -targetdir:/var/temp/coverage -reporttypes:HtmlInline_AzurePipelines\;HTMLChart\;Cobertura \ No newline at end of file +CMD /wait && dotnet test -f net7.0 Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj --collect:"XPlat Code Coverage" && dotnet test -f net7.0 Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj --collect:"XPlat Code Coverage" && tools/reportgenerator -reports:Specification*/**/coverage.cobertura.xml -targetdir:/var/temp/coverage -reporttypes:HtmlInline_AzurePipelines\;HTMLChart\;Cobertura diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Ardalis.Specification.EntityFramework6.csproj b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Ardalis.Specification.EntityFramework6.csproj index 18c7d61b..6333bf0a 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Ardalis.Specification.EntityFramework6.csproj +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Ardalis.Specification.EntityFramework6.csproj @@ -2,33 +2,36 @@ net472 + 11.0 Library false + + Ardalis.Specification.EntityFramework6 Ardalis.Specification.EntityFramework6 Ardalis.Specification.EntityFramework6 true Steve Smith (@ardalis); Fati Iseni (@fiseni); Scott DePouw Ardalis.com - https://github.com/ardalis/specification EF6 plugin package to Ardalis.Specification containing EF6 evaluator and abstract repository. EF6 plugin package to Ardalis.Specification containing EF6 evaluator and abstract repository. + https://github.com/ardalis/specification https://github.com/ardalis/specification spec;specification;repository;ddd;ef;ef6;entity framework + icon.png 7.0.0 - * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 - * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 - * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 - * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 - * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 - * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 - * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 - * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 - * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 - * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 - * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 - Ardalis.Specification.EntityFramework6 - icon.png + * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 + * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 + * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 + * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 + * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 + * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 + * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 + * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 + * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 + * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 + * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 + true true $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb @@ -40,13 +43,16 @@ - + - + - + + + 1701;1702;1591;1573;1712 + diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/AsNoTrackingEvaluator.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/AsNoTrackingEvaluator.cs index ffccb719..5f5b999c 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/AsNoTrackingEvaluator.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/AsNoTrackingEvaluator.cs @@ -1,10 +1,10 @@ using System.Data.Entity; using System.Linq; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public class AsNoTrackingEvaluator : IEvaluator { - public class AsNoTrackingEvaluator : IEvaluator - { private AsNoTrackingEvaluator() { } public static AsNoTrackingEvaluator Instance { get; } = new AsNoTrackingEvaluator(); @@ -12,12 +12,11 @@ private AsNoTrackingEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.AsNoTracking) - { - query = query.AsNoTracking(); - } + if (specification.AsNoTracking) + { + query = query.AsNoTracking(); + } - return query; + return query; } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/IncludeEvaluator.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/IncludeEvaluator.cs index 50734215..a8f63885 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/IncludeEvaluator.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/IncludeEvaluator.cs @@ -1,10 +1,10 @@ using System.Data.Entity; using System.Linq; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public class IncludeEvaluator : IEvaluator { - public class IncludeEvaluator : IEvaluator - { private IncludeEvaluator() { } public static IncludeEvaluator Instance { get; } = new IncludeEvaluator(); @@ -12,24 +12,23 @@ private IncludeEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - foreach (var includeString in specification.IncludeStrings) - { - query = query.Include(includeString); - } - - foreach (var includeInfo in specification.IncludeExpressions) - { - if (includeInfo.Type == IncludeTypeEnum.Include) + foreach (var includeString in specification.IncludeStrings) { - query = query.Include(includeInfo); + query = query.Include(includeString); } - else if (includeInfo.Type == IncludeTypeEnum.ThenInclude) + + foreach (var includeInfo in specification.IncludeExpressions) { - query = query.ThenInclude(includeInfo); + if (includeInfo.Type == IncludeTypeEnum.Include) + { + query = query.Include(includeInfo); + } + else if (includeInfo.Type == IncludeTypeEnum.ThenInclude) + { + query = query.ThenInclude(includeInfo); + } } - } - return query; + return query; } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/OrderEvaluator.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/OrderEvaluator.cs index 5f4a328e..e75eb4f5 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/OrderEvaluator.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/OrderEvaluator.cs @@ -2,10 +2,10 @@ using System.Linq; using System.Linq.Expressions; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public class OrderEvaluator : IEvaluator, IInMemoryEvaluator { - public class OrderEvaluator : IEvaluator, IInMemoryEvaluator - { private OrderEvaluator() { } public static OrderEvaluator Instance { get; } = new OrderEvaluator(); @@ -13,91 +13,90 @@ private OrderEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.OrderExpressions != null) - { - if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy - || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + if (specification.OrderExpressions != null) { - throw new DuplicateOrderChainException(); - } + if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy + || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + { + throw new DuplicateOrderChainException(); + } - IOrderedQueryable orderedQuery = null; - foreach (var orderExpression in specification.OrderExpressions) - { - if (orderExpression.OrderType == OrderTypeEnum.OrderBy) - { - orderedQuery = Queryable.OrderBy((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) - { - orderedQuery = Queryable.OrderByDescending((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) - { - orderedQuery = Queryable.ThenBy((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) - { - orderedQuery = Queryable.ThenByDescending((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - } + IOrderedQueryable orderedQuery = null; + foreach (var orderExpression in specification.OrderExpressions) + { + if (orderExpression.OrderType == OrderTypeEnum.OrderBy) + { + orderedQuery = Queryable.OrderBy((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) + { + orderedQuery = Queryable.OrderByDescending((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) + { + orderedQuery = Queryable.ThenBy((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) + { + orderedQuery = Queryable.ThenByDescending((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + } - if (orderedQuery != null) - { - query = orderedQuery; + if (orderedQuery != null) + { + query = orderedQuery; + } } - } - return query; + return query; } public IEnumerable Evaluate(IEnumerable query, ISpecification specification) { - if (specification.OrderExpressions != null) - { - if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy - || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + if (specification.OrderExpressions != null) { - throw new DuplicateOrderChainException(); - } + if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy + || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + { + throw new DuplicateOrderChainException(); + } - IOrderedEnumerable orderedQuery = null; - foreach (var orderExpression in specification.OrderExpressions) - { - if (orderExpression.OrderType == OrderTypeEnum.OrderBy) - { - orderedQuery = Queryable.OrderBy((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) - { - orderedQuery = Queryable.OrderByDescending((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) - { - orderedQuery = Queryable.ThenBy((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) - { - orderedQuery = Queryable.ThenByDescending((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); - } - } + IOrderedEnumerable orderedQuery = null; + foreach (var orderExpression in specification.OrderExpressions) + { + if (orderExpression.OrderType == OrderTypeEnum.OrderBy) + { + orderedQuery = Queryable.OrderBy((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) + { + orderedQuery = Queryable.OrderByDescending((dynamic)query, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) + { + orderedQuery = Queryable.ThenBy((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) + { + orderedQuery = Queryable.ThenByDescending((dynamic)orderedQuery, (dynamic)RemoveConvert(orderExpression.KeySelector)); + } + } - if (orderedQuery != null) - { - query = orderedQuery; + if (orderedQuery != null) + { + query = orderedQuery; + } } - } - return query; + return query; } private LambdaExpression RemoveConvert(LambdaExpression source) { - var body = source.Body; - while (body.NodeType == ExpressionType.Convert) - body = ((UnaryExpression)body).Operand; + var body = source.Body; + while (body.NodeType == ExpressionType.Convert) + body = ((UnaryExpression)body).Operand; - return Expression.Lambda(body, source.Parameters); + return Expression.Lambda(body, source.Parameters); } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SearchEvaluator.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SearchEvaluator.cs index 67d8ddda..20079770 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SearchEvaluator.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SearchEvaluator.cs @@ -1,9 +1,9 @@ using System.Linq; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public class SearchEvaluator : IEvaluator { - public class SearchEvaluator : IEvaluator - { private SearchEvaluator() { } public static SearchEvaluator Instance { get; } = new SearchEvaluator(); @@ -11,12 +11,11 @@ private SearchEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - foreach (var searchCriteria in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) - { - query = query.Search(searchCriteria); - } + foreach (var searchCriteria in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) + { + query = query.Search(searchCriteria); + } - return query; + return query; } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SpecificationEvaluator.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SpecificationEvaluator.cs index 75d9ebf7..82b8d6e8 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SpecificationEvaluator.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Evaluators/SpecificationEvaluator.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; using System.Linq; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +/// +public class SpecificationEvaluator : ISpecificationEvaluator { - /// - public class SpecificationEvaluator : ISpecificationEvaluator - { // Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided evaluators. public static SpecificationEvaluator Default { get; } = new SpecificationEvaluator(); @@ -14,48 +14,47 @@ public class SpecificationEvaluator : ISpecificationEvaluator public SpecificationEvaluator() { - this.Evaluators.AddRange(new IEvaluator[] - { - WhereEvaluator.Instance, - SearchEvaluator.Instance, - IncludeEvaluator.Instance, - OrderEvaluator.Instance, - PaginationEvaluator.Instance, - AsNoTrackingEvaluator.Instance - }); + Evaluators.AddRange(new IEvaluator[] + { + WhereEvaluator.Instance, + SearchEvaluator.Instance, + IncludeEvaluator.Instance, + OrderEvaluator.Instance, + PaginationEvaluator.Instance, + AsNoTrackingEvaluator.Instance + }); } public SpecificationEvaluator(IEnumerable evaluators) { - this.Evaluators.AddRange(evaluators); + Evaluators.AddRange(evaluators); } /// public virtual IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification is null) throw new ArgumentNullException("Specification is required"); - if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); - if (specification.Selector != null && specification.SelectorMany != null) throw new ConcurrentSelectorsException(); + if (specification is null) throw new ArgumentNullException("Specification is required"); + if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); + if (specification.Selector != null && specification.SelectorMany != null) throw new ConcurrentSelectorsException(); - query = GetQuery(query, (ISpecification)specification); + query = GetQuery(query, (ISpecification)specification); - return specification.Selector != null - ? query.Select(specification.Selector) - : query.SelectMany(specification.SelectorMany); + return specification.Selector != null + ? query.Select(specification.Selector) + : query.SelectMany(specification.SelectorMany); } /// public virtual IQueryable GetQuery(IQueryable query, ISpecification specification, bool evaluateCriteriaOnly = false) where T : class { - if (specification is null) throw new ArgumentNullException("Specification is required"); + if (specification is null) throw new ArgumentNullException("Specification is required"); - var evaluators = evaluateCriteriaOnly ? this.Evaluators.Where(x => x.IsCriteriaEvaluator) : this.Evaluators; + var evaluators = evaluateCriteriaOnly ? Evaluators.Where(x => x.IsCriteriaEvaluator) : Evaluators; - foreach (var evaluator in evaluators) - { - query = evaluator.GetQuery(query, specification); - } + foreach (var evaluator in evaluators) + { + query = evaluator.GetQuery(query, specification); + } - return query; + return query; } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs index 03497123..75376321 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/DbSetExtensions.cs @@ -4,21 +4,21 @@ using System.Threading; using System.Threading.Tasks; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public static class DbSetExtensions { - public static class DbSetExtensions - { public static async Task> ToListAsync( this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class { - var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); + var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null - ? result - : specification.PostProcessingAction(result).ToList(); + return specification.PostProcessingAction == null + ? result + : specification.PostProcessingAction(result).ToList(); } public static async Task> ToEnumerableAsync( @@ -27,11 +27,11 @@ public static async Task> ToEnumerableAsync( CancellationToken cancellationToken = default) where TSource : class { - var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); + var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null - ? result - : specification.PostProcessingAction(result); + return specification.PostProcessingAction == null + ? result + : specification.PostProcessingAction(result); } public static IQueryable WithSpecification( @@ -40,8 +40,8 @@ public static IQueryable WithSpecification( ISpecificationEvaluator evaluator = null) where TSource : class { - evaluator = evaluator ?? SpecificationEvaluator.Default; - return evaluator.GetQuery(source, specification); + evaluator ??= SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); } public static IQueryable WithSpecification( @@ -50,8 +50,7 @@ public static IQueryable WithSpecification( ISpecificationEvaluator evaluator = null) where TSource : class { - evaluator = evaluator ?? SpecificationEvaluator.Default; - return evaluator.GetQuery(source, specification); + evaluator ??= SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs index 0c5bb473..1a8dc420 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/IncludeExtensions.cs @@ -5,61 +5,60 @@ using System.Linq.Expressions; using System.Reflection; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public static class IncludeExtensions { - public static class IncludeExtensions - { public static IQueryable Include(this IQueryable source, IncludeExpressionInfo info) { - _ = info ?? throw new ArgumentNullException(nameof(info)); - var propertyName = GetPropertyName(info.LambdaExpression); + _ = info ?? throw new ArgumentNullException(nameof(info)); + var propertyName = GetPropertyName(info.LambdaExpression); - return QueryableExtensions.Include(source, propertyName); + return QueryableExtensions.Include(source, propertyName); } public static IQueryable ThenInclude(this IQueryable source, IncludeExpressionInfo info) { - _ = info ?? throw new ArgumentNullException(nameof(info)); - _ = info.PreviousPropertyType ?? throw new ArgumentNullException(nameof(info.PreviousPropertyType)); + _ = info ?? throw new ArgumentNullException(nameof(info)); + _ = info.PreviousPropertyType ?? throw new ArgumentNullException(nameof(info.PreviousPropertyType)); - var exp = source.Expression as MethodCallExpression; - var arg = exp.Arguments[0] as ConstantExpression; + var exp = source.Expression as MethodCallExpression; + var arg = exp.Arguments[0] as ConstantExpression; - string previousPropertyName; - if (arg.Value is string) - { - previousPropertyName = arg.Value.ToString(); - } - else - { - // System.Data.Entity.Core.Objects.Span is an internal class, so here's some reflection to get to the previous property. + string previousPropertyName; + if (arg.Value is string) + { + previousPropertyName = arg.Value.ToString(); + } + else + { + // System.Data.Entity.Core.Objects.Span is an internal class, so here's some reflection to get to the previous property. - var propertyInfo = arg.Value.GetType().GetProperty("SpanList", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); - var spanList = propertyInfo.GetValue(arg.Value); + var propertyInfo = arg.Value.GetType().GetProperty("SpanList", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var spanList = propertyInfo.GetValue(arg.Value); - // Get the first item of the span list - propertyInfo = propertyInfo.PropertyType.GetProperty("Item"); - var spanPath = propertyInfo.GetValue(spanList, new object[] { 0 }); + // Get the first item of the span list + propertyInfo = propertyInfo.PropertyType.GetProperty("Item"); + var spanPath = propertyInfo.GetValue(spanList, new object[] { 0 }); - var fieldInfo = spanPath.GetType().GetField("Navigations", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); - var navigations = fieldInfo.GetValue(spanPath) as List; - previousPropertyName = string.Join(".", navigations); - } + var fieldInfo = spanPath.GetType().GetField("Navigations", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); + var navigations = fieldInfo.GetValue(spanPath) as List; + previousPropertyName = string.Join(".", navigations); + } - var propertyName = GetPropertyName(info.LambdaExpression); + var propertyName = GetPropertyName(info.LambdaExpression); - return QueryableExtensions.Include(source, $"{previousPropertyName}.{propertyName}"); + return QueryableExtensions.Include(source, $"{previousPropertyName}.{propertyName}"); } private static string GetPropertyName(this Expression propertySelector, char delimiter = '.', char endTrim = ')') { - var asString = propertySelector.ToString(); - var firstDelim = asString.IndexOf(delimiter); + var asString = propertySelector.ToString(); + var firstDelim = asString.IndexOf(delimiter); - return firstDelim < 0 - ? asString - : asString.Substring(firstDelim + 1).TrimEnd(endTrim); + return firstDelim < 0 + ? asString + : asString.Substring(firstDelim + 1).TrimEnd(endTrim); } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/ParameterReplacerVisitor.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/ParameterReplacerVisitor.cs index 942d25f1..d54d6a68 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/ParameterReplacerVisitor.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/ParameterReplacerVisitor.cs @@ -1,33 +1,21 @@ using System.Linq.Expressions; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +internal class ParameterReplacerVisitor : ExpressionVisitor { - internal class ParameterReplacerVisitor : ExpressionVisitor - { - private readonly Expression newExpression; - private readonly ParameterExpression oldParameter; + private readonly Expression _newExpression; + private readonly ParameterExpression _oldParameter; private ParameterReplacerVisitor(ParameterExpression oldParameter, Expression newExpression) { - this.oldParameter = oldParameter; - this.newExpression = newExpression; + _oldParameter = oldParameter; + _newExpression = newExpression; } internal static Expression Replace(Expression expression, ParameterExpression oldParameter, Expression newExpression) - { - return new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression); - } + => new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression); protected override Expression VisitParameter(ParameterExpression p) - { - if (p == oldParameter) - { - return newExpression; - } - else - { - return p; - } - } - } + => p == _oldParameter ? _newExpression : p; } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/SearchExtension.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/SearchExtension.cs index 8cffe870..1a9df6bb 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/SearchExtension.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Extensions/SearchExtension.cs @@ -4,10 +4,10 @@ using System.Linq; using System.Linq.Expressions; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +public static class SearchExtension { - public static class SearchExtension - { /// /// Filters by applying an 'SQL LIKE' operation to it. /// @@ -22,33 +22,32 @@ public static class SearchExtension /// public static IQueryable Search(this IQueryable source, IEnumerable> criterias) { - Expression expr = null; - var parameter = Expression.Parameter(typeof(T), "x"); + Expression expr = null; + var parameter = Expression.Parameter(typeof(T), "x"); - foreach (var criteria in criterias) - { - var (selector, searchTerm) = (criteria.Selector, criteria.SearchTerm); - if (string.IsNullOrEmpty(criteria.SearchTerm)) + foreach (var criteria in criterias) { - continue; - } + var (selector, searchTerm) = (criteria.Selector, criteria.SearchTerm); + if (string.IsNullOrEmpty(criteria.SearchTerm)) + { + continue; + } - var like = typeof(DbFunctions).GetMethod(nameof(DbFunctions.Like), new Type[] { typeof(string), typeof(string) }); + var like = typeof(DbFunctions).GetMethod(nameof(DbFunctions.Like), new Type[] { typeof(string), typeof(string) }); - var propertySelector = ParameterReplacerVisitor.Replace(selector, selector.Parameters[0], parameter); + var propertySelector = ParameterReplacerVisitor.Replace(selector, selector.Parameters[0], parameter); - var likeExpression = Expression.Call( - null, - like, - (propertySelector as LambdaExpression)?.Body, - Expression.Constant(searchTerm)); + var likeExpression = Expression.Call( + null, + like, + (propertySelector as LambdaExpression)?.Body, + Expression.Constant(searchTerm)); - expr = expr == null ? (Expression)likeExpression : Expression.OrElse(expr, likeExpression); - } + expr = expr == null ? (Expression)likeExpression : Expression.OrElse(expr, likeExpression); + } - return expr == null - ? source - : source.Where(Expression.Lambda>(expr, parameter)); + return expr == null + ? source + : source.Where(Expression.Lambda>(expr, parameter)); } - } } diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Properties/AssemblyInfo.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Properties/AssemblyInfo.cs index 01386a11..481099a7 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Properties/AssemblyInfo.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs index ed50d851..eb4b5eb1 100644 --- a/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs +++ b/Specification.EntityFramework6/src/Ardalis.Specification.EntityFramework6/RepositoryBaseOfT.cs @@ -5,13 +5,13 @@ using System.Threading; using System.Threading.Tasks; -namespace Ardalis.Specification.EntityFramework6 +namespace Ardalis.Specification.EntityFramework6; + +/// +public abstract class RepositoryBase : IRepositoryBase where T : class { - /// - public abstract class RepositoryBase : IRepositoryBase where T : class - { - private readonly DbContext dbContext; - private readonly ISpecificationEvaluator specificationEvaluator; + private readonly DbContext _dbContext; + private readonly ISpecificationEvaluator _specificationEvaluator; public RepositoryBase(DbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) @@ -21,159 +21,159 @@ public RepositoryBase(DbContext dbContext) /// public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator) { - this.dbContext = dbContext; - this.specificationEvaluator = specificationEvaluator; + _dbContext = dbContext; + _specificationEvaluator = specificationEvaluator; } /// public virtual async Task AddAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Set().Add(entity); + _dbContext.Set().Add(entity); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); - return entity; + return entity; } /// public virtual async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - dbContext.Set().AddRange(entities); + _dbContext.Set().AddRange(entities); await SaveChangesAsync(cancellationToken); return entities; } - + /// public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Entry(entity).State = EntityState.Modified; + _dbContext.Entry(entity).State = EntityState.Modified; - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task UpdateRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - foreach (var entity in entities) - { - dbContext.Entry(entity).State = EntityState.Modified; - } + foreach (var entity in entities) + { + _dbContext.Entry(entity).State = EntityState.Modified; + } - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Set().Remove(entity); + _dbContext.Set().Remove(entity); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - dbContext.Set().RemoveRange(entities); + _dbContext.Set().RemoveRange(entities); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } - + /// public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return await dbContext.SaveChangesAsync(cancellationToken); + return await _dbContext.SaveChangesAsync(cancellationToken); } /// public virtual async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) { - return await dbContext.Set().FindAsync(cancellationToken: cancellationToken, new object[] { id }); + return await _dbContext.Set().FindAsync(cancellationToken: cancellationToken, new object[] { id }); } /// [Obsolete] public virtual async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// [Obsolete] public virtual async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); } /// public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); } /// public virtual async Task> ListAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().ToListAsync(cancellationToken); + return await _dbContext.Set().ToListAsync(cancellationToken); } /// public virtual async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); + var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } /// public virtual async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); + var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } /// public virtual async Task CountAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification, true).CountAsync(cancellationToken); + return await ApplySpecification(specification, true).CountAsync(cancellationToken); } /// public virtual async Task CountAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().CountAsync(cancellationToken); + return await _dbContext.Set().CountAsync(cancellationToken); } /// public virtual async Task AnyAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification, true).AnyAsync(cancellationToken); + return await ApplySpecification(specification, true).AnyAsync(cancellationToken); } /// public virtual async Task AnyAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().AnyAsync(cancellationToken); + return await _dbContext.Set().AnyAsync(cancellationToken); } /// @@ -184,7 +184,7 @@ public virtual async Task AnyAsync(CancellationToken cancellationToken = d /// The filtered entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, bool evaluateCriteriaOnly = false) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); + return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); } /// @@ -199,7 +199,6 @@ protected virtual IQueryable ApplySpecification(ISpecification specificati /// The filtered projected entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification); + return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Ardalis.Specification.EntityFramework6.IntegrationTests.csproj b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Ardalis.Specification.EntityFramework6.IntegrationTests.csproj index 459a6493..1b22261b 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Ardalis.Specification.EntityFramework6.IntegrationTests.csproj +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Ardalis.Specification.EntityFramework6.IntegrationTests.csproj @@ -1,7 +1,8 @@ - + net472 + 11.0 Library false true @@ -15,23 +16,24 @@ - - - runtime; build; native; contentfiles; analyzers - all - + + + - - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive + + runtime; build; native; contentfiles; analyzers + all + @@ -40,4 +42,9 @@ + + + 1701;1702;1591;1573;1712;0612 + + diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/AddressConfiguration.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/AddressConfiguration.cs index 40deed39..beaef87d 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/AddressConfiguration.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/AddressConfiguration.cs @@ -1,14 +1,13 @@ -using System.Data.Entity.ModelConfiguration; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System.Data.Entity.ModelConfiguration; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations; + +public class AddressConfiguration : EntityTypeConfiguration
{ - public class AddressConfiguration : EntityTypeConfiguration
- { public AddressConfiguration() { - ToTable("Address"); - HasKey(c => c.Id); + ToTable("Address"); + HasKey(c => c.Id); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CompanyConfiguration.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CompanyConfiguration.cs index 8954b787..3d641085 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CompanyConfiguration.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CompanyConfiguration.cs @@ -1,20 +1,19 @@ -using System.Data.Entity.ModelConfiguration; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System.Data.Entity.ModelConfiguration; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations; + +public class CompanyConfiguration : EntityTypeConfiguration { - public class CompanyConfiguration : EntityTypeConfiguration - { public CompanyConfiguration() { - ToTable("Company"); - HasKey(c => c.Id); + ToTable("Company"); + HasKey(c => c.Id); - Property(c => c.Id) - .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); + Property(c => c.Id) + .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None); - //HasMany(c => c.Stores) - // .WithRequired(s => s.Company); + //HasMany(c => c.Stores) + // .WithRequired(s => s.Company); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CountryConfiguration.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CountryConfiguration.cs index ae0c2dfa..3dada338 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CountryConfiguration.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/CountryConfiguration.cs @@ -1,14 +1,13 @@ -using System.Data.Entity.ModelConfiguration; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System.Data.Entity.ModelConfiguration; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations; + +public class CountryConfiguration : EntityTypeConfiguration { - public class CountryConfiguration : EntityTypeConfiguration - { public CountryConfiguration() { - ToTable("Country"); - HasKey(c => c.Id); + ToTable("Country"); + HasKey(c => c.Id); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/ProductConfiguration.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/ProductConfiguration.cs index e1190982..77179ea4 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/ProductConfiguration.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/ProductConfiguration.cs @@ -1,14 +1,13 @@ -using System.Data.Entity.ModelConfiguration; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System.Data.Entity.ModelConfiguration; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations; + +public class ProductConfiguration : EntityTypeConfiguration { - public class ProductConfiguration : EntityTypeConfiguration - { public ProductConfiguration() { - ToTable("Product"); - HasKey(c => c.Id); + ToTable("Product"); + HasKey(c => c.Id); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/StoreConfiguration.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/StoreConfiguration.cs index 3557f726..e86d309d 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/StoreConfiguration.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/Configurations/StoreConfiguration.cs @@ -1,17 +1,16 @@ -using System.Data.Entity.ModelConfiguration; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System.Data.Entity.ModelConfiguration; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture.Configurations; + +public class StoreConfiguration : EntityTypeConfiguration { - public class StoreConfiguration : EntityTypeConfiguration - { public StoreConfiguration() { - ToTable("Store"); - HasKey(c => c.Id); + ToTable("Store"); + HasKey(c => c.Id); - HasOptional(s => s.Address) - .WithRequired(x => x.Store); + HasOptional(s => s.Address) + .WithRequired(x => x.Store); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/DbInitializer.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/DbInitializer.cs index 421db014..117ae0ae 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/DbInitializer.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/DbInitializer.cs @@ -1,23 +1,20 @@ -using System.Data.Entity; -using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; +using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; +using System.Data.Entity; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; + +public class DbInitializer : CreateDatabaseIfNotExists { - public class DbInitializer : CreateDatabaseIfNotExists - { protected override void Seed(TestDbContext context) { - base.Seed(context); - - var companies = CompanySeed.Get(); + base.Seed(context); - context.Addresses.AddRange(AddressSeed.Get()); - context.Countries.AddRange(CountrySeed.Get()); - context.Companies.AddRange(CompanySeed.Get()); - context.Products.AddRange(ProductSeed.Get()); - context.Stores.AddRange(StoreSeed.Get()); + context.Addresses.AddRange(AddressSeed.Get()); + context.Countries.AddRange(CountrySeed.Get()); + context.Companies.AddRange(CompanySeed.Get()); + context.Products.AddRange(ProductSeed.Get()); + context.Stores.AddRange(StoreSeed.Get()); - context.SaveChanges(); + context.SaveChanges(); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/IntegrationTestBase.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/IntegrationTestBase.cs index d9dd00b0..97083010 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/IntegrationTestBase.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/IntegrationTestBase.cs @@ -1,20 +1,19 @@ using Ardalis.Specification.UnitTests.Fixture.Entities; using Xunit; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; + +public class IntegrationTestBase : IClassFixture { - public class IntegrationTestBase : IClassFixture - { protected TestDbContext dbContext; protected Repository companyRepository; protected Repository storeRepository; public IntegrationTestBase(SharedDatabaseFixture fixture) { - dbContext = fixture.CreateContext(); + dbContext = fixture.CreateContext(); - companyRepository = new Repository(dbContext); - storeRepository = new Repository(dbContext); + companyRepository = new Repository(dbContext); + storeRepository = new Repository(dbContext); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs index 4f838788..c2b3516a 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/RepositoryOfT.cs @@ -1,13 +1,12 @@ -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; + +/// +public class Repository : RepositoryBase where T : class { - /// - public class Repository : RepositoryBase where T : class - { protected readonly TestDbContext dbContext; public Repository(TestDbContext dbContext) : base(dbContext) { - this.dbContext = dbContext; + this.dbContext = dbContext; } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/SharedDatabaseFixture.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/SharedDatabaseFixture.cs index 1470a4a4..840472e8 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/SharedDatabaseFixture.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/SharedDatabaseFixture.cs @@ -1,47 +1,46 @@ -using System; +using MartinCostello.SqlLocalDb; +using System; using System.Data.Common; using System.Data.SqlClient; -using MartinCostello.SqlLocalDb; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; + +public class SharedDatabaseFixture : IDisposable { - public class SharedDatabaseFixture : IDisposable - { // (docker) - public const string ConnectionStringDocker = "Data Source=databaseEF6;Initial Catalog=SpecificationEF6TestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!"; + public const string _connectionStringDocker = "Data Source=databaseEF6;Initial Catalog=SpecificationEF6TestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!"; // (localdb) - public const string ConnectionStringLocalDb = "Server=(localdb)\\mssqllocaldb;Integrated Security=SSPI;Initial Catalog=SpecificationEF6TestsDB;ConnectRetryCount=0"; + public const string _connectionStringLocalDb = "Server=(localdb)\\mssqllocaldb;Integrated Security=SSPI;Initial Catalog=SpecificationEF6TestsDB;ConnectRetryCount=0"; public SharedDatabaseFixture() { - var isLocalDBInstalled = false; + var isLocalDBInstalled = false; - using (var localDB = new SqlLocalDbApi()) - { - isLocalDBInstalled = localDB.IsLocalDBInstalled(); - } + using (var localDB = new SqlLocalDbApi()) + { + isLocalDBInstalled = localDB.IsLocalDBInstalled(); + } - Connection = isLocalDBInstalled - ? new SqlConnection(ConnectionStringLocalDb) - : new SqlConnection(ConnectionStringDocker); + Connection = isLocalDBInstalled + ? new SqlConnection(_connectionStringLocalDb) + : new SqlConnection(_connectionStringDocker); } public DbConnection Connection { get; } public TestDbContext CreateContext(DbTransaction transaction = null) { - var context = new TestDbContext(Connection); + var context = new TestDbContext(Connection); - if (transaction != null) - { - context.Database.UseTransaction(transaction); - } + if (transaction != null) + { + context.Database.UseTransaction(transaction); + } - return context; + return context; } public void Dispose() => Connection.Dispose(); - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/TestDbContext.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/TestDbContext.cs index 2683f3db..fc1524d1 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/TestDbContext.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Fixture/TestDbContext.cs @@ -1,18 +1,18 @@ -using System; +using Ardalis.Specification.UnitTests.Fixture.Entities; +using System; using System.Data.Common; using System.Data.Entity; using System.Data.Entity.ModelConfiguration; using System.Linq; using System.Reflection; -using Ardalis.Specification.UnitTests.Fixture.Entities; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; + +public class TestDbContext : DbContext { - public class TestDbContext : DbContext - { public TestDbContext(DbConnection connection) : base(connection, false) { - Database.SetInitializer(new DbInitializer()); + Database.SetInitializer(new DbInitializer()); } public virtual DbSet Countries { get; set; } @@ -23,17 +23,16 @@ public TestDbContext(DbConnection connection) : base(connection, false) protected override void OnModelCreating(DbModelBuilder modelBuilder) { - var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() - .Where(type => !string.IsNullOrEmpty(type.Namespace)) - .Where(type => type.BaseType != null && type.BaseType.IsGenericType - && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); - foreach (var type in typesToRegister) - { - dynamic configurationInstance = Activator.CreateInstance(type); - modelBuilder.Configurations.Add(configurationInstance); - } + var typesToRegister = Assembly.GetExecutingAssembly().GetTypes() + .Where(type => !string.IsNullOrEmpty(type.Namespace)) + .Where(type => type.BaseType != null && type.BaseType.IsGenericType + && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>)); + foreach (var type in typesToRegister) + { + dynamic configurationInstance = Activator.CreateInstance(type); + modelBuilder.Configurations.Add(configurationInstance); + } - base.OnModelCreating(modelBuilder); + base.OnModelCreating(modelBuilder); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Properties/AssemblyInfo.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Properties/AssemblyInfo.cs index ebb759ea..42c42f1f 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Properties/AssemblyInfo.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/Properties/AssemblyInfo.cs @@ -1,5 +1,4 @@ using System.Reflection; -using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_AnyAsync.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_AnyAsync.cs index c2c527d2..349b2465 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_AnyAsync.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_AnyAsync.cs @@ -1,38 +1,37 @@ -using System.Threading.Tasks; -using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; +using System.Threading.Tasks; using Xunit; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests +namespace Ardalis.Specification.EntityFramework6.IntegrationTests; + +public class RepositoryOfT_AnyAsync : IntegrationTestBase { - public class RepositoryOfT_AnyAsync : IntegrationTestBase - { public RepositoryOfT_AnyAsync(SharedDatabaseFixture fixture) : base(fixture) { } [Fact] public async Task ReturnsTrueOnStoresRecords_WithoutSpec() { - var result = await storeRepository.AnyAsync(); + var result = await storeRepository.AnyAsync(); - result.Should().BeTrue(); + result.Should().BeTrue(); } [Fact] public async Task ReturnsTrue_GivenStoreByIdSpecWithValidStore() { - var result = await storeRepository.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID)); - result.Should().BeTrue(); + result.Should().BeTrue(); } [Fact] public async Task ReturnsFalse_GivenStoreByIdSpecWithInvalidStore() { - var result = await storeRepository.AnyAsync(new StoreByIdSpec(0)); + var result = await storeRepository.AnyAsync(new StoreByIdSpec(0)); - result.Should().BeFalse(); + result.Should().BeFalse(); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetById.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetById.cs index d6667e7b..a53c03df 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetById.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetById.cs @@ -1,31 +1,30 @@ -using System.Threading.Tasks; -using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using FluentAssertions; +using System.Threading.Tasks; using Xunit; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests +namespace Ardalis.Specification.EntityFramework6.IntegrationTests; + +public class RepositoryOfT_GetById : IntegrationTestBase { - public class RepositoryOfT_GetById : IntegrationTestBase - { public RepositoryOfT_GetById(SharedDatabaseFixture fixture) : base(fixture) { } [Fact] public async Task ReturnsStore_GivenId() { - var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); + var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); } [Fact] public async Task ReturnsStore_GivenGenericId() { - var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); + var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetBySpec.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetBySpec.cs index a9d610af..41480ff1 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetBySpec.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_GetBySpec.cs @@ -1,104 +1,103 @@ -using System.Data.Entity; -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; +using System.Data.Entity; +using System.Linq; +using System.Threading.Tasks; using Xunit; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests +namespace Ardalis.Specification.EntityFramework6.IntegrationTests; + +public class RepositoryOfT_GetBySpec : IntegrationTestBase { - public class RepositoryOfT_GetBySpec : IntegrationTestBase - { public RepositoryOfT_GetBySpec(SharedDatabaseFixture fixture) : base(fixture) { } [Fact] public async Task ReturnsStoreWithProducts_GivenStoreByIdIncludeProductsSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); } [Fact] public async Task ReturnsStoreWithAddress_GivenStoreByIdIncludeAddressSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); } [Fact] public async Task ReturnsStoreWithAddressAndProduct_GivenStoreByIdIncludeAddressAndProductsSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressAndProductsSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressAndProductsSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); - result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); + result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); } [Fact] public async Task ReturnsStoreWithProducts_GivenStoreByIdIncludeProductsUsingStringSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsUsingStringSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsUsingStringSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); } [Fact] public async Task ReturnsCompanyWithStoresAndAddress_GivenCompanyByIdIncludeStoresThenIncludeAddressSpec() { - var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeAddressSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeAddressSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - result.Stores.Count.Should().BeGreaterThan(49); - result.Stores.Select(x => x.Address).Count().Should().BeGreaterThan(0); + result.Should().NotBeNull(); + result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + result.Stores.Count.Should().BeGreaterThan(49); + result.Stores.Select(x => x.Address).Count().Should().BeGreaterThan(0); } [Fact] public async Task ReturnsCompanyWithStoresAndProducts_GivenCompanyByIdIncludeStoresThenIncludeProductsSpec() { - var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeProductsSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeProductsSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - result.Stores.Count.Should().BeGreaterThan(49); - result.Stores.Select(x => x.Products).Count().Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + result.Stores.Count.Should().BeGreaterThan(49); + result.Stores.Select(x => x.Products).Count().Should().BeGreaterThan(1); } [Fact] public async Task ReturnsUntrackedCompany_GivenCompanyByIdAsUntrackedSpec() { - //dbContext.ChangeTracker.Clear(); + //dbContext.ChangeTracker.Clear(); - var result = await companyRepository.GetBySpecAsync(new CompanyByIdAsUntrackedSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdAsUntrackedSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result?.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - dbContext.Entry(result).State.Should().Be(EntityState.Detached); + result.Should().NotBeNull(); + result?.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + dbContext.Entry(result).State.Should().Be(EntityState.Detached); } [Fact] public async Task ReturnsStoreWithCompanyAndCountryAndStoresForCompany_GivenStoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec(StoreSeed.VALID_STORE_ID)); - - result.Should().NotBeNull(); - result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Company.Should().NotBeNull(); - result.Company?.Country.Should().NotBeNull(); - result.Company?.Stores.Should().HaveCountGreaterOrEqualTo(2); - result.Company?.Stores?.Should().Match(x => x.Any(z => z.Products.Count > 0)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec(StoreSeed.VALID_STORE_ID)); + + result.Should().NotBeNull(); + result.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Company.Should().NotBeNull(); + result.Company?.Country.Should().NotBeNull(); + result.Company?.Stores.Should().HaveCountGreaterOrEqualTo(2); + result.Company?.Stores?.Should().Match(x => x.Any(z => z.Products.Count > 0)); } - } } diff --git a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_ListAsync.cs b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_ListAsync.cs index 97ff4d67..0c40567d 100644 --- a/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_ListAsync.cs +++ b/Specification.EntityFramework6/tests/Ardalis.Specification.EntityFramework6.IntegrationTests/RepositoryOfT_ListAsync.cs @@ -1,184 +1,183 @@ -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFramework6.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; +using System.Linq; +using System.Threading.Tasks; using Xunit; -namespace Ardalis.Specification.EntityFramework6.IntegrationTests +namespace Ardalis.Specification.EntityFramework6.IntegrationTests; + +public class RepositoryOfT_ListAsync : IntegrationTestBase { - public class RepositoryOfT_ListAsync : IntegrationTestBase - { public RepositoryOfT_ListAsync(SharedDatabaseFixture fixture) : base(fixture) { } [Fact] public async Task ReturnsStoreWithProducts_GivenStoreIncludeProductsSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeProductsSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeProductsSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Products.Should().NotBeEmpty(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Products.Should().NotBeEmpty(); } [Fact] public async Task ReturnsStoreWithAddress_GivenStoreIncludeAddressSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeAddressSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeAddressSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Address.Should().NotBeNull(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Address.Should().NotBeNull(); } [Fact] public async Task ReturnsStoreWithAddressAndProduct_GivenStoreIncludeAddressAndProductsSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeAddressAndProductsSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeAddressAndProductsSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Address.Should().NotBeNull(); - result[0].Products.Should().NotBeEmpty(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Address.Should().NotBeNull(); + result[0].Products.Should().NotBeEmpty(); } [Fact] public async Task ReturnsStoreWithIdFrom15To30_GivenStoresByIdListSpec() { - var ids = Enumerable.Range(15, 16); - var spec = new StoresByIdListSpec(ids); + var ids = Enumerable.Range(15, 16); + var spec = new StoresByIdListSpec(ids); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(16); - stores.OrderBy(x => x.Id).First().Id.Should().Be(15); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); + stores.Count.Should().Be(16); + stores.OrderBy(x => x.Id).First().Id.Should().Be(15); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); } [Fact] public async Task ReturnsSecondPageOfStoreNames_GivenStoreNamesPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoreNamesPaginatedSpec(skip, take); + var spec = new StoreNamesPaginatedSpec(skip, take); - var storeNames = await storeRepository.ListAsync(spec); + var storeNames = await storeRepository.ListAsync(spec); - storeNames.Count.Should().Be(take); - storeNames.First().Should().Be("Store 11"); - storeNames.Last().Should().Be("Store 20"); + storeNames.Count.Should().Be(take); + storeNames.First().Should().Be("Store 11"); + storeNames.Last().Should().Be("Store 20"); } [Fact] public async Task ReturnsSecondPageOfStores_GivenStoresPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresPaginatedSpec(skip, take); + var spec = new StoresPaginatedSpec(skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(11); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); + stores.Count.Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(11); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); } [Fact] public async Task ReturnsOrderStoresByNameDescForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameSpec() { - var spec = new StoresByCompanyOrderedDescByNameSpec(2); + var spec = new StoresByCompanyOrderedDescByNameSpec(2); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); } [Fact] public async Task ReturnsOrderStoresByNameDescThenByIdForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameThenByIdSpec() { - var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); + var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(99); - stores.Last().Id.Should().Be(98); + stores.First().Id.Should().Be(99); + stores.Last().Id.Should().Be(98); } [Fact] public async Task ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedOrderedDescByNameSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); + stores.Count.Should().Be(take); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); } [Fact] public async Task ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedSpec(2, skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(61); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); + stores.Count.Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(61); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); } [Fact] public async Task ReturnsOrderedStores_GivenStoresOrderedSpecByName() { - var spec = new StoresOrderedSpecByName(); + var spec = new StoresOrderedSpecByName(); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); } [Fact] public async Task ReturnsOrderedStores_GivenStoresOrderedDescendingByNameSpec() { - var spec = new StoresOrderedDescendingByNameSpec(); + var spec = new StoresOrderedDescendingByNameSpec(); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); } [Fact] public async Task ReturnsStoreContainingCity1_GivenStoreIncludeProductsSpec() { - var result = await storeRepository.ListAsync(new StoreSearchByNameOrCitySpec(StoreSeed.VALID_Search_City_Key)); + var result = await storeRepository.ListAsync(new StoreSearchByNameOrCitySpec(StoreSeed.VALID_Search_City_Key)); - result.Should().NotBeNull(); - result.Should().ContainSingle(); - result[0].Id.Should().Be(StoreSeed.VALID_Search_ID); - result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key); + result.Should().NotBeNull(); + result.Should().ContainSingle(); + result[0].Id.Should().Be(StoreSeed.VALID_Search_ID); + result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key); } [Fact] public virtual async Task ReturnsAllProducts_GivenStoreSelectManyProductsSpec() { - var result = await storeRepository.ListAsync(new StoreProductNamesSpec()); + var result = await storeRepository.ListAsync(new StoreProductNamesSpec()); - result.Should().NotBeNull(); - result.Should().HaveCount(ProductSeed.TOTAL_PRODUCT_COUNT); - result.OrderBy(x => x).First().Should().Be(ProductSeed.VALID_PRODUCT_NAME); + result.Should().NotBeNull(); + result.Should().HaveCount(ProductSeed.TOTAL_PRODUCT_COUNT); + result.OrderBy(x => x).First().Should().Be(ProductSeed.VALID_PRODUCT_NAME); } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Ardalis.Specification.EntityFrameworkCore.csproj b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Ardalis.Specification.EntityFrameworkCore.csproj index e6db8b37..3105a170 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Ardalis.Specification.EntityFrameworkCore.csproj +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Ardalis.Specification.EntityFrameworkCore.csproj @@ -2,50 +2,57 @@ net6.0 + 11.0 + enable + enable + + Ardalis.Specification.EntityFrameworkCore Ardalis.Specification.EntityFrameworkCore Ardalis.Specification.EntityFrameworkCore true Steve Smith (@ardalis); Fati Iseni (@fiseni); Scott DePouw Ardalis.com - https://github.com/ardalis/specification EF Core plugin package to Ardalis.Specification containing EF Core evaluator and abstract repository. EF Core plugin package to Ardalis.Specification containing EF Core evaluator and abstract repository. + https://github.com/ardalis/specification https://github.com/ardalis/specification spec;specification;repository;ddd;ef;ef core;entity framework;entity framework core + icon.png 7.0.0 - * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 - * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 - * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 - * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 - * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 - * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 - * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 - * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 - * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 - * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 - * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 - Ardalis.Specification.EntityFrameworkCore - icon.png + * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 + * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 + * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 + * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 + * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 + * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 + * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 + * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 + * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 + * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 + * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 + true true $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - 9.0 - enable bin\$(Configuration)\Ardalis.Specification.EntityFrameworkCore.xml - - + + - + - + - + + + + 1701;1702;1591;1573;1712 + diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/CachedReadConcurrentDictionary.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/CachedReadConcurrentDictionary.cs index 5245dfce..f493d7cd 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/CachedReadConcurrentDictionary.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/CachedReadConcurrentDictionary.cs @@ -1,44 +1,41 @@ -using System; -using System.Collections; +using System.Collections; using System.Collections.Concurrent; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; -using System.Threading; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +/// +/// A thread-safe dictionary for read-heavy workloads. +/// +/// The key type. +/// The value type. +internal class CachedReadConcurrentDictionary : IDictionary where TKey : notnull { - /// - /// A thread-safe dictionary for read-heavy workloads. - /// - /// The key type. - /// The value type. - internal class CachedReadConcurrentDictionary : IDictionary where TKey : notnull - { /// /// The number of cache misses which are tolerated before the cache is regenerated. /// - private const int CacheMissesBeforeCaching = 10; - private readonly ConcurrentDictionary dictionary; - private readonly IEqualityComparer? comparer; + private const int _cacheMissesBeforeCaching = 10; + private readonly ConcurrentDictionary _dictionary; + private readonly IEqualityComparer? _comparer; /// /// Approximate number of reads which did not hit the cache since it was last invalidated. /// This is used as a heuristic that the dictionary is not being modified frequently with respect to the read volume. /// - private int cacheMissReads; + private int _cacheMissReads; /// - /// Cached version of . + /// Cached version of . /// - private Dictionary? readCache; + private Dictionary? _readCache; /// /// Initializes a new instance of the class. /// public CachedReadConcurrentDictionary() { - this.dictionary = new ConcurrentDictionary(); + _dictionary = new ConcurrentDictionary(); } /// @@ -50,7 +47,7 @@ public CachedReadConcurrentDictionary() /// public CachedReadConcurrentDictionary(IEnumerable> collection) { - this.dictionary = new ConcurrentDictionary(collection); + _dictionary = new ConcurrentDictionary(collection); } /// @@ -63,8 +60,8 @@ public CachedReadConcurrentDictionary(IEnumerable> co /// public CachedReadConcurrentDictionary(IEqualityComparer comparer) { - this.comparer = comparer; - this.dictionary = new ConcurrentDictionary(comparer); + _comparer = comparer; + _dictionary = new ConcurrentDictionary(comparer); } /// @@ -80,49 +77,49 @@ public CachedReadConcurrentDictionary(IEqualityComparer comparer) /// public CachedReadConcurrentDictionary(IEnumerable> collection, IEqualityComparer comparer) { - this.comparer = comparer; - this.dictionary = new ConcurrentDictionary(collection, comparer); + _comparer = comparer; + _dictionary = new ConcurrentDictionary(collection, comparer); } /// - IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); /// - public IEnumerator> GetEnumerator() => this.GetReadDictionary().GetEnumerator(); + public IEnumerator> GetEnumerator() => GetReadDictionary().GetEnumerator(); /// public void Add(KeyValuePair item) { - ((IDictionary)this.dictionary).Add(item); - this.InvalidateCache(); + ((IDictionary)_dictionary).Add(item); + InvalidateCache(); } /// public void Clear() { - this.dictionary.Clear(); - this.InvalidateCache(); + _dictionary.Clear(); + InvalidateCache(); } /// - public bool Contains(KeyValuePair item) => this.GetReadDictionary().Contains(item); + public bool Contains(KeyValuePair item) => GetReadDictionary().Contains(item); /// public void CopyTo(KeyValuePair[] array, int arrayIndex) { - this.GetReadDictionary().CopyTo(array, arrayIndex); + GetReadDictionary().CopyTo(array, arrayIndex); } /// public bool Remove(KeyValuePair item) { - var result = ((IDictionary)this.dictionary).Remove(item); - if (result) this.InvalidateCache(); - return result; + var result = ((IDictionary)_dictionary).Remove(item); + if (result) InvalidateCache(); + return result; } /// - public int Count => this.GetReadDictionary().Count; + public int Count => GetReadDictionary().Count; /// public bool IsReadOnly => false; @@ -130,8 +127,8 @@ public bool Remove(KeyValuePair item) /// public void Add(TKey key, TValue value) { - ((IDictionary)this.dictionary).Add(key, value); - this.InvalidateCache(); + ((IDictionary)_dictionary).Add(key, value); + InvalidateCache(); } /// @@ -142,15 +139,15 @@ public void Add(TKey key, TValue value) /// The value for the key. This will be either the existing value for the key if the key is already in the dictionary, or the new value if the key was not in the dictionary. public TValue GetOrAdd(TKey key, Func valueFactory) { - if (this.GetReadDictionary().TryGetValue(key, out var value)) - { - return value; - } + if (GetReadDictionary().TryGetValue(key, out var value)) + { + return value; + } - value = this.dictionary.GetOrAdd(key, valueFactory); - InvalidateCache(); + value = _dictionary.GetOrAdd(key, valueFactory); + InvalidateCache(); - return value; + return value; } /// @@ -162,72 +159,65 @@ public TValue GetOrAdd(TKey key, Func valueFactory) /// true if the key/value pair was added successfully; otherwise, false. public bool TryAdd(TKey key, TValue value) { - if (this.dictionary.TryAdd(key, value)) - { - this.InvalidateCache(); - return true; - } + if (_dictionary.TryAdd(key, value)) + { + InvalidateCache(); + return true; + } - return false; + return false; } /// - public bool ContainsKey(TKey key) => this.GetReadDictionary().ContainsKey(key); + public bool ContainsKey(TKey key) => GetReadDictionary().ContainsKey(key); /// public bool Remove(TKey key) { - var result = ((IDictionary)this.dictionary).Remove(key); - if (result) this.InvalidateCache(); - return result; + var result = ((IDictionary)_dictionary).Remove(key); + if (result) InvalidateCache(); + return result; } -#if NET6_0_OR_GREATER /// - public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) => this.GetReadDictionary().TryGetValue(key, out value); -#else - /// - public bool TryGetValue(TKey key, out TValue value) => this.GetReadDictionary().TryGetValue(key, out value); - -#endif + public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) => GetReadDictionary().TryGetValue(key, out value); /// public TValue this[TKey key] { - get => this.GetReadDictionary()[key]; - set - { - this.dictionary[key] = value; - this.InvalidateCache(); - } + get => GetReadDictionary()[key]; + set + { + _dictionary[key] = value; + InvalidateCache(); + } } /// - public ICollection Keys => this.GetReadDictionary().Keys; + public ICollection Keys => GetReadDictionary().Keys; /// - public ICollection Values => this.GetReadDictionary().Values; + public ICollection Values => GetReadDictionary().Values; [MethodImpl(MethodImplOptions.AggressiveInlining)] - private IDictionary GetReadDictionary() => this.readCache ?? this.GetWithoutCache(); + private IDictionary GetReadDictionary() => _readCache ?? GetWithoutCache(); private IDictionary GetWithoutCache() { - // If the dictionary was recently modified or the cache is being recomputed, return the dictionary directly. - if (Interlocked.Increment(ref this.cacheMissReads) < CacheMissesBeforeCaching) - { - return this.dictionary; - } - - // Recompute the cache if too many cache misses have occurred. - this.cacheMissReads = 0; - return this.readCache = new Dictionary(this.dictionary, this.comparer); + // If the dictionary was recently modified or the cache is being recomputed, return the dictionary directly. + if (Interlocked.Increment(ref _cacheMissReads) < _cacheMissesBeforeCaching) + { + return _dictionary; + } + + // Recompute the cache if too many cache misses have occurred. + _cacheMissReads = 0; + return _readCache = new Dictionary(_dictionary, _comparer); } private void InvalidateCache() { - this.cacheMissReads = 0; - this.readCache = null; + _cacheMissReads = 0; + _readCache = null; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs index 961505a3..7be55c15 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/ContextFactoryRepositoryBaseOfT.cs @@ -1,18 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace Ardalis.Specification.EntityFrameworkCore +using Microsoft.EntityFrameworkCore; + +namespace Ardalis.Specification.EntityFrameworkCore; + +public abstract class ContextFactoryRepositoryBaseOfT : IRepositoryBase + where TEntity : class + where TContext : DbContext { - public abstract class ContextFactoryRepositoryBaseOfT : IRepositoryBase - where TEntity : class - where TContext : DbContext - { - private IDbContextFactory dbContextFactory; - private ISpecificationEvaluator specificationEvaluator; + private readonly IDbContextFactory _dbContextFactory; + private readonly ISpecificationEvaluator _specificationEvaluator; public ContextFactoryRepositoryBaseOfT(IDbContextFactory dbContextFactory) : this(dbContextFactory, SpecificationEvaluator.Default) @@ -22,189 +17,189 @@ public ContextFactoryRepositoryBaseOfT(IDbContextFactory dbContextFact public ContextFactoryRepositoryBaseOfT(IDbContextFactory dbContextFactory, ISpecificationEvaluator specificationEvaluator) { - this.dbContextFactory = dbContextFactory; - this.specificationEvaluator = specificationEvaluator; + _dbContextFactory = dbContextFactory; + _specificationEvaluator = specificationEvaluator; } /// public async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); } /// public async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext).FirstOrDefaultAsync(cancellationToken); } /// public async Task> ListAsync(CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await dbContext.Set().ToListAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await dbContext.Set().ToListAsync(cancellationToken); } /// public async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - var queryResult = await ApplySpecification(specification, dbContext).ToListAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + var queryResult = await ApplySpecification(specification, dbContext).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } - + /// public async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - var queryResult = await ApplySpecification(specification, dbContext).ToListAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + var queryResult = await ApplySpecification(specification, dbContext).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } /// public async Task CountAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext, true).CountAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext, true).CountAsync(cancellationToken); } /// public async Task CountAsync(CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await dbContext.Set().CountAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await dbContext.Set().CountAsync(cancellationToken); } /// public async Task AnyAsync(ISpecification specification, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await ApplySpecification(specification, dbContext, true).AnyAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await ApplySpecification(specification, dbContext, true).AnyAsync(cancellationToken); } /// public async Task AnyAsync(CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - return await dbContext.Set().AnyAsync(cancellationToken); + await using var dbContext = _dbContextFactory.CreateDbContext(); + return await dbContext.Set().AnyAsync(cancellationToken); } /// public IAsyncEnumerable AsAsyncEnumerable(ISpecification specification) { - using var dbContext = this.dbContextFactory.CreateDbContext(); - return ApplySpecification(specification, dbContext).AsAsyncEnumerable(); + using var dbContext = _dbContextFactory.CreateDbContext(); + return ApplySpecification(specification, dbContext).AsAsyncEnumerable(); } /// public async Task AddAsync(TEntity entity, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().Add(entity); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().Add(entity); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); - return entity; + return entity; } /// public async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().AddRange(entities); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().AddRange(entities); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); - return entities; + return entities; } /// public async Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().Update(entity); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().Update(entity); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); } /// public async Task UpdateRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().UpdateRange(entities); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().UpdateRange(entities); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); } /// public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().Remove(entity); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().Remove(entity); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); } /// public async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - await using var dbContext = this.dbContextFactory.CreateDbContext(); - dbContext.Set().RemoveRange(entities); + await using var dbContext = _dbContextFactory.CreateDbContext(); + dbContext.Set().RemoveRange(entities); - await SaveChangesAsync(dbContext, cancellationToken); + await SaveChangesAsync(dbContext, cancellationToken); } /// - public async Task SaveChangesAsync(CancellationToken cancellationToken = default) + public Task SaveChangesAsync(CancellationToken cancellationToken = default) { - throw new InvalidOperationException(); + throw new InvalidOperationException(); } public async Task SaveChangesAsync(TContext dbContext, CancellationToken cancellationToken = default) { - return await dbContext.SaveChangesAsync(cancellationToken); + return await dbContext.SaveChangesAsync(cancellationToken); } - + /// /// Filters the entities of , to those that match the encapsulated query logic of the /// . @@ -213,7 +208,7 @@ public async Task SaveChangesAsync(TContext dbContext, CancellationToken ca /// The filtered entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, TContext dbContext, bool evaluateCriteriaOnly = false) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); + return _specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); } /// @@ -228,7 +223,6 @@ protected virtual IQueryable ApplySpecification(ISpecification /// The filtered projected entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, TContext dbContext) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification); + return _specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification); } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/EFRepositoryFactory.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/EFRepositoryFactory.cs index dba7bc13..3e21e647 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/EFRepositoryFactory.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/EFRepositoryFactory.cs @@ -1,21 +1,20 @@ -using System; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +/// +/// +/// +/// The Interface of the repository created by this Factory +/// +/// The Concrete implementation of the repository interface to create +/// +/// The DbContext derived class to support the concrete repository +public class EFRepositoryFactory : IRepositoryFactory + where TConcreteRepository : TRepository + where TContext : DbContext { - /// - /// - /// - /// The Interface of the repository created by this Factory - /// - /// The Concrete implementation of the repository interface to create - /// - /// The DbContext derived class to support the concrete repository - public class EFRepositoryFactory : IRepositoryFactory - where TConcreteRepository : TRepository - where TContext : DbContext - { - private IDbContextFactory dbContextFactory; + private readonly IDbContextFactory _dbContextFactory; /// /// Initialises a new instance of the EFRepositoryFactory @@ -23,14 +22,13 @@ public class EFRepositoryFactory : I /// The IDbContextFactory to use to generate the TContext public EFRepositoryFactory(IDbContextFactory dbContextFactory) { - this.dbContextFactory = dbContextFactory; + _dbContextFactory = dbContextFactory; } /// public TRepository CreateRepository() { - var args = new object[] { dbContextFactory.CreateDbContext() }; - return (TRepository)Activator.CreateInstance(typeof(TConcreteRepository), args); + var args = new object[] { _dbContextFactory.CreateDbContext() }; + return (TRepository)Activator.CreateInstance(typeof(TConcreteRepository), args)!; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingEvaluator.cs index b2352cd3..7859f23a 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingEvaluator.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public class AsNoTrackingEvaluator : IEvaluator { - public class AsNoTrackingEvaluator : IEvaluator - { private AsNoTrackingEvaluator() { } public static AsNoTrackingEvaluator Instance { get; } = new AsNoTrackingEvaluator(); @@ -15,12 +11,11 @@ private AsNoTrackingEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.AsNoTracking) - { - query = query.AsNoTracking(); - } + if (specification.AsNoTracking) + { + query = query.AsNoTracking(); + } - return query; + return query; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingWithIdentityResolutionEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingWithIdentityResolutionEvaluator.cs index ccf87541..0db45cd0 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingWithIdentityResolutionEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsNoTrackingWithIdentityResolutionEvaluator.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public class AsNoTrackingWithIdentityResolutionEvaluator : IEvaluator { -#if !NETSTANDARD2_0 - public class AsNoTrackingWithIdentityResolutionEvaluator : IEvaluator - { private AsNoTrackingWithIdentityResolutionEvaluator() { } public static AsNoTrackingWithIdentityResolutionEvaluator Instance { get; } = new AsNoTrackingWithIdentityResolutionEvaluator(); @@ -16,13 +11,11 @@ private AsNoTrackingWithIdentityResolutionEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.AsNoTrackingWithIdentityResolution) - { - query = query.AsNoTrackingWithIdentityResolution(); - } + if (specification.AsNoTrackingWithIdentityResolution) + { + query = query.AsNoTrackingWithIdentityResolution(); + } - return query; + return query; } - } -#endif } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsSplitQueryEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsSplitQueryEvaluator.cs index e2ff7e3c..f50800f6 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsSplitQueryEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsSplitQueryEvaluator.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public class AsSplitQueryEvaluator : IEvaluator { -#if !NETSTANDARD2_0 - public class AsSplitQueryEvaluator : IEvaluator - { private AsSplitQueryEvaluator() { } public static AsSplitQueryEvaluator Instance { get; } = new AsSplitQueryEvaluator(); @@ -16,13 +11,11 @@ private AsSplitQueryEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.AsSplitQuery) - { - query = query.AsSplitQuery(); - } + if (specification.AsSplitQuery) + { + query = query.AsSplitQuery(); + } - return query; + return query; } - } -#endif } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs index f67273b2..1af2b43d 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/AsTrackingEvaluator.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public class AsTrackingEvaluator : IEvaluator { - public class AsTrackingEvaluator : IEvaluator - { private AsTrackingEvaluator() { } public static AsTrackingEvaluator Instance { get; } = new AsTrackingEvaluator(); @@ -15,12 +11,11 @@ private AsTrackingEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.AsTracking) - { - query = query.AsTracking(); - } + if (specification.AsTracking) + { + query = query.AsTracking(); + } - return query; + return query; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IgnoreQueryFiltersEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IgnoreQueryFiltersEvaluator.cs index b03b5128..91db6db5 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IgnoreQueryFiltersEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IgnoreQueryFiltersEvaluator.cs @@ -1,14 +1,13 @@ -using System.Linq; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +/// +/// This evaluator applies EF Core's IgnoreQueryFilters feature to a given query +/// See: https://docs.microsoft.com/en-us/ef/core/querying/filters +/// +public class IgnoreQueryFiltersEvaluator : IEvaluator { - /// - /// This evaluator applies EF Core's IgnoreQueryFilters feature to a given query - /// See: https://docs.microsoft.com/en-us/ef/core/querying/filters - /// - public class IgnoreQueryFiltersEvaluator : IEvaluator - { private IgnoreQueryFiltersEvaluator() { } public static IgnoreQueryFiltersEvaluator Instance { get; } = new IgnoreQueryFiltersEvaluator(); @@ -16,12 +15,11 @@ private IgnoreQueryFiltersEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.IgnoreQueryFilters) - { - query = query.IgnoreQueryFilters(); - } + if (specification.IgnoreQueryFilters) + { + query = query.IgnoreQueryFilters(); + } - return query; + return query; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IncludeEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IncludeEvaluator.cs index e63a0bd3..2a42bfce 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IncludeEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/IncludeEvaluator.cs @@ -1,22 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Query; using System.Linq.Expressions; using System.Reflection; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Query; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public class IncludeEvaluator : IEvaluator { - public class IncludeEvaluator : IEvaluator - { - private static readonly MethodInfo IncludeMethodInfo = typeof(EntityFrameworkQueryableExtensions) + private static readonly MethodInfo _includeMethodInfo = typeof(EntityFrameworkQueryableExtensions) .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.Include)) .Single(mi => mi.GetGenericArguments().Length == 2 && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>) && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); - private static readonly MethodInfo ThenIncludeAfterReferenceMethodInfo + private static readonly MethodInfo _thenIncludeAfterReferenceMethodInfo = typeof(EntityFrameworkQueryableExtensions) .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) .Single(mi => mi.GetGenericArguments().Length == 3 @@ -24,29 +21,28 @@ private static readonly MethodInfo ThenIncludeAfterReferenceMethodInfo && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); - private static readonly MethodInfo ThenIncludeAfterEnumerableMethodInfo + private static readonly MethodInfo _thenIncludeAfterEnumerableMethodInfo = typeof(EntityFrameworkQueryableExtensions) .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) .Where(mi => mi.GetGenericArguments().Length == 3) .Single( mi => { - var typeInfo = mi.GetParameters()[0].ParameterType.GenericTypeArguments[1]; + var typeInfo = mi.GetParameters()[0].ParameterType.GenericTypeArguments[1]; - return typeInfo.IsGenericType + return typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>) && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>); }); - private static readonly CachedReadConcurrentDictionary<(Type EntityType, Type PropertyType, Type? PreviousPropertyType), Lazy>> DelegatesCache = - new CachedReadConcurrentDictionary<(Type EntityType, Type PropertyType, Type? PreviousPropertyType), Lazy>>(); + private static readonly CachedReadConcurrentDictionary<(Type EntityType, Type PropertyType, Type? PreviousPropertyType), Lazy>> _delegatesCache = new(); - private readonly bool cacheEnabled; + private readonly bool _cacheEnabled; private IncludeEvaluator(bool cacheEnabled) { - this.cacheEnabled = cacheEnabled; + _cacheEnabled = cacheEnabled; } /// @@ -63,82 +59,82 @@ private IncludeEvaluator(bool cacheEnabled) public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - foreach (var includeString in specification.IncludeStrings) - { - query = query.Include(includeString); - } - - foreach (var includeInfo in specification.IncludeExpressions) - { - if (includeInfo.Type == IncludeTypeEnum.Include) + foreach (var includeString in specification.IncludeStrings) { - query = this.BuildInclude(query, includeInfo); + query = query.Include(includeString); } - else if (includeInfo.Type == IncludeTypeEnum.ThenInclude) + + foreach (var includeInfo in specification.IncludeExpressions) { - query = this.BuildThenInclude(query, includeInfo); + if (includeInfo.Type == IncludeTypeEnum.Include) + { + query = BuildInclude(query, includeInfo); + } + else if (includeInfo.Type == IncludeTypeEnum.ThenInclude) + { + query = BuildThenInclude(query, includeInfo); + } } - } - return query; + return query; } private IQueryable BuildInclude(IQueryable query, IncludeExpressionInfo includeInfo) { - _ = includeInfo ?? throw new ArgumentNullException(nameof(includeInfo)); + _ = includeInfo ?? throw new ArgumentNullException(nameof(includeInfo)); - if (!this.cacheEnabled) - { - var result = IncludeMethodInfo.MakeGenericMethod(includeInfo.EntityType, includeInfo.PropertyType).Invoke(null, new object[] { query, includeInfo.LambdaExpression }); + if (!_cacheEnabled) + { + var result = _includeMethodInfo.MakeGenericMethod(includeInfo.EntityType, includeInfo.PropertyType).Invoke(null, new object[] { query, includeInfo.LambdaExpression }); - _ = result ?? throw new TargetException(); + _ = result ?? throw new TargetException(); - return (IQueryable)result; - } + return (IQueryable)result; + } - var include = DelegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, null), CreateIncludeDelegate).Value; + var include = _delegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, null), CreateIncludeDelegate).Value; - return (IQueryable)include(query, includeInfo.LambdaExpression); + return (IQueryable)include(query, includeInfo.LambdaExpression); } private IQueryable BuildThenInclude(IQueryable query, IncludeExpressionInfo includeInfo) { - _ = includeInfo ?? throw new ArgumentNullException(nameof(includeInfo)); - _ = includeInfo.PreviousPropertyType ?? throw new ArgumentNullException(nameof(includeInfo.PreviousPropertyType)); + _ = includeInfo ?? throw new ArgumentNullException(nameof(includeInfo)); + _ = includeInfo.PreviousPropertyType ?? throw new ArgumentNullException(nameof(includeInfo.PreviousPropertyType)); - if (!this.cacheEnabled) - { - var result = (IsGenericEnumerable(includeInfo.PreviousPropertyType, out var previousPropertyType) - ? ThenIncludeAfterEnumerableMethodInfo - : ThenIncludeAfterReferenceMethodInfo).MakeGenericMethod(includeInfo.EntityType, previousPropertyType, includeInfo.PropertyType) - .Invoke(null, new object[] { query, includeInfo.LambdaExpression, }); + if (!_cacheEnabled) + { + var result = (IsGenericEnumerable(includeInfo.PreviousPropertyType, out var previousPropertyType) + ? _thenIncludeAfterEnumerableMethodInfo + : _thenIncludeAfterReferenceMethodInfo).MakeGenericMethod(includeInfo.EntityType, previousPropertyType, includeInfo.PropertyType) + .Invoke(null, new object[] { query, includeInfo.LambdaExpression, }); - _ = result ?? throw new TargetException(); + _ = result ?? throw new TargetException(); - return (IQueryable)result; - } + return (IQueryable)result; + } - var thenInclude = DelegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, includeInfo.PreviousPropertyType), CreateThenIncludeDelegate).Value; + var thenInclude = _delegatesCache.GetOrAdd((includeInfo.EntityType, includeInfo.PropertyType, includeInfo.PreviousPropertyType), CreateThenIncludeDelegate).Value; - return (IQueryable)thenInclude(query, includeInfo.LambdaExpression); + return (IQueryable)thenInclude(query, includeInfo.LambdaExpression); } // (source, selector) => EntityFrameworkQueryableExtensions.Include((IQueryable)source, (Expression>)selector); private static Lazy> CreateIncludeDelegate((Type EntityType, Type PropertyType, Type? PreviousPropertyType) cacheKey) - => new Lazy>(() => + => new(() => { - var concreteInclude = IncludeMethodInfo.MakeGenericMethod(cacheKey.EntityType, cacheKey.PropertyType); - var sourceParameter = Expression.Parameter(typeof(IQueryable)); - var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); + var concreteInclude = _includeMethodInfo.MakeGenericMethod(cacheKey.EntityType, cacheKey.PropertyType); + var sourceParameter = Expression.Parameter(typeof(IQueryable)); + var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); - var call = Expression.Call( + var call = Expression.Call( concreteInclude, Expression.Convert(sourceParameter, typeof(IQueryable<>).MakeGenericType(cacheKey.EntityType)), Expression.Convert(selectorParameter, typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(cacheKey.EntityType, cacheKey.PropertyType)))); - var lambda = Expression.Lambda>(call, sourceParameter, selectorParameter); + var lambda = Expression.Lambda>(call, sourceParameter, selectorParameter); - return lambda.Compile(); + return lambda.Compile(); }); // ((source, selector) => @@ -150,44 +146,43 @@ private static Lazy> CreateInclud // (IIncludableQueryable>)source, // (Expression>)selector); private static Lazy> CreateThenIncludeDelegate((Type EntityType, Type PropertyType, Type? PreviousPropertyType) cacheKey) - => new Lazy>(() => + => new(() => { - _ = cacheKey.PreviousPropertyType ?? throw new ArgumentNullException(nameof(cacheKey.PreviousPropertyType)); + _ = cacheKey.PreviousPropertyType ?? throw new ArgumentNullException(nameof(cacheKey.PreviousPropertyType)); - MethodInfo thenIncludeInfo = ThenIncludeAfterReferenceMethodInfo; - if (IsGenericEnumerable(cacheKey.PreviousPropertyType, out var previousPropertyType)) - { - thenIncludeInfo = ThenIncludeAfterEnumerableMethodInfo; - } + var thenIncludeInfo = _thenIncludeAfterReferenceMethodInfo; + if (IsGenericEnumerable(cacheKey.PreviousPropertyType, out var previousPropertyType)) + { + thenIncludeInfo = _thenIncludeAfterEnumerableMethodInfo; + } - var concreteThenInclude = thenIncludeInfo.MakeGenericMethod(cacheKey.EntityType, previousPropertyType, cacheKey.PropertyType); - var sourceParameter = Expression.Parameter(typeof(IQueryable)); - var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); + var concreteThenInclude = thenIncludeInfo.MakeGenericMethod(cacheKey.EntityType, previousPropertyType, cacheKey.PropertyType); + var sourceParameter = Expression.Parameter(typeof(IQueryable)); + var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); - var call = Expression.Call( + var call = Expression.Call( concreteThenInclude, Expression.Convert( sourceParameter, typeof(IIncludableQueryable<,>).MakeGenericType(cacheKey.EntityType, cacheKey.PreviousPropertyType)), // cacheKey.PreviousPropertyType must be exact type, not generic type argument Expression.Convert(selectorParameter, typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(previousPropertyType, cacheKey.PropertyType)))); - var lambda = Expression.Lambda>(call, sourceParameter, selectorParameter); + var lambda = Expression.Lambda>(call, sourceParameter, selectorParameter); - return lambda.Compile(); + return lambda.Compile(); }); private static bool IsGenericEnumerable(Type type, out Type propertyType) { - if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)) - { - propertyType = type.GenericTypeArguments[0]; + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)) + { + propertyType = type.GenericTypeArguments[0]; - return true; - } + return true; + } - propertyType = type; + propertyType = type; - return false; + return false; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SearchEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SearchEvaluator.cs index 121d4319..a5bbf0af 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SearchEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SearchEvaluator.cs @@ -1,9 +1,7 @@ -using System.Linq; +namespace Ardalis.Specification.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +public class SearchEvaluator : IEvaluator { - public class SearchEvaluator : IEvaluator - { private SearchEvaluator() { } public static SearchEvaluator Instance { get; } = new SearchEvaluator(); @@ -11,12 +9,11 @@ private SearchEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - foreach (var searchCriteria in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) - { - query = query.Search(searchCriteria); - } + foreach (var searchCriteria in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) + { + query = query.Search(searchCriteria); + } - return query; + return query; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs index 559e5786..5434ac11 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Evaluators/SpecificationEvaluator.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; +namespace Ardalis.Specification.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +/// +public class SpecificationEvaluator : ISpecificationEvaluator { - /// - public class SpecificationEvaluator : ISpecificationEvaluator - { // Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided evaluators. /// /// instance with default evaluators and without any additional features enabled. @@ -22,53 +18,52 @@ public class SpecificationEvaluator : ISpecificationEvaluator public SpecificationEvaluator(bool cacheEnabled = false) { - this.Evaluators.AddRange(new IEvaluator[] - { - WhereEvaluator.Instance, - SearchEvaluator.Instance, - cacheEnabled ? IncludeEvaluator.Cached : IncludeEvaluator.Default, - OrderEvaluator.Instance, - PaginationEvaluator.Instance, - AsNoTrackingEvaluator.Instance, - AsNoTrackingWithIdentityResolutionEvaluator.Instance, - AsTrackingEvaluator.Instance, - IgnoreQueryFiltersEvaluator.Instance, - AsSplitQueryEvaluator.Instance - }); + Evaluators.AddRange(new IEvaluator[] + { + WhereEvaluator.Instance, + SearchEvaluator.Instance, + cacheEnabled ? IncludeEvaluator.Cached : IncludeEvaluator.Default, + OrderEvaluator.Instance, + PaginationEvaluator.Instance, + AsNoTrackingEvaluator.Instance, + AsNoTrackingWithIdentityResolutionEvaluator.Instance, + AsTrackingEvaluator.Instance, + IgnoreQueryFiltersEvaluator.Instance, + AsSplitQueryEvaluator.Instance + }); } public SpecificationEvaluator(IEnumerable evaluators) { - this.Evaluators.AddRange(evaluators); + Evaluators.AddRange(evaluators); } /// public virtual IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification is null) throw new ArgumentNullException("Specification is required"); - if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); - if (specification.Selector != null && specification.SelectorMany != null) throw new ConcurrentSelectorsException(); + if (specification is null) throw new ArgumentNullException(nameof(specification)); + if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); + if (specification.Selector is not null && specification.SelectorMany is not null) throw new ConcurrentSelectorsException(); - query = GetQuery(query, (ISpecification)specification); + query = GetQuery(query, (ISpecification)specification); - return specification.Selector is not null - ? query.Select(specification.Selector) - : query.SelectMany(specification.SelectorMany!); + return specification.Selector is not null + ? query.Select(specification.Selector) + : query.SelectMany(specification.SelectorMany!); } /// public virtual IQueryable GetQuery(IQueryable query, ISpecification specification, bool evaluateCriteriaOnly = false) where T : class { - if (specification is null) throw new ArgumentNullException("Specification is required"); + if (specification is null) throw new ArgumentNullException(nameof(specification)); - var evaluators = evaluateCriteriaOnly ? this.Evaluators.Where(x => x.IsCriteriaEvaluator) : this.Evaluators; + var evaluators = evaluateCriteriaOnly ? Evaluators.Where(x => x.IsCriteriaEvaluator) : Evaluators; - foreach (var evaluator in evaluators) - { - query = evaluator.GetQuery(query, specification); - } + foreach (var evaluator in evaluators) + { + query = evaluator.GetQuery(query, specification); + } - return query; + return query; } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs index ed13f61a..e0c9e197 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/DbSetExtensions.cs @@ -1,24 +1,20 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public static class DbSetExtensions { - public static class DbSetExtensions - { public static async Task> ToListAsync( this DbSet source, ISpecification specification, CancellationToken cancellationToken = default) where TSource : class { - var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); + var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null - ? result - : specification.PostProcessingAction(result).ToList(); + return specification.PostProcessingAction == null + ? result + : specification.PostProcessingAction(result).ToList(); } public static async Task> ToEnumerableAsync( @@ -27,11 +23,11 @@ public static async Task> ToEnumerableAsync( CancellationToken cancellationToken = default) where TSource : class { - var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); + var result = await SpecificationEvaluator.Default.GetQuery(source, specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null - ? result - : specification.PostProcessingAction(result); + return specification.PostProcessingAction == null + ? result + : specification.PostProcessingAction(result); } public static IQueryable WithSpecification( @@ -40,8 +36,8 @@ public static IQueryable WithSpecification( ISpecificationEvaluator? evaluator = null) where TSource : class { - evaluator = evaluator ?? SpecificationEvaluator.Default; - return evaluator.GetQuery(source, specification); + evaluator ??= SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); } public static IQueryable WithSpecification( @@ -50,8 +46,7 @@ public static IQueryable WithSpecification( ISpecificationEvaluator? evaluator = null) where TSource : class { - evaluator = evaluator ?? SpecificationEvaluator.Default; - return evaluator.GetQuery(source, specification); + evaluator ??= SpecificationEvaluator.Default; + return evaluator.GetQuery(source, specification); } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/ParameterReplacerVisitor.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/ParameterReplacerVisitor.cs index 9c2e7b09..74823678 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/ParameterReplacerVisitor.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/ParameterReplacerVisitor.cs @@ -1,33 +1,21 @@ using System.Linq.Expressions; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +internal class ParameterReplacerVisitor : ExpressionVisitor { - internal class ParameterReplacerVisitor : ExpressionVisitor - { - private readonly Expression newExpression; - private readonly ParameterExpression oldParameter; + private readonly Expression _newExpression; + private readonly ParameterExpression _oldParameter; private ParameterReplacerVisitor(ParameterExpression oldParameter, Expression newExpression) { - this.oldParameter = oldParameter; - this.newExpression = newExpression; + _oldParameter = oldParameter; + _newExpression = newExpression; } internal static Expression Replace(Expression expression, ParameterExpression oldParameter, Expression newExpression) - { - return new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression); - } + => new ParameterReplacerVisitor(oldParameter, newExpression).Visit(expression); protected override Expression VisitParameter(ParameterExpression p) - { - if (p == oldParameter) - { - return newExpression; - } - else - { - return p; - } - } - } + => p == _oldParameter ? _newExpression : p; } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/SearchExtension.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/SearchExtension.cs index 51ca508b..dd199fc2 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/SearchExtension.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/Extensions/SearchExtension.cs @@ -1,20 +1,17 @@ -using System; -using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; using System.Data; -using System.Linq; using System.Linq.Expressions; using System.Reflection; -using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +public static class SearchExtension { - public static class SearchExtension - { - private static readonly MethodInfo LikeMethodInfo = typeof(DbFunctionsExtensions) + private static readonly MethodInfo _likeMethodInfo = typeof(DbFunctionsExtensions) .GetMethod(nameof(DbFunctionsExtensions.Like), new Type[] { typeof(DbFunctions), typeof(string), typeof(string) }) ?? throw new TargetException("The EF.Functions.Like not found"); - private static readonly MemberExpression Functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions)) + private static readonly MemberExpression _functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions)) ?? throw new TargetException("The EF.Functions not found!")); /// @@ -31,33 +28,32 @@ public static class SearchExtension /// public static IQueryable Search(this IQueryable source, IEnumerable> criterias) { - Expression? expr = null; - var parameter = Expression.Parameter(typeof(T), "x"); + Expression? expr = null; + var parameter = Expression.Parameter(typeof(T), "x"); - foreach (var criteria in criterias) - { - if (string.IsNullOrEmpty(criteria.SearchTerm)) - continue; + foreach (var criteria in criterias) + { + if (string.IsNullOrEmpty(criteria.SearchTerm)) + continue; - var propertySelector = ParameterReplacerVisitor.Replace(criteria.Selector, criteria.Selector.Parameters[0], parameter) as LambdaExpression; - _ = propertySelector ?? throw new InvalidExpressionException(); + var propertySelector = ParameterReplacerVisitor.Replace(criteria.Selector, criteria.Selector.Parameters[0], parameter) as LambdaExpression; + _ = propertySelector ?? throw new InvalidExpressionException(); - // Create a closure - var searchTermAsExpression = ((Expression>)(() => criteria.SearchTerm)).Body; + // Create a closure + var searchTermAsExpression = ((Expression>)(() => criteria.SearchTerm)).Body; - var likeExpression = Expression.Call( - null, - LikeMethodInfo, - Functions, - propertySelector.Body, - searchTermAsExpression); + var likeExpression = Expression.Call( + null, + _likeMethodInfo, + _functions, + propertySelector.Body, + searchTermAsExpression); - expr = expr == null ? (Expression)likeExpression : Expression.OrElse(expr, likeExpression); - } + expr = expr == null ? (Expression)likeExpression : Expression.OrElse(expr, likeExpression); + } - return expr == null - ? source - : source.Where(Expression.Lambda>(expr, parameter)); + return expr == null + ? source + : source.Where(Expression.Lambda>(expr, parameter)); } - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/IRepositoryFactory.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/IRepositoryFactory.cs index 257b9d27..62fc343e 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/IRepositoryFactory.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/IRepositoryFactory.cs @@ -1,18 +1,17 @@ -namespace Ardalis.Specification.EntityFrameworkCore +namespace Ardalis.Specification.EntityFrameworkCore; + +/// +/// Generates new instances of to encapsulate the 'Unit of Work' pattern +/// in scenarios where injected types may be long-lived (e.g. Blazor) +/// +/// +/// The Interface of the Repository to be generated. +/// +public interface IRepositoryFactory { - /// - /// Generates new instances of to encapsulate the 'Unit of Work' pattern - /// in scenarios where injected types may be long-lived (e.g. Blazor) - /// - /// - /// The Interface of the Repository to be generated. - /// - public interface IRepositoryFactory - { /// /// Generates a new repository instance /// /// The generated repository instance public TRepository CreateRepository(); - } } diff --git a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs index 564b6dab..36ce58c9 100644 --- a/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs +++ b/Specification.EntityFrameworkCore/src/Ardalis.Specification.EntityFrameworkCore/RepositoryBaseOfT.cs @@ -1,17 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace Ardalis.Specification.EntityFrameworkCore +using Microsoft.EntityFrameworkCore; + +namespace Ardalis.Specification.EntityFrameworkCore; + +/// +public abstract class RepositoryBase : IRepositoryBase where T : class { - /// - public abstract class RepositoryBase : IRepositoryBase where T : class - { - private readonly DbContext dbContext; - private readonly ISpecificationEvaluator specificationEvaluator; + private readonly DbContext _dbContext; + private readonly ISpecificationEvaluator _specificationEvaluator; public RepositoryBase(DbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) @@ -21,162 +16,162 @@ public RepositoryBase(DbContext dbContext) /// public RepositoryBase(DbContext dbContext, ISpecificationEvaluator specificationEvaluator) { - this.dbContext = dbContext; - this.specificationEvaluator = specificationEvaluator; + _dbContext = dbContext; + _specificationEvaluator = specificationEvaluator; } /// public virtual async Task AddAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Set().Add(entity); + _dbContext.Set().Add(entity); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); - return entity; + return entity; } /// public virtual async Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - dbContext.Set().AddRange(entities); + _dbContext.Set().AddRange(entities); await SaveChangesAsync(cancellationToken); return entities; } - + /// public virtual async Task UpdateAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Set().Update(entity); + _dbContext.Set().Update(entity); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task UpdateRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - dbContext.Set().UpdateRange(entities); + _dbContext.Set().UpdateRange(entities); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task DeleteAsync(T entity, CancellationToken cancellationToken = default) { - dbContext.Set().Remove(entity); + _dbContext.Set().Remove(entity); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } /// public virtual async Task DeleteRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default) { - dbContext.Set().RemoveRange(entities); + _dbContext.Set().RemoveRange(entities); - await SaveChangesAsync(cancellationToken); + await SaveChangesAsync(cancellationToken); } - + /// public virtual async Task SaveChangesAsync(CancellationToken cancellationToken = default) { - return await dbContext.SaveChangesAsync(cancellationToken); + return await _dbContext.SaveChangesAsync(cancellationToken); } /// public virtual async Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) where TId : notnull { - return await dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); + return await _dbContext.Set().FindAsync(new object[] { id }, cancellationToken: cancellationToken); } /// [Obsolete] public virtual async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// [Obsolete] public virtual async Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).FirstOrDefaultAsync(cancellationToken); } /// public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); } /// public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); + return await ApplySpecification(specification).SingleOrDefaultAsync(cancellationToken); } /// public virtual async Task> ListAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().ToListAsync(cancellationToken); + return await _dbContext.Set().ToListAsync(cancellationToken); } /// public virtual async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); + var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } /// public virtual async Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) { - var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); + var queryResult = await ApplySpecification(specification).ToListAsync(cancellationToken); - return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); + return specification.PostProcessingAction == null ? queryResult : specification.PostProcessingAction(queryResult).ToList(); } /// public virtual async Task CountAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification, true).CountAsync(cancellationToken); + return await ApplySpecification(specification, true).CountAsync(cancellationToken); } /// public virtual async Task CountAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().CountAsync(cancellationToken); + return await _dbContext.Set().CountAsync(cancellationToken); } /// public virtual async Task AnyAsync(ISpecification specification, CancellationToken cancellationToken = default) { - return await ApplySpecification(specification, true).AnyAsync(cancellationToken); + return await ApplySpecification(specification, true).AnyAsync(cancellationToken); } /// public virtual async Task AnyAsync(CancellationToken cancellationToken = default) { - return await dbContext.Set().AnyAsync(cancellationToken); + return await _dbContext.Set().AnyAsync(cancellationToken); } /// public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specification) { - return ApplySpecification(specification).AsAsyncEnumerable(); + return ApplySpecification(specification).AsAsyncEnumerable(); } /// @@ -187,7 +182,7 @@ public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specifica /// The filtered entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification, bool evaluateCriteriaOnly = false) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); + return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification, evaluateCriteriaOnly); } /// @@ -202,7 +197,6 @@ protected virtual IQueryable ApplySpecification(ISpecification specificati /// The filtered projected entities as an . protected virtual IQueryable ApplySpecification(ISpecification specification) { - return specificationEvaluator.GetQuery(dbContext.Set().AsQueryable(), specification); + return _specificationEvaluator.GetQuery(_dbContext.Set().AsQueryable(), specification); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj index 939e1773..6dd63555 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests.csproj @@ -1,27 +1,27 @@  - net6.0 + net7.0 + 11.0 + disable + enable false - 9.0 - enable - - - runtime; build; native; contentfiles; analyzers + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -32,4 +32,8 @@ + + 1701;1702;1591;1573;0612 + + diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs index c98c4c4e..7ed719c5 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/ContextFactoryRepositoryBaseOfTTests.cs @@ -1,150 +1,146 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class ContextFactoryRepositoryBaseOfTTests : IClassFixture { - public class ContextFactoryRepositoryBaseOfTTests : IClassFixture - { protected TestDbContext dbContext; protected IServiceProvider serviceProvider; protected ContextFactoryRepository repository; public ContextFactoryRepositoryBaseOfTTests(SharedDatabaseFixture fixture) { - dbContext = fixture.CreateContext(); + dbContext = fixture.CreateContext(); - serviceProvider = new ServiceCollection() - .AddDbContextFactory((builder => builder.UseSqlServer(fixture.Connection)), - ServiceLifetime.Transient).BuildServiceProvider(); + serviceProvider = new ServiceCollection() + .AddDbContextFactory((builder => builder.UseSqlServer(fixture.Connection)), + ServiceLifetime.Transient).BuildServiceProvider(); - var contextFactory = serviceProvider.GetService>(); - repository = new ContextFactoryRepository(contextFactory); + var contextFactory = serviceProvider.GetService>(); + repository = new ContextFactoryRepository(contextFactory); } [Fact] public async Task Saves_new_entity() { - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company(); - company.Name = "Test save new company name"; - company.CountryId = country.Id; + var company = new Company(); + company.Name = "Test save new company name"; + company.CountryId = country.Id; - await repository.AddAsync(company); - Assert.NotEqual(0, company.Id); + await repository.AddAsync(company); + Assert.NotEqual(0, company.Id); } [Fact] public async Task Updates_existing_entity() { - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; - await repository.AddAsync(company); + var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; + await repository.AddAsync(company); - var existingCompany = await repository.GetByIdAsync(company.Id); - existingCompany.Name = "Updated company name"; - await repository.UpdateAsync(existingCompany); + var existingCompany = await repository.GetByIdAsync(company.Id); + existingCompany.Name = "Updated company name"; + await repository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); } [Fact] public async Task Updates_existing_entity_across_context_instances() { - var contextFactory = serviceProvider.GetService>(); - var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); - var companySaveRepository = new ContextFactoryRepository(contextFactory); + var contextFactory = serviceProvider.GetService>(); + var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); + var companySaveRepository = new ContextFactoryRepository(contextFactory); - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; - await repository.AddAsync(company); + var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; + await repository.AddAsync(company); - var existingCompany = await companyRetrievalRepository.GetByIdAsync(company.Id); - existingCompany.Name = "Updated company name"; - await companySaveRepository.UpdateAsync(existingCompany); + var existingCompany = await companyRetrievalRepository.GetByIdAsync(company.Id); + existingCompany.Name = "Updated company name"; + await companySaveRepository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); } [Fact] public async Task Updates_graph() { - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update graph", CountryId = country.Id }; - var store = new Store { Name = "Store Number 1" }; - company.Stores.Add(store); + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + var store = new Store { Name = "Store Number 1" }; + company.Stores.Add(store); - await repository.AddAsync(company); + await repository.AddAsync(company); - var spec = new GetCompanyWithStoresSpec(company.Id); - var existingCompany = await repository.FirstOrDefaultAsync(spec); - existingCompany.Name = "Updated company name"; - var existingStore = existingCompany.Stores.FirstOrDefault(); - existingStore.Name = "Updated Store Name"; + var spec = new GetCompanyWithStoresSpec(company.Id); + var existingCompany = await repository.FirstOrDefaultAsync(spec); + existingCompany.Name = "Updated company name"; + var existingStore = existingCompany.Stores.FirstOrDefault(); + existingStore.Name = "Updated Store Name"; - await repository.UpdateAsync(existingCompany); + await repository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); - var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); - Assert.Equal(validationStore.Name, existingStore.Name); + var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); + Assert.Equal(validationStore.Name, existingStore.Name); } [Fact] public async Task Updates_graph_across_context_instances() { - var contextFactory = serviceProvider.GetService>(); - var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); - var companySaveRepository = new ContextFactoryRepository(contextFactory); + var contextFactory = serviceProvider.GetService>(); + var companyRetrievalRepository = new ContextFactoryRepository(contextFactory); + var companySaveRepository = new ContextFactoryRepository(contextFactory); - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update graph", CountryId = country.Id }; - var store = new Store { Name = "Store Number 1" }; - company.Stores.Add(store); + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + var store = new Store { Name = "Store Number 1" }; + company.Stores.Add(store); - await repository.AddAsync(company); + await repository.AddAsync(company); - var spec = new GetCompanyWithStoresSpec(company.Id); - var existingCompany = await companyRetrievalRepository.FirstOrDefaultAsync(spec); - existingCompany.Name = "Updated company name"; - var existingStore = existingCompany.Stores.FirstOrDefault(); - existingStore.Name = "Updated Store Name"; + var spec = new GetCompanyWithStoresSpec(company.Id); + var existingCompany = await companyRetrievalRepository.FirstOrDefaultAsync(spec); + existingCompany.Name = "Updated company name"; + var existingStore = existingCompany.Stores.FirstOrDefault(); + existingStore.Name = "Updated Store Name"; - await companySaveRepository.UpdateAsync(existingCompany); + await companySaveRepository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); - var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); - Assert.Equal(validationStore.Name, existingStore.Name); + var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); + Assert.Equal(validationStore.Name, existingStore.Name); } [Fact] public async Task Deletes_entity() { - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update graph", CountryId = country.Id }; - await repository.AddAsync(company); + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + await repository.AddAsync(company); - var companyId = company.Id; - await repository.DeleteAsync(company); + var companyId = company.Id; + await repository.DeleteAsync(company); - var validationCompany = await repository.GetByIdAsync(companyId); - Assert.Null(validationCompany); + var validationCompany = await repository.GetByIdAsync(companyId); + Assert.Null(validationCompany); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs index c30df303..18d66fac 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/EFRepositoryFactoryTests.cs @@ -1,17 +1,14 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Internal; using Microsoft.Extensions.DependencyInjection; +using Moq; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class EFRepositoryFactoryTests : IClassFixture { - public class EFRepositoryFactoryTests : IClassFixture - { protected TestDbContext dbContext; protected IServiceProvider serviceProvider; protected IRepositoryFactory> repositoryFactory; @@ -19,73 +16,110 @@ public class EFRepositoryFactoryTests : IClassFixture public EFRepositoryFactoryTests(SharedDatabaseFixture fixture) { - dbContext = fixture.CreateContext(); - - serviceProvider = new ServiceCollection() - .AddDbContextFactory((builder => builder.UseSqlServer(fixture.Connection)), - ServiceLifetime.Transient).BuildServiceProvider(); - - contextFactory = serviceProvider.GetService>(); - repositoryFactory = - new EFRepositoryFactory, Repository, TestDbContext>(contextFactory); + dbContext = fixture.CreateContext(); + + serviceProvider = new ServiceCollection() + .AddDbContextFactory((builder => builder.UseSqlServer(fixture.Connection)), + ServiceLifetime.Transient).BuildServiceProvider(); + + contextFactory = serviceProvider.GetService>(); + repositoryFactory = + new EFRepositoryFactory, Repository, TestDbContext>(contextFactory); } - + + [Fact] + public void CorrectlyInstantiatesRepository() + { + var mockContextFactory = new Mock>(); + mockContextFactory.Setup(x => x.CreateDbContext()) + .Returns(() => new SampleDbContext(new DbContextOptions())); + + var repositoryFactory = + new EFRepositoryFactory, MyRepository, SampleDbContext>(mockContextFactory + .Object); + + var repository = repositoryFactory.CreateRepository(); + Assert.IsType>(repository); + } + [Fact] public async Task Saves_new_entity() { - var repository = repositoryFactory.CreateRepository(); - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company(); - company.Name = "Test save new company name"; - company.CountryId = country.Id; + var company = new Company(); + company.Name = "Test save new company name"; + company.CountryId = country.Id; - await repository.AddAsync(company); - Assert.NotEqual(0, company.Id); + await repository.AddAsync(company); + Assert.NotEqual(0, company.Id); } [Fact] public async Task Updates_existing_entity() { - var repository = repositoryFactory.CreateRepository(); - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; - await repository.AddAsync(company); + var company = new Company { Name = "Test update existing company name", CountryId = country.Id }; + await repository.AddAsync(company); - var existingCompany = await repository.GetByIdAsync(company.Id); - existingCompany.Name = "Updated company name"; - await repository.UpdateAsync(existingCompany); + var existingCompany = await repository.GetByIdAsync(company.Id); + existingCompany.Name = "Updated company name"; + await repository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); } [Fact] public async Task Updates_graph() { - var repository = repositoryFactory.CreateRepository(); - var country = await dbContext.Countries.FirstOrDefaultAsync(); + var repository = repositoryFactory.CreateRepository(); + var country = await dbContext.Countries.FirstOrDefaultAsync(); - var company = new Company { Name = "Test update graph", CountryId = country.Id }; - var store = new Store { Name = "Store Number 1" }; - company.Stores.Add(store); + var company = new Company { Name = "Test update graph", CountryId = country.Id }; + var store = new Store { Name = "Store Number 1" }; + company.Stores.Add(store); - await repository.AddAsync(company); + await repository.AddAsync(company); - var spec = new GetCompanyWithStoresSpec(company.Id); - var existingCompany = await repository.FirstOrDefaultAsync(spec); - existingCompany.Name = "Updated company name"; - var existingStore = existingCompany.Stores.FirstOrDefault(); - existingStore.Name = "Updated Store Name"; + var spec = new GetCompanyWithStoresSpec(company.Id); + var existingCompany = await repository.FirstOrDefaultAsync(spec); + existingCompany.Name = "Updated company name"; + var existingStore = existingCompany.Stores.FirstOrDefault(); + existingStore.Name = "Updated Store Name"; - await repository.UpdateAsync(existingCompany); + await repository.UpdateAsync(existingCompany); - var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); - Assert.Equal(validationCompany.Name, existingCompany.Name); + var validationCompany = await dbContext.Companies.FirstOrDefaultAsync(x => x.Id == company.Id); + Assert.Equal(validationCompany.Name, existingCompany.Name); - var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); - Assert.Equal(validationStore.Name, existingStore.Name); + var validationStore = await dbContext.Stores.FirstOrDefaultAsync(x => x.CompanyId == company.Id); + Assert.Equal(validationStore.Name, existingStore.Name); + } + + public record Customer(int Id, string Name); + + public class SampleDbContext : DbContext + { + public DbSet Customers { get; set; } + + public SampleDbContext(DbContextOptions options) + : base(options) + { + } + } + + public interface IRepository : IRepositoryBase where T : class + { + } + + public class MyRepository : RepositoryBase, IRepository where T : class + { + public MyRepository(SampleDbContext dbContext) : base(dbContext) + { + } } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/ContextFactoryRepositoryOfT.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/ContextFactoryRepositoryOfT.cs index 80a4fd19..adc3cfee 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/ContextFactoryRepositoryOfT.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/ContextFactoryRepositoryOfT.cs @@ -1,10 +1,10 @@ using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +public class ContextFactoryRepository : ContextFactoryRepositoryBaseOfT + where T : class where TContext : DbContext { - public class ContextFactoryRepository : ContextFactoryRepositoryBaseOfT - where T : class where TContext : DbContext - { public ContextFactoryRepository(IDbContextFactory dbContextFactory) : base(dbContextFactory) { } @@ -13,5 +13,4 @@ public ContextFactoryRepository(IDbContextFactory dbContextFactory, ISpecificationEvaluator specificationEvaluator) : base(dbContextFactory, specificationEvaluator) { } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/GetCompanyWithStoresSpec.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/GetCompanyWithStoresSpec.cs index e4560e12..34ae57c6 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/GetCompanyWithStoresSpec.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/GetCompanyWithStoresSpec.cs @@ -1,12 +1,11 @@ using Ardalis.Specification.UnitTests.Fixture.Entities; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +public class GetCompanyWithStoresSpec : Specification, ISingleResultSpecification { - public class GetCompanyWithStoresSpec : Specification, ISingleResultSpecification - { public GetCompanyWithStoresSpec(int companyId) { - this.Query.Where(x => x.Id == companyId).Include(x => x.Stores); + Query.Where(x => x.Id == companyId).Include(x => x.Stores); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/IntegrationTestBase.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/IntegrationTestBase.cs index 51fca8c0..ff6e6f7f 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/IntegrationTestBase.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/IntegrationTestBase.cs @@ -1,20 +1,19 @@ using Ardalis.Specification.UnitTests.Fixture.Entities; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +public abstract class IntegrationTestBase : IClassFixture { - public abstract class IntegrationTestBase : IClassFixture - { protected TestDbContext dbContext; protected Repository companyRepository; protected Repository storeRepository; protected IntegrationTestBase(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) { - dbContext = fixture.CreateContext(); + dbContext = fixture.CreateContext(); - companyRepository = new Repository(dbContext, specificationEvaluator); - storeRepository = new Repository(dbContext, specificationEvaluator); + companyRepository = new Repository(dbContext, specificationEvaluator); + storeRepository = new Repository(dbContext, specificationEvaluator); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/LoggerFactoryProvider.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/LoggerFactoryProvider.cs index 42a357c8..addb289a 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/LoggerFactoryProvider.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/LoggerFactoryProvider.cs @@ -1,16 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +public class LoggerFactoryProvider { - public class LoggerFactoryProvider - { public static readonly ILoggerFactory LoggerFactoryInstance = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { - builder.AddFilter("Ardalis.Specification.EF", LogLevel.Debug); - builder.AddConsole(); + builder.AddFilter("Ardalis.Specification.EF", LogLevel.Debug); + builder.AddConsole(); }); - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs index cc82da8a..4d000e94 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/RepositoryOfT.cs @@ -1,8 +1,8 @@ -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +/// +public class Repository : RepositoryBase where T : class { - /// - public class Repository : RepositoryBase where T : class - { protected readonly TestDbContext dbContext; public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvaluator.Default) @@ -11,7 +11,6 @@ public Repository(TestDbContext dbContext) : this(dbContext, SpecificationEvalua public Repository(TestDbContext dbContext, ISpecificationEvaluator specificationEvaluator) : base(dbContext, specificationEvaluator) { - this.dbContext = dbContext; + this.dbContext = dbContext; } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/SharedDatabaseFixture.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/SharedDatabaseFixture.cs index 92eb4bb6..de338a3b 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/SharedDatabaseFixture.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/SharedDatabaseFixture.cs @@ -1,94 +1,87 @@ -using System; -using System.Collections.Generic; -using System.Data.Common; -using System.Text; -using MartinCostello.SqlLocalDb; +using MartinCostello.SqlLocalDb; using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; +using System.Data.Common; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture -{ - public class SharedDatabaseFixture : IDisposable - { - // Docker - public const string ConnectionStringDocker = "Data Source=databaseEFCore;Initial Catalog=SpecificationEFCoreTestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!"; +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; - // (localdb) - public const string ConnectionStringLocalDb = "Server=(localdb)\\mssqllocaldb;Integrated Security=SSPI;Initial Catalog=SpecificationEFTestsDB;ConnectRetryCount=0"; +public class SharedDatabaseFixture : IDisposable +{ + public const string _connectionStringDocker = "Data Source=databaseEFCore;Initial Catalog=SpecificationEFCoreTestsDB;PersistSecurityInfo=True;User ID=sa;Password=P@ssW0rd!;TrustServerCertificate=Yes"; + public const string _connectionStringLocalDb = "Server=(localdb)\\mssqllocaldb;Integrated Security=SSPI;Initial Catalog=SpecificationEFTestsDB;ConnectRetryCount=0"; - private static readonly object _lock = new object(); + private static readonly object _lock = new(); private static bool _databaseInitialized; public SharedDatabaseFixture() { - var isLocalDBInstalled = false; + var isLocalDBInstalled = false; - using (var localDB = new SqlLocalDbApi()) - { - isLocalDBInstalled = localDB.IsLocalDBInstalled(); - } + using (var localDB = new SqlLocalDbApi()) + { + isLocalDBInstalled = localDB.IsLocalDBInstalled(); + } - Connection = isLocalDBInstalled - ? new SqlConnection(ConnectionStringLocalDb) - : new SqlConnection(ConnectionStringDocker); + Connection = isLocalDBInstalled + ? new SqlConnection(_connectionStringLocalDb) + : new SqlConnection(_connectionStringDocker); - Seed(); + Seed(); - Connection.Open(); + Connection.Open(); } // This would work only if the DB already exists, otherwise obviously won't be able to open a connection. // Therefore, in the ctor we're using a Nuget package for this check, it's more robust. - private bool IsLocalDbAvailable1() + private static bool IsLocalDbAvailable1() { - try - { - using (var connection = new SqlConnection(ConnectionStringLocalDb)) + try { - connection.Open(); - connection.Close(); - } + using (var connection = new SqlConnection(_connectionStringLocalDb)) + { + connection.Open(); + connection.Close(); + } - return true; - } - catch (Exception) - { - return false; - } + return true; + } + catch (Exception) + { + return false; + } } public DbConnection Connection { get; } - public TestDbContext CreateContext(DbTransaction? transaction = null) + public TestDbContext CreateContext(DbTransaction transaction = null) { - var context = new TestDbContext(new DbContextOptionsBuilder().UseSqlServer(Connection).Options); + var context = new TestDbContext(new DbContextOptionsBuilder().UseSqlServer(Connection).Options); - if (transaction != null) - { - context.Database.UseTransaction(transaction); - } + if (transaction != null) + { + context.Database.UseTransaction(transaction); + } - return context; + return context; } private void Seed() { - lock (_lock) - { - if (!_databaseInitialized) + lock (_lock) { - using (var context = CreateContext()) - { - context.Database.EnsureDeleted(); - context.Database.EnsureCreated(); - } - - _databaseInitialized = true; + if (!_databaseInitialized) + { + using (var context = CreateContext()) + { + context.Database.EnsureDeleted(); + context.Database.EnsureCreated(); + } + + _databaseInitialized = true; + } } - } } public void Dispose() => Connection.Dispose(); - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/TestDbContext.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/TestDbContext.cs index 80edee5d..864b3e98 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/TestDbContext.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/Fixture/TestDbContext.cs @@ -1,19 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +using Ardalis.Specification.UnitTests.Fixture.Entities; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Microsoft.EntityFrameworkCore; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; + +public class TestDbContext : DbContext { - public class TestDbContext : DbContext - { - public DbSet? Countries { get; set; } - public DbSet? Companies { get; set; } - public DbSet? Stores { get; set; } - public DbSet
? Addresses { get; set; } - public DbSet? Products { get; set; } + public DbSet Countries => Set(); + public DbSet Companies => Set(); + public DbSet Stores => Set(); + public DbSet
Addresses => Set
(); + public DbSet Products => Set(); public TestDbContext(DbContextOptions options) : base(options) { @@ -21,21 +18,20 @@ public TestDbContext(DbContextOptions options) : base(options) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseLoggerFactory(LoggerFactoryProvider.LoggerFactoryInstance); - base.OnConfiguring(optionsBuilder); + optionsBuilder.UseLoggerFactory(LoggerFactoryProvider.LoggerFactoryInstance); + base.OnConfiguring(optionsBuilder); } protected override void OnModelCreating(ModelBuilder modelBuilder) { - base.OnModelCreating(modelBuilder); + base.OnModelCreating(modelBuilder); - modelBuilder.Entity().HasOne(x => x.Address).WithOne(x => x!.Store!).HasForeignKey
(x => x.StoreId); + modelBuilder.Entity().HasOne(x => x.Address).WithOne(x => x!.Store!).HasForeignKey
(x => x.StoreId); - modelBuilder.Entity().HasData(CountrySeed.Get()); - modelBuilder.Entity().HasData(CompanySeed.Get()); - modelBuilder.Entity
().HasData(AddressSeed.Get()); - modelBuilder.Entity().HasData(StoreSeed.Get()); - modelBuilder.Entity().HasData(ProductSeed.Get()); + modelBuilder.Entity().HasData(CountrySeed.Get()); + modelBuilder.Entity().HasData(CompanySeed.Get()); + modelBuilder.Entity
().HasData(AddressSeed.Get()); + modelBuilder.Entity().HasData(StoreSeed.Get()); + modelBuilder.Entity().HasData(ProductSeed.Get()); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AnyAsync.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AnyAsync.cs index 27adfaa2..c0193fc7 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AnyAsync.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AnyAsync.cs @@ -1,52 +1,50 @@ -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class RepositoryOfT_AnyAsync : RepositoryOfT_AnyAsync_TestKit { - public class RepositoryOfT_AnyAsync : RepositoryOfT_AnyAsync_TestKit - { public RepositoryOfT_AnyAsync(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default) { } - } +} - public class RepositoryOfT_AnyAsync_Cached : RepositoryOfT_AnyAsync_TestKit - { +public class RepositoryOfT_AnyAsync_Cached : RepositoryOfT_AnyAsync_TestKit +{ public RepositoryOfT_AnyAsync_Cached(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Cached) { } - } +} - public abstract class RepositoryOfT_AnyAsync_TestKit : IntegrationTestBase - { +public abstract class RepositoryOfT_AnyAsync_TestKit : IntegrationTestBase +{ protected RepositoryOfT_AnyAsync_TestKit(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) : base(fixture, specificationEvaluator) { } [Fact] public virtual async Task ReturnsTrueOnStoresRecords_WithoutSpec() { - var result = await storeRepository.AnyAsync(); + var result = await storeRepository.AnyAsync(); - result.Should().BeTrue(); + result.Should().BeTrue(); } [Fact] public virtual async Task ReturnsTrue_GivenStoreByIdSpecWithValidStore() { - var result = await storeRepository.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.AnyAsync(new StoreByIdSpec(StoreSeed.VALID_STORE_ID)); - result.Should().BeTrue(); + result.Should().BeTrue(); } [Fact] public virtual async Task ReturnsFalse_GivenStoreByIdSpecWithInvalidStore() { - var result = await storeRepository.AnyAsync(new StoreByIdSpec(0)); + var result = await storeRepository.AnyAsync(new StoreByIdSpec(0)); - result.Should().BeFalse(); + result.Should().BeFalse(); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AsAsyncEnumerable.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AsAsyncEnumerable.cs index 9825bea7..32615b82 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AsAsyncEnumerable.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_AsAsyncEnumerable.cs @@ -1,56 +1,52 @@ -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class RepositoryOfT_AsAsyncEnumerable : RepositoryOfT_AnyAsync_TestKit { - public class RepositoryOfT_AsAsyncEnumerable : RepositoryOfT_AnyAsync_TestKit - { public RepositoryOfT_AsAsyncEnumerable(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default) { } - } +} - public abstract class RepositoryOfT_AsAsyncEnumerable_TestKit : IntegrationTestBase - { +public abstract class RepositoryOfT_AsAsyncEnumerable_TestKit : IntegrationTestBase +{ protected RepositoryOfT_AsAsyncEnumerable_TestKit(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) : base(fixture, specificationEvaluator) { } - + [Fact] public virtual async Task ReturnsTrueOnStoresRecords_WithoutSpec() { - var results = storeRepository.AsAsyncEnumerable(new StoreIncludeProductsSpec()); + var results = storeRepository.AsAsyncEnumerable(new StoreIncludeProductsSpec()); - await foreach (var result in results.WithCancellation(CancellationToken.None)) - { - result.Should().NotBeNull(); - result.Products.Should().NotBeEmpty(); - } + await foreach (var result in results.WithCancellation(CancellationToken.None)) + { + result.Should().NotBeNull(); + result.Products.Should().NotBeEmpty(); + } } - + [Fact] public virtual async Task ReturnsStoreWithIdFrom15To30_GivenStoresByIdAsAsyncEnumerableSpec() { - var ids = Enumerable.Range(15, 16); - var spec = new StoresByIdListSpec(ids); - - int counter = 0; - var results = storeRepository.AsAsyncEnumerable(spec); - await foreach (var result in results.WithCancellation(CancellationToken.None)) - { - result.Should().NotBeNull(); - result.Products.Should().NotBeEmpty(); - ++counter; - } - - counter.Should().Be(16); + var ids = Enumerable.Range(15, 16); + var spec = new StoresByIdListSpec(ids); + + var counter = 0; + var results = storeRepository.AsAsyncEnumerable(spec); + await foreach (var result in results.WithCancellation(CancellationToken.None)) + { + result.Should().NotBeNull(); + result.Products.Should().NotBeEmpty(); + ++counter; + } + + counter.Should().Be(16); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetById.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetById.cs index 95b6bba0..74b55d07 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetById.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetById.cs @@ -1,45 +1,43 @@ -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using FluentAssertions; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class RepositoryOfT_GetById : RepositoryOfT_GetById_TestKit { - public class RepositoryOfT_GetById : RepositoryOfT_GetById_TestKit - { public RepositoryOfT_GetById(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default) { } - } +} - public class RepositoryOfT_GetById_Cached : RepositoryOfT_GetById_TestKit - { +public class RepositoryOfT_GetById_Cached : RepositoryOfT_GetById_TestKit +{ public RepositoryOfT_GetById_Cached(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Cached) { } - } +} - public abstract class RepositoryOfT_GetById_TestKit : IntegrationTestBase - { +public abstract class RepositoryOfT_GetById_TestKit : IntegrationTestBase +{ protected RepositoryOfT_GetById_TestKit(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) : base(fixture, specificationEvaluator) { } [Fact] public virtual async Task ReturnsStore_GivenId() { - var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); + var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); } [Fact] public virtual async Task ReturnsStore_GivenGenericId() { - var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); + var result = await storeRepository.GetByIdAsync(StoreSeed.VALID_STORE_ID); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetBySpec.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetBySpec.cs index 7804728b..afab280c 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetBySpec.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_GetBySpec.cs @@ -1,117 +1,114 @@ -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class RepositoryOfT_GetBySpec : RepositoryOfT_GetBySpec_TestKit { - public class RepositoryOfT_GetBySpec : RepositoryOfT_GetBySpec_TestKit - { public RepositoryOfT_GetBySpec(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default) { } - } +} - public class RepositoryOfT_GetBySpec_Cached : RepositoryOfT_GetBySpec_TestKit - { +public class RepositoryOfT_GetBySpec_Cached : RepositoryOfT_GetBySpec_TestKit +{ public RepositoryOfT_GetBySpec_Cached(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Cached) { } - } +} - public abstract class RepositoryOfT_GetBySpec_TestKit : IntegrationTestBase - { +public abstract class RepositoryOfT_GetBySpec_TestKit : IntegrationTestBase +{ protected RepositoryOfT_GetBySpec_TestKit(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) : base(fixture, specificationEvaluator) { } [Fact] public virtual async Task ReturnsStoreWithProducts_GivenStoreByIdIncludeProductsSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); } [Fact] public virtual async Task ReturnsStoreWithAddress_GivenStoreByIdIncludeAddressSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); } [Fact] public virtual async Task ReturnsStoreWithAddressAndProduct_GivenStoreByIdIncludeAddressAndProductsSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressAndProductsSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeAddressAndProductsSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); - result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); + result.Address?.Street.Should().Be(AddressSeed.VALID_STREET_FOR_STOREID1); } [Fact] public virtual async Task ReturnsStoreWithProducts_GivenStoreByIdIncludeProductsUsingStringSpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsUsingStringSpec(StoreSeed.VALID_STORE_ID)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeProductsUsingStringSpec(StoreSeed.VALID_STORE_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Products.Count.Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Products.Count.Should().BeGreaterThan(1); } [Fact] public virtual async Task ReturnsCompanyWithStoresAndAddress_GivenCompanyByIdIncludeStoresThenIncludeAddressSpec() { - var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeAddressSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeAddressSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - result.Stores.Count.Should().BeGreaterThan(49); - result.Stores.Select(x => x.Address).Count().Should().BeGreaterThan(0); + result.Should().NotBeNull(); + result!.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + result.Stores.Count.Should().BeGreaterThan(49); + result.Stores.Select(x => x.Address).Count().Should().BeGreaterThan(0); } [Fact] public virtual async Task ReturnsCompanyWithStoresAndProducts_GivenCompanyByIdIncludeStoresThenIncludeProductsSpec() { - var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeProductsSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdIncludeStoresThenIncludeProductsSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result!.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - result.Stores.Count.Should().BeGreaterThan(49); - result.Stores.Select(x => x.Products).Count().Should().BeGreaterThan(1); + result.Should().NotBeNull(); + result!.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + result.Stores.Count.Should().BeGreaterThan(49); + result.Stores.Select(x => x.Products).Count().Should().BeGreaterThan(1); } [Fact] public virtual async Task ReturnsUntrackedCompany_GivenCompanyByIdAsUntrackedSpec() { - dbContext.ChangeTracker.Clear(); + dbContext.ChangeTracker.Clear(); - var result = await companyRepository.GetBySpecAsync(new CompanyByIdAsUntrackedSpec(CompanySeed.VALID_COMPANY_ID)); + var result = await companyRepository.GetBySpecAsync(new CompanyByIdAsUntrackedSpec(CompanySeed.VALID_COMPANY_ID)); - result.Should().NotBeNull(); - result?.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); - dbContext.Entry(result!).State.Should().Be(Microsoft.EntityFrameworkCore.EntityState.Detached); + result.Should().NotBeNull(); + result?.Name.Should().Be(CompanySeed.VALID_COMPANY_NAME); + dbContext.Entry(result!).State.Should().Be(Microsoft.EntityFrameworkCore.EntityState.Detached); } [Fact] public virtual async Task ReturnsStoreWithCompanyAndCountryAndStoresForCompany_GivenStoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec() { - var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec(StoreSeed.VALID_STORE_ID)); - - result.Should().NotBeNull(); - result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); - result.Company.Should().NotBeNull(); - result.Company!.Country.Should().NotBeNull(); - result.Company!.Stores.Should().HaveCountGreaterOrEqualTo(2); - result.Company?.Stores?.Should().Match(x => x.Any(z => z.Products.Count > 0)); + var result = await storeRepository.GetBySpecAsync(new StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec(StoreSeed.VALID_STORE_ID)); + + result.Should().NotBeNull(); + result!.Name.Should().Be(StoreSeed.VALID_STORE_NAME); + result.Company.Should().NotBeNull(); + result.Company!.Country.Should().NotBeNull(); + result.Company!.Stores.Should().HaveCountGreaterOrEqualTo(2); + result.Company?.Stores?.Should().Match(x => x.Any(z => z.Products.Count > 0)); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_ListAsync.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_ListAsync.cs index ee00cc82..77f12ce1 100644 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_ListAsync.cs +++ b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.IntegrationTests/RepositoryOfT_ListAsync.cs @@ -1,210 +1,207 @@ -using System.Linq; -using System.Threading.Tasks; -using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; +using Ardalis.Specification.EntityFrameworkCore.IntegrationTests.Fixture; using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; using Ardalis.Specification.UnitTests.Fixture.Specs; using FluentAssertions; using Xunit; -namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests +namespace Ardalis.Specification.EntityFrameworkCore.IntegrationTests; + +public class RepositoryOfT_ListAsync : RepositoryOfT_ListAsync_TestKit { - public class RepositoryOfT_ListAsync : RepositoryOfT_ListAsync_TestKit - { public RepositoryOfT_ListAsync(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Default) { } - } +} - public class RepositoryOfT_ListAsync_Cached : RepositoryOfT_ListAsync_TestKit - { +public class RepositoryOfT_ListAsync_Cached : RepositoryOfT_ListAsync_TestKit +{ public RepositoryOfT_ListAsync_Cached(SharedDatabaseFixture fixture) : base(fixture, SpecificationEvaluator.Cached) { } - } +} - public abstract class RepositoryOfT_ListAsync_TestKit : IntegrationTestBase - { +public abstract class RepositoryOfT_ListAsync_TestKit : IntegrationTestBase +{ protected RepositoryOfT_ListAsync_TestKit(SharedDatabaseFixture fixture, ISpecificationEvaluator specificationEvaluator) : base(fixture, specificationEvaluator) { } [Fact] public virtual async Task ReturnsStoreWithProducts_GivenStoreIncludeProductsSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeProductsSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeProductsSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Products.Should().NotBeEmpty(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Products.Should().NotBeEmpty(); } [Fact] public virtual async Task ReturnsStoreWithAddress_GivenStoreIncludeAddressSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeAddressSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeAddressSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Address.Should().NotBeNull(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Address.Should().NotBeNull(); } [Fact] public virtual async Task ReturnsStoreWithAddressAndProduct_GivenStoreIncludeAddressAndProductsSpec() { - var result = await storeRepository.ListAsync(new StoreIncludeAddressAndProductsSpec()); + var result = await storeRepository.ListAsync(new StoreIncludeAddressAndProductsSpec()); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Address.Should().NotBeNull(); - result[0].Products.Should().NotBeEmpty(); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Address.Should().NotBeNull(); + result[0].Products.Should().NotBeEmpty(); } [Fact] public virtual async Task ReturnsCompanyWithStoreWithIdOne_GivenCompanyIncludeFilteredStoresSpec() { - var result = await companyRepository.ListAsync(new CompanyIncludeFilteredStoresSpec(1)); + var result = await companyRepository.ListAsync(new CompanyIncludeFilteredStoresSpec(1)); - result.Should().NotBeNull(); - result.Should().NotBeEmpty(); - result[0].Stores.Should().NotBeEmpty(); - result[0].Stores.Should().HaveCount(1); - result[0].Stores.First().Id.Should().Be(1); + result.Should().NotBeNull(); + result.Should().NotBeEmpty(); + result[0].Stores.Should().NotBeEmpty(); + result[0].Stores.Should().HaveCount(1); + result[0].Stores.First().Id.Should().Be(1); } [Fact] public virtual async Task ReturnsStoreWithIdFrom15To30_GivenStoresByIdListSpec() { - var ids = Enumerable.Range(15, 16); - var spec = new StoresByIdListSpec(ids); + var ids = Enumerable.Range(15, 16); + var spec = new StoresByIdListSpec(ids); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(16); - stores.OrderBy(x => x.Id).First().Id.Should().Be(15); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); + stores.Count.Should().Be(16); + stores.OrderBy(x => x.Id).First().Id.Should().Be(15); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); } [Fact] public virtual async Task ReturnsSecondPageOfStoreNames_GivenStoreNamesPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoreNamesPaginatedSpec(skip, take); + var spec = new StoreNamesPaginatedSpec(skip, take); - var storeNames = await storeRepository.ListAsync(spec); + var storeNames = await storeRepository.ListAsync(spec); - storeNames.Count.Should().Be(take); - storeNames.First().Should().Be("Store 11"); - storeNames.Last().Should().Be("Store 20"); + storeNames.Count.Should().Be(take); + storeNames.First().Should().Be("Store 11"); + storeNames.Last().Should().Be("Store 20"); } [Fact] public virtual async Task ReturnsSecondPageOfStores_GivenStoresPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresPaginatedSpec(skip, take); + var spec = new StoresPaginatedSpec(skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(11); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); + stores.Count.Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(11); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); } [Fact] public virtual async Task ReturnsOrderStoresByNameDescForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameSpec() { - var spec = new StoresByCompanyOrderedDescByNameSpec(2); + var spec = new StoresByCompanyOrderedDescByNameSpec(2); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); } [Fact] public virtual async Task ReturnsOrderStoresByNameDescThenByIdForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameThenByIdSpec() { - var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); + var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(99); - stores.Last().Id.Should().Be(98); + stores.First().Id.Should().Be(99); + stores.Last().Id.Should().Be(98); } [Fact] public virtual async Task ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedOrderedDescByNameSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); + stores.Count.Should().Be(take); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); } [Fact] public virtual async Task ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedSpec(2, skip, take); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.Count.Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(61); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); + stores.Count.Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(61); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); } [Fact] public virtual async Task ReturnsOrderedStores_GivenStoresOrderedSpecByName() { - var spec = new StoresOrderedSpecByName(); + var spec = new StoresOrderedSpecByName(); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); } [Fact] public virtual async Task ReturnsOrderedStores_GivenStoresOrderedDescendingByNameSpec() { - var spec = new StoresOrderedDescendingByNameSpec(); + var spec = new StoresOrderedDescendingByNameSpec(); - var stores = await storeRepository.ListAsync(spec); + var stores = await storeRepository.ListAsync(spec); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); } [Fact] public virtual async Task ReturnsStoreContainingCity1_GivenStoreIncludeProductsSpec() { - var result = await storeRepository.ListAsync(new StoreSearchByNameOrCitySpec(StoreSeed.VALID_Search_City_Key)); + var result = await storeRepository.ListAsync(new StoreSearchByNameOrCitySpec(StoreSeed.VALID_Search_City_Key)); - result.Should().NotBeNull(); - result.Should().ContainSingle(); - result[0].Id.Should().Be(StoreSeed.VALID_Search_ID); - result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key); + result.Should().NotBeNull(); + result.Should().ContainSingle(); + result[0].Id.Should().Be(StoreSeed.VALID_Search_ID); + result[0].City.Should().Contain(StoreSeed.VALID_Search_City_Key); } [Fact] public virtual async Task ReturnsAllProducts_GivenStoreSelectManyProductsSpec() { - var result = await storeRepository.ListAsync(new StoreProductNamesSpec()); + var result = await storeRepository.ListAsync(new StoreProductNamesSpec()); - result.Should().NotBeNull(); - result.Should().HaveCount(ProductSeed.TOTAL_PRODUCT_COUNT); - result.OrderBy(x => x).First().Should().Be(ProductSeed.VALID_PRODUCT_NAME); + result.Should().NotBeNull(); + result.Should().HaveCount(ProductSeed.TOTAL_PRODUCT_COUNT); + result.OrderBy(x => x).First().Should().Be(ProductSeed.VALID_PRODUCT_NAME); } - } } diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/Ardalis.Specification.EntityFrameworkCore.UnitTests.csproj b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/Ardalis.Specification.EntityFrameworkCore.UnitTests.csproj deleted file mode 100644 index 059f6c6c..00000000 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/Ardalis.Specification.EntityFrameworkCore.UnitTests.csproj +++ /dev/null @@ -1,30 +0,0 @@ - - - - net6.0 - enable - enable - - false - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - diff --git a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/EFRepositoryFactoryTests.cs b/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/EFRepositoryFactoryTests.cs deleted file mode 100644 index 07d50d64..00000000 --- a/Specification.EntityFrameworkCore/tests/Ardalis.Specification.EntityFrameworkCore.UnitTests/EFRepositoryFactoryTests.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; -using Ardalis.SampleApp.Core.Interfaces; -using Ardalis.SampleApp.Infrastructure.Data; -using Ardalis.SampleApp.Infrastructure.DataAccess; -using Microsoft.EntityFrameworkCore; -using Moq; -using Xunit; - -namespace Ardalis.Specification.EntityFrameworkCore.UnitTests; - -public class EFRepositoryFactoryTests -{ - [Fact] - public void CorrectlyInstantiatesRepository() - { - var mockContextFactory = new Mock>(); - mockContextFactory.Setup(x => x.CreateDbContext()) - .Returns(() => new SampleDbContext(new DbContextOptions())); - - var repositoryFactory = - new EFRepositoryFactory, MyRepository, SampleDbContext>(mockContextFactory - .Object); - - var repository = repositoryFactory.CreateRepository(); - Assert.IsType>(repository); - } -} diff --git a/Specification/src/Ardalis.Specification/Ardalis.Specification.csproj b/Specification/src/Ardalis.Specification/Ardalis.Specification.csproj index 7810f08b..aa7f3106 100644 --- a/Specification/src/Ardalis.Specification/Ardalis.Specification.csproj +++ b/Specification/src/Ardalis.Specification/Ardalis.Specification.csproj @@ -2,40 +2,41 @@ net6.0;netstandard2.0 + 11.0 + enable + + Ardalis.Specification Ardalis.Specification Ardalis.Specification true Steve Smith (@ardalis); Fati Iseni (@fiseni); Scott DePouw; Ardalis.com - https://github.com/ardalis/specification A simple package with a base Specification class, for use in creating queries that work with Repository types. A simple package with a base Specification class, for use in creating queries that work with Repository types. + https://github.com/ardalis/specification https://github.com/ardalis/specification spec;specification;repository;ddd + icon.png 7.0.0 - * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 - * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 - * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 - * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 - * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 - * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 - * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 - * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 - * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 - * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 - * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 + * Patch 2 by @davidhenley in https://github.com/ardalis/Specification/pull/283 + * Fix `Just the Docs` link in docs home page by @snowfrogdev in https://github.com/ardalis/Specification/pull/293 + * Update url path by @ta1H3n in https://github.com/ardalis/Specification/pull/303 + * Implement SelectMany support by @amdavie in https://github.com/ardalis/Specification/pull/320 + * Add two methods for consuming repositories in scenarios where repositories could be longer lived (e.g. Blazor component Injections) by @jasonsummers in https://github.com/ardalis/Specification/pull/289 + * Added support for AsAsyncEnumerable by @nkz-soft in https://github.com/ardalis/Specification/pull/316 + * Lamadelrae/doc faq ef versions by @Lamadelrae in https://github.com/ardalis/Specification/pull/324 + * Updated projects, drop support for old TFMs. by @fiseni in https://github.com/ardalis/Specification/pull/326 + * Update the search feature to generate parameterized query. by @fiseni in https://github.com/ardalis/Specification/pull/327 + * Add support for extending default evaluator list by @fiseni in https://github.com/ardalis/Specification/pull/328 + * Ardalis/cleanup by @ardalis in https://github.com/ardalis/Specification/pull/332 - Ardalis.Specification - icon.png true true $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - 9.0 - enable bin\$(Configuration)\Ardalis.Specification.xml - + @@ -43,4 +44,8 @@ + + + 1701;1702;1591;1573;1712 + diff --git a/Specification/src/Ardalis.Specification/Builder/CacheSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/CacheSpecificationBuilder.cs index 5378f093..c5691ecd 100644 --- a/Specification/src/Ardalis.Specification/Builder/CacheSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/CacheSpecificationBuilder.cs @@ -1,7 +1,7 @@ -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class CacheSpecificationBuilder : ICacheSpecificationBuilder where T : class { - public class CacheSpecificationBuilder : ICacheSpecificationBuilder where T : class - { public Specification Specification { get; } public bool IsChainDiscarded { get; set; } @@ -12,8 +12,7 @@ public CacheSpecificationBuilder(Specification specification) public CacheSpecificationBuilder(Specification specification, bool isChainDiscarded) { - this.Specification = specification; - this.IsChainDiscarded = isChainDiscarded; + Specification = specification; + IsChainDiscarded = isChainDiscarded; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/ICacheSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/ICacheSpecificationBuilder.cs index 422528e5..460deac7 100644 --- a/Specification/src/Ardalis.Specification/Builder/ICacheSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/ICacheSpecificationBuilder.cs @@ -1,7 +1,6 @@ -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public interface ICacheSpecificationBuilder : ISpecificationBuilder where T : class { - public interface ICacheSpecificationBuilder : ISpecificationBuilder where T : class - { bool IsChainDiscarded { get; set; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/IIncludableSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/IIncludableSpecificationBuilder.cs index ce7d7bef..07857e19 100644 --- a/Specification/src/Ardalis.Specification/Builder/IIncludableSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/IIncludableSpecificationBuilder.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public interface IIncludableSpecificationBuilder : ISpecificationBuilder where T : class { - public interface IIncludableSpecificationBuilder : ISpecificationBuilder where T : class - { bool IsChainDiscarded { get; set; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/IOrderedSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/IOrderedSpecificationBuilder.cs index d2bbf175..0039dd79 100644 --- a/Specification/src/Ardalis.Specification/Builder/IOrderedSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/IOrderedSpecificationBuilder.cs @@ -1,12 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public interface IOrderedSpecificationBuilder : ISpecificationBuilder { - public interface IOrderedSpecificationBuilder : ISpecificationBuilder - { bool IsChainDiscarded { get; set; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/ISpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/ISpecificationBuilder.cs index 2a5e6b90..dfeba22c 100644 --- a/Specification/src/Ardalis.Specification/Builder/ISpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/ISpecificationBuilder.cs @@ -1,17 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public interface ISpecificationBuilder : ISpecificationBuilder { - public interface ISpecificationBuilder : ISpecificationBuilder - { new Specification Specification { get; } - } +} - public interface ISpecificationBuilder - { +public interface ISpecificationBuilder +{ Specification Specification { get; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/IncludableBuilderExtensions.cs b/Specification/src/Ardalis.Specification/Builder/IncludableBuilderExtensions.cs index 7fce9a0d..ec72dc8f 100644 --- a/Specification/src/Ardalis.Specification/Builder/IncludableBuilderExtensions.cs +++ b/Specification/src/Ardalis.Specification/Builder/IncludableBuilderExtensions.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public static class IncludableBuilderExtensions { - public static class IncludableBuilderExtensions - { public static IIncludableSpecificationBuilder ThenInclude( this IIncludableSpecificationBuilder previousBuilder, Expression> thenIncludeExpression) @@ -18,16 +18,16 @@ public static IIncludableSpecificationBuilder ThenInclude)previousBuilder.Specification.IncludeExpressions).Add(info); - } + ((List)previousBuilder.Specification.IncludeExpressions).Add(info); + } - var includeBuilder = new IncludableSpecificationBuilder(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded); + var includeBuilder = new IncludableSpecificationBuilder(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded); - return includeBuilder; + return includeBuilder; } public static IIncludableSpecificationBuilder ThenInclude( @@ -42,16 +42,15 @@ public static IIncludableSpecificationBuilder ThenInclude)); + if (condition && !previousBuilder.IsChainDiscarded) + { + var info = new IncludeExpressionInfo(thenIncludeExpression, typeof(TEntity), typeof(TProperty), typeof(IEnumerable)); - ((List)previousBuilder.Specification.IncludeExpressions).Add(info); - } + ((List)previousBuilder.Specification.IncludeExpressions).Add(info); + } - var includeBuilder = new IncludableSpecificationBuilder(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded); + var includeBuilder = new IncludableSpecificationBuilder(previousBuilder.Specification, !condition || previousBuilder.IsChainDiscarded); - return includeBuilder; + return includeBuilder; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/IncludableSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/IncludableSpecificationBuilder.cs index 18a5999a..f1686f50 100644 --- a/Specification/src/Ardalis.Specification/Builder/IncludableSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/IncludableSpecificationBuilder.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public class IncludableSpecificationBuilder : IIncludableSpecificationBuilder where T : class { - public class IncludableSpecificationBuilder : IIncludableSpecificationBuilder where T : class - { public Specification Specification { get; } public bool IsChainDiscarded { get; set; } @@ -16,8 +12,7 @@ public IncludableSpecificationBuilder(Specification specification) public IncludableSpecificationBuilder(Specification specification, bool isChainDiscarded) { - this.Specification = specification; - this.IsChainDiscarded = isChainDiscarded; + Specification = specification; + IsChainDiscarded = isChainDiscarded; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/OrderedBuilderExtensions.cs b/Specification/src/Ardalis.Specification/Builder/OrderedBuilderExtensions.cs index 62232372..e6250361 100644 --- a/Specification/src/Ardalis.Specification/Builder/OrderedBuilderExtensions.cs +++ b/Specification/src/Ardalis.Specification/Builder/OrderedBuilderExtensions.cs @@ -1,12 +1,11 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public static class OrderedBuilderExtensions { - public static class OrderedBuilderExtensions - { public static IOrderedSpecificationBuilder ThenBy( this IOrderedSpecificationBuilder orderedBuilder, Expression> orderExpression) @@ -17,16 +16,16 @@ public static IOrderedSpecificationBuilder ThenBy( Expression> orderExpression, bool condition) { - if (condition && !orderedBuilder.IsChainDiscarded) - { - ((List>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.ThenBy)); - } - else - { - orderedBuilder.IsChainDiscarded = true; - } + if (condition && !orderedBuilder.IsChainDiscarded) + { + ((List>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.ThenBy)); + } + else + { + orderedBuilder.IsChainDiscarded = true; + } - return orderedBuilder; + return orderedBuilder; } public static IOrderedSpecificationBuilder ThenByDescending( @@ -39,16 +38,15 @@ public static IOrderedSpecificationBuilder ThenByDescending( Expression> orderExpression, bool condition) { - if (condition && !orderedBuilder.IsChainDiscarded) - { - ((List>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.ThenByDescending)); - } - else - { - orderedBuilder.IsChainDiscarded = true; - } + if (condition && !orderedBuilder.IsChainDiscarded) + { + ((List>)orderedBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.ThenByDescending)); + } + else + { + orderedBuilder.IsChainDiscarded = true; + } - return orderedBuilder; + return orderedBuilder; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/OrderedSpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/OrderedSpecificationBuilder.cs index 22e935ec..7a319030 100644 --- a/Specification/src/Ardalis.Specification/Builder/OrderedSpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/OrderedSpecificationBuilder.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public class OrderedSpecificationBuilder : IOrderedSpecificationBuilder { - public class OrderedSpecificationBuilder : IOrderedSpecificationBuilder - { public Specification Specification { get; } public bool IsChainDiscarded { get; set; } @@ -17,8 +12,7 @@ public OrderedSpecificationBuilder(Specification specification) public OrderedSpecificationBuilder(Specification specification, bool isChainDiscarded) { - this.Specification = specification; - this.IsChainDiscarded = isChainDiscarded; + Specification = specification; + IsChainDiscarded = isChainDiscarded; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/SpecificationBuilder.cs b/Specification/src/Ardalis.Specification/Builder/SpecificationBuilder.cs index a149aa13..b23eed0b 100644 --- a/Specification/src/Ardalis.Specification/Builder/SpecificationBuilder.cs +++ b/Specification/src/Ardalis.Specification/Builder/SpecificationBuilder.cs @@ -1,28 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq.Expressions; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public class SpecificationBuilder : SpecificationBuilder, ISpecificationBuilder { - public class SpecificationBuilder : SpecificationBuilder, ISpecificationBuilder - { public new Specification Specification { get; } public SpecificationBuilder(Specification specification) : base(specification) { - this.Specification = specification; + Specification = specification; } - } +} - public class SpecificationBuilder : ISpecificationBuilder - { +public class SpecificationBuilder : ISpecificationBuilder +{ public Specification Specification { get; } public SpecificationBuilder(Specification specification) { - this.Specification = specification; + Specification = specification; } - } } diff --git a/Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs b/Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs index b3da0032..74204a62 100644 --- a/Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs +++ b/Specification/src/Ardalis.Specification/Builder/SpecificationBuilderExtensions.cs @@ -2,10 +2,10 @@ using System.Collections.Generic; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public static class SpecificationBuilderExtensions { - public static class SpecificationBuilderExtensions - { /// /// Specify a predicate that will be applied to the query /// @@ -29,12 +29,12 @@ public static ISpecificationBuilder Where( Expression> criteria, bool condition) { - if (condition) - { - ((List>)specificationBuilder.Specification.WhereExpressions).Add(new WhereExpressionInfo(criteria)); - } + if (condition) + { + ((List>)specificationBuilder.Specification.WhereExpressions).Add(new WhereExpressionInfo(criteria)); + } - return specificationBuilder; + return specificationBuilder; } /// @@ -60,14 +60,14 @@ public static IOrderedSpecificationBuilder OrderBy( Expression> orderExpression, bool condition) { - if (condition) - { - ((List>)specificationBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.OrderBy)); - } + if (condition) + { + ((List>)specificationBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.OrderBy)); + } - var orderedSpecificationBuilder = new OrderedSpecificationBuilder(specificationBuilder.Specification, !condition); + var orderedSpecificationBuilder = new OrderedSpecificationBuilder(specificationBuilder.Specification, !condition); - return orderedSpecificationBuilder; + return orderedSpecificationBuilder; } /// @@ -93,14 +93,14 @@ public static IOrderedSpecificationBuilder OrderByDescending( Expression> orderExpression, bool condition) { - if (condition) - { - ((List>)specificationBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.OrderByDescending)); - } + if (condition) + { + ((List>)specificationBuilder.Specification.OrderExpressions).Add(new OrderExpressionInfo(orderExpression, OrderTypeEnum.OrderByDescending)); + } - var orderedSpecificationBuilder = new OrderedSpecificationBuilder(specificationBuilder.Specification, !condition); + var orderedSpecificationBuilder = new OrderedSpecificationBuilder(specificationBuilder.Specification, !condition); - return orderedSpecificationBuilder; + return orderedSpecificationBuilder; } /// @@ -132,16 +132,16 @@ public static IIncludableSpecificationBuilder Include> includeExpression, bool condition) where T : class { - if (condition) - { - var info = new IncludeExpressionInfo(includeExpression, typeof(T), typeof(TProperty)); + if (condition) + { + var info = new IncludeExpressionInfo(includeExpression, typeof(T), typeof(TProperty)); - ((List)specificationBuilder.Specification.IncludeExpressions).Add(info); - } + ((List)specificationBuilder.Specification.IncludeExpressions).Add(info); + } - var includeBuilder = new IncludableSpecificationBuilder(specificationBuilder.Specification, !condition); + var includeBuilder = new IncludableSpecificationBuilder(specificationBuilder.Specification, !condition); - return includeBuilder; + return includeBuilder; } /// @@ -167,12 +167,12 @@ public static ISpecificationBuilder Include( string includeString, bool condition) where T : class { - if (condition) - { - ((List)specificationBuilder.Specification.IncludeStrings).Add(includeString); - } + if (condition) + { + ((List)specificationBuilder.Specification.IncludeStrings).Add(includeString); + } - return specificationBuilder; + return specificationBuilder; } /// @@ -206,12 +206,12 @@ public static ISpecificationBuilder Search( bool condition, int searchGroup = 1) where T : class { - if (condition) - { - ((List>)specificationBuilder.Specification.SearchCriterias).Add(new SearchExpressionInfo(selector, searchTerm, searchGroup)); - } + if (condition) + { + ((List>)specificationBuilder.Specification.SearchCriterias).Add(new SearchExpressionInfo(selector, searchTerm, searchGroup)); + } - return specificationBuilder; + return specificationBuilder; } /// @@ -235,14 +235,14 @@ public static ISpecificationBuilder Take( int take, bool condition) { - if (condition) - { - if (specificationBuilder.Specification.Take != null) throw new DuplicateTakeException(); + if (condition) + { + if (specificationBuilder.Specification.Take != null) throw new DuplicateTakeException(); - specificationBuilder.Specification.Take = take; - } + specificationBuilder.Specification.Take = take; + } - return specificationBuilder; + return specificationBuilder; } /// @@ -268,14 +268,14 @@ public static ISpecificationBuilder Skip( int skip, bool condition) { - if (condition) - { - if (specificationBuilder.Specification.Skip != null) throw new DuplicateSkipException(); + if (condition) + { + if (specificationBuilder.Specification.Skip != null) throw new DuplicateSkipException(); - specificationBuilder.Specification.Skip = skip; - } + specificationBuilder.Specification.Skip = skip; + } - return specificationBuilder; + return specificationBuilder; } /// @@ -286,9 +286,9 @@ public static ISpecificationBuilder Select( this ISpecificationBuilder specificationBuilder, Expression> selector) { - specificationBuilder.Specification.Selector = selector; + specificationBuilder.Specification.Selector = selector; - return specificationBuilder; + return specificationBuilder; } /// @@ -299,9 +299,9 @@ public static ISpecificationBuilder SelectMany( this ISpecificationBuilder specificationBuilder, Expression>> selector) { - specificationBuilder.Specification.SelectorMany = selector; + specificationBuilder.Specification.SelectorMany = selector; - return specificationBuilder; + return specificationBuilder; } /// @@ -312,9 +312,9 @@ public static ISpecificationBuilder PostProcessingAction( this ISpecificationBuilder specificationBuilder, Func, IEnumerable> predicate) { - specificationBuilder.Specification.PostProcessingAction = predicate; + specificationBuilder.Specification.PostProcessingAction = predicate; - return specificationBuilder; + return specificationBuilder; } /// @@ -325,9 +325,9 @@ public static ISpecificationBuilder PostProcessingAction this ISpecificationBuilder specificationBuilder, Func, IEnumerable> predicate) { - specificationBuilder.Specification.PostProcessingAction = predicate; + specificationBuilder.Specification.PostProcessingAction = predicate; - return specificationBuilder; + return specificationBuilder; } /// @@ -353,21 +353,21 @@ public static ICacheSpecificationBuilder EnableCache( bool condition, params object[] args) where T : class { - if (condition) - { - if (string.IsNullOrEmpty(specificationName)) + if (condition) { - throw new ArgumentException($"Required input {specificationName} was null or empty.", specificationName); - } + if (string.IsNullOrEmpty(specificationName)) + { + throw new ArgumentException($"Required input {specificationName} was null or empty.", specificationName); + } - specificationBuilder.Specification.CacheKey = $"{specificationName}-{string.Join("-", args)}"; + specificationBuilder.Specification.CacheKey = $"{specificationName}-{string.Join("-", args)}"; - specificationBuilder.Specification.CacheEnabled = true; - } + specificationBuilder.Specification.CacheEnabled = true; + } - var cacheBuilder = new CacheSpecificationBuilder(specificationBuilder.Specification, !condition); + var cacheBuilder = new CacheSpecificationBuilder(specificationBuilder.Specification, !condition); - return cacheBuilder; + return cacheBuilder; } /// @@ -389,14 +389,14 @@ public static ISpecificationBuilder AsTracking( this ISpecificationBuilder specificationBuilder, bool condition) where T : class { - if (condition) - { - specificationBuilder.Specification.AsNoTracking = false; - specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = false; - specificationBuilder.Specification.AsTracking = true; - } - - return specificationBuilder; + if (condition) + { + specificationBuilder.Specification.AsNoTracking = false; + specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = false; + specificationBuilder.Specification.AsTracking = true; + } + + return specificationBuilder; } /// @@ -418,14 +418,14 @@ public static ISpecificationBuilder AsNoTracking( this ISpecificationBuilder specificationBuilder, bool condition) where T : class { - if (condition) - { - specificationBuilder.Specification.AsTracking = false; - specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = false; - specificationBuilder.Specification.AsNoTracking = true; - } - - return specificationBuilder; + if (condition) + { + specificationBuilder.Specification.AsTracking = false; + specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = false; + specificationBuilder.Specification.AsNoTracking = true; + } + + return specificationBuilder; } /// @@ -455,12 +455,12 @@ public static ISpecificationBuilder AsSplitQuery( this ISpecificationBuilder specificationBuilder, bool condition) where T : class { - if (condition) - { - specificationBuilder.Specification.AsSplitQuery = true; - } + if (condition) + { + specificationBuilder.Specification.AsSplitQuery = true; + } - return specificationBuilder; + return specificationBuilder; } /// @@ -492,14 +492,14 @@ public static ISpecificationBuilder AsNoTrackingWithIdentityResolution( this ISpecificationBuilder specificationBuilder, bool condition) where T : class { - if (condition) - { - specificationBuilder.Specification.AsTracking = false; - specificationBuilder.Specification.AsNoTracking = false; - specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = true; - } - - return specificationBuilder; + if (condition) + { + specificationBuilder.Specification.AsTracking = false; + specificationBuilder.Specification.AsNoTracking = false; + specificationBuilder.Specification.AsNoTrackingWithIdentityResolution = true; + } + + return specificationBuilder; } /// @@ -527,12 +527,11 @@ public static ISpecificationBuilder IgnoreQueryFilters( this ISpecificationBuilder specificationBuilder, bool condition) where T : class { - if (condition) - { - specificationBuilder.Specification.IgnoreQueryFilters = true; - } + if (condition) + { + specificationBuilder.Specification.IgnoreQueryFilters = true; + } - return specificationBuilder; + return specificationBuilder; } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/IEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/IEvaluator.cs index 4f4435c0..e4bd3571 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/IEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/IEvaluator.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public interface IEvaluator { - public interface IEvaluator - { bool IsCriteriaEvaluator { get; } IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class; - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/IInMemoryEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/IInMemoryEvaluator.cs index 5dafa207..6e91cc2b 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/IInMemoryEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/IInMemoryEvaluator.cs @@ -1,12 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public interface IInMemoryEvaluator { - public interface IInMemoryEvaluator - { IEnumerable Evaluate(IEnumerable query, ISpecification specification); - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/IInMemorySpecificationEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/IInMemorySpecificationEvaluator.cs index b44f2112..648e244d 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/IInMemorySpecificationEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/IInMemorySpecificationEvaluator.cs @@ -1,13 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public interface IInMemorySpecificationEvaluator { - public interface IInMemorySpecificationEvaluator - { IEnumerable Evaluate(IEnumerable source, ISpecification specification); IEnumerable Evaluate(IEnumerable source, ISpecification specification); - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/ISpecificationEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/ISpecificationEvaluator.cs index 824d9f50..6c6bcbcd 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/ISpecificationEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/ISpecificationEvaluator.cs @@ -1,15 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Evaluates the logic encapsulated by an . +/// +public interface ISpecificationEvaluator { - /// - /// Evaluates the logic encapsulated by an . - /// - public interface ISpecificationEvaluator - { /// /// Applies the logic encapsulated by to given , /// and projects the result into . @@ -26,5 +23,4 @@ public interface ISpecificationEvaluator /// The encapsulated query logic. /// A filtered sequence of IQueryable GetQuery(IQueryable inputQuery, ISpecification specification, bool evaluateCriteriaOnly = false) where T : class; - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs index 26df3d5f..efce6274 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/InMemorySpecificationEvaluator.cs @@ -1,14 +1,10 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Linq.Expressions; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class InMemorySpecificationEvaluator : IInMemorySpecificationEvaluator { - public class InMemorySpecificationEvaluator : IInMemorySpecificationEvaluator - { // Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided evaluators. public static InMemorySpecificationEvaluator Default { get; } = new InMemorySpecificationEvaluator(); @@ -16,45 +12,44 @@ public class InMemorySpecificationEvaluator : IInMemorySpecificationEvaluator public InMemorySpecificationEvaluator() { - this.Evaluators.AddRange(new IInMemoryEvaluator[] - { - WhereEvaluator.Instance, - SearchEvaluator.Instance, - OrderEvaluator.Instance, - PaginationEvaluator.Instance - }); + Evaluators.AddRange(new IInMemoryEvaluator[] + { + WhereEvaluator.Instance, + SearchEvaluator.Instance, + OrderEvaluator.Instance, + PaginationEvaluator.Instance + }); } public InMemorySpecificationEvaluator(IEnumerable evaluators) { - this.Evaluators.AddRange(evaluators); + Evaluators.AddRange(evaluators); } public virtual IEnumerable Evaluate(IEnumerable source, ISpecification specification) { - if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); - if (specification.Selector != null && specification.SelectorMany != null) throw new ConcurrentSelectorsException(); + if (specification.Selector is null && specification.SelectorMany is null) throw new SelectorNotFoundException(); + if (specification.Selector != null && specification.SelectorMany != null) throw new ConcurrentSelectorsException(); - var baseQuery = Evaluate(source, (ISpecification)specification); + var baseQuery = Evaluate(source, (ISpecification)specification); - var resultQuery = specification.Selector != null - ? baseQuery.Select(specification.Selector.Compile()) - : baseQuery.SelectMany(specification.SelectorMany!.Compile()); + var resultQuery = specification.Selector != null + ? baseQuery.Select(specification.Selector.Compile()) + : baseQuery.SelectMany(specification.SelectorMany!.Compile()); - return specification.PostProcessingAction == null - ? resultQuery - : specification.PostProcessingAction(resultQuery); + return specification.PostProcessingAction == null + ? resultQuery + : specification.PostProcessingAction(resultQuery); } public virtual IEnumerable Evaluate(IEnumerable source, ISpecification specification) { - foreach (var evaluator in Evaluators) - { - source = evaluator.Evaluate(source, specification); - } - - return specification.PostProcessingAction == null - ? source - : specification.PostProcessingAction(source); + foreach (var evaluator in Evaluators) + { + source = evaluator.Evaluate(source, specification); + } + + return specification.PostProcessingAction == null + ? source + : specification.PostProcessingAction(source); } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/OrderEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/OrderEvaluator.cs index da8e4862..ed0b3824 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/OrderEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/OrderEvaluator.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class OrderEvaluator : IEvaluator, IInMemoryEvaluator { - public class OrderEvaluator : IEvaluator, IInMemoryEvaluator - { private OrderEvaluator() { } public static OrderEvaluator Instance { get; } = new OrderEvaluator(); @@ -12,82 +12,81 @@ private OrderEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - if (specification.OrderExpressions != null) - { - if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy - || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + if (specification.OrderExpressions != null) { - throw new DuplicateOrderChainException(); - } + if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy + || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + { + throw new DuplicateOrderChainException(); + } - IOrderedQueryable? orderedQuery = null; - foreach (var orderExpression in specification.OrderExpressions) - { - if (orderExpression.OrderType == OrderTypeEnum.OrderBy) - { - orderedQuery = query.OrderBy(orderExpression.KeySelector); - } - else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) - { - orderedQuery = query.OrderByDescending(orderExpression.KeySelector); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) - { - orderedQuery = orderedQuery.ThenBy(orderExpression.KeySelector); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) - { - orderedQuery = orderedQuery.ThenByDescending(orderExpression.KeySelector); - } - } + IOrderedQueryable? orderedQuery = null; + foreach (var orderExpression in specification.OrderExpressions) + { + if (orderExpression.OrderType == OrderTypeEnum.OrderBy) + { + orderedQuery = query.OrderBy(orderExpression.KeySelector); + } + else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) + { + orderedQuery = query.OrderByDescending(orderExpression.KeySelector); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) + { + orderedQuery = orderedQuery!.ThenBy(orderExpression.KeySelector); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) + { + orderedQuery = orderedQuery!.ThenByDescending(orderExpression.KeySelector); + } + } - if (orderedQuery != null) - { - query = orderedQuery; + if (orderedQuery != null) + { + query = orderedQuery; + } } - } - return query; + return query; } public IEnumerable Evaluate(IEnumerable query, ISpecification specification) { - if (specification.OrderExpressions != null) - { - if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy - || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + if (specification.OrderExpressions != null) { - throw new DuplicateOrderChainException(); - } + if (specification.OrderExpressions.Count(x => x.OrderType == OrderTypeEnum.OrderBy + || x.OrderType == OrderTypeEnum.OrderByDescending) > 1) + { + throw new DuplicateOrderChainException(); + } - IOrderedEnumerable? orderedQuery = null; - foreach (var orderExpression in specification.OrderExpressions) - { - if (orderExpression.OrderType == OrderTypeEnum.OrderBy) - { - orderedQuery = query.OrderBy(orderExpression.KeySelectorFunc); - } - else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) - { - orderedQuery = query.OrderByDescending(orderExpression.KeySelectorFunc); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) - { - orderedQuery = orderedQuery.ThenBy(orderExpression.KeySelectorFunc); - } - else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) - { - orderedQuery = orderedQuery.ThenByDescending(orderExpression.KeySelectorFunc); - } - } + IOrderedEnumerable? orderedQuery = null; + foreach (var orderExpression in specification.OrderExpressions) + { + if (orderExpression.OrderType == OrderTypeEnum.OrderBy) + { + orderedQuery = query.OrderBy(orderExpression.KeySelectorFunc); + } + else if (orderExpression.OrderType == OrderTypeEnum.OrderByDescending) + { + orderedQuery = query.OrderByDescending(orderExpression.KeySelectorFunc); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenBy) + { + orderedQuery = orderedQuery!.ThenBy(orderExpression.KeySelectorFunc); + } + else if (orderExpression.OrderType == OrderTypeEnum.ThenByDescending) + { + orderedQuery = orderedQuery!.ThenByDescending(orderExpression.KeySelectorFunc); + } + } - if (orderedQuery != null) - { - query = orderedQuery; + if (orderedQuery != null) + { + query = orderedQuery; + } } - } - return query; + return query; } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/PaginationEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/PaginationEvaluator.cs index 41173a74..f669024f 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/PaginationEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/PaginationEvaluator.cs @@ -1,12 +1,10 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class PaginationEvaluator : IEvaluator, IInMemoryEvaluator { - public class PaginationEvaluator : IEvaluator, IInMemoryEvaluator - { private PaginationEvaluator() { } public static PaginationEvaluator Instance { get; } = new PaginationEvaluator(); @@ -14,33 +12,32 @@ private PaginationEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - // If skip is 0, avoid adding to the IQueryable. It will generate more optimized SQL that way. - if (specification.Skip != null && specification.Skip != 0) - { - query = query.Skip(specification.Skip.Value); - } - - if (specification.Take != null) - { - query = query.Take(specification.Take.Value); - } - - return query; + // If skip is 0, avoid adding to the IQueryable. It will generate more optimized SQL that way. + if (specification.Skip != null && specification.Skip != 0) + { + query = query.Skip(specification.Skip.Value); + } + + if (specification.Take != null) + { + query = query.Take(specification.Take.Value); + } + + return query; } public IEnumerable Evaluate(IEnumerable query, ISpecification specification) { - if (specification.Skip != null && specification.Skip != 0) - { - query = query.Skip(specification.Skip.Value); - } + if (specification.Skip != null && specification.Skip != 0) + { + query = query.Skip(specification.Skip.Value); + } - if (specification.Take != null) - { - query = query.Take(specification.Take.Value); - } + if (specification.Take != null) + { + query = query.Take(specification.Take.Value); + } - return query; + return query; } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/SearchEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/SearchEvaluator.cs index c84ac0b0..0e029e8b 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/SearchEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/SearchEvaluator.cs @@ -1,21 +1,20 @@ using System.Collections.Generic; using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class SearchEvaluator : IInMemoryEvaluator { - public class SearchEvaluator : IInMemoryEvaluator - { private SearchEvaluator() { } public static SearchEvaluator Instance { get; } = new SearchEvaluator(); public IEnumerable Evaluate(IEnumerable query, ISpecification specification) { - foreach (var searchGroup in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) - { - query = query.Where(x => searchGroup.Any(c => c.SelectorFunc(x).Like(c.SearchTerm))); - } + foreach (var searchGroup in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) + { + query = query.Where(x => searchGroup.Any(c => c.SelectorFunc(x).Like(c.SearchTerm))); + } - return query; + return query; } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/SearchExtension.cs b/Specification/src/Ardalis.Specification/Evaluators/SearchExtension.cs index 5981dfed..e6899e65 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/SearchExtension.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/SearchExtension.cs @@ -1,21 +1,20 @@ using System; using System.Collections.Generic; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public static class SearchExtension { - public static class SearchExtension - { public static bool Like(this string input, string pattern) { - try - { - return SqlLike(input, pattern); - } - catch (Exception) - { - throw new InvalidSearchPatternException(pattern); - } + try + { + return SqlLike(input, pattern); + } + catch (Exception) + { + throw new InvalidSearchPatternException(pattern); + } } // This C# implementation of SQL Like operator is based on the following SO post https://stackoverflow.com/a/8583383/10577116 @@ -23,136 +22,134 @@ public static bool Like(this string input, string pattern) // It may fail/throw in some very specific and edge cases, hence, wrap it in try/catch. private static bool SqlLike(string str, string pattern) { - bool isMatch = true, - isWildCardOn = false, - isCharWildCardOn = false, - isCharSetOn = false, - isNotCharSetOn = false, - endOfPattern = false; - int lastWildCard = -1; - int patternIndex = 0; - List set = new List(); - char p = '\0'; + var isMatch = true; + var isWildCardOn = false; + var isCharWildCardOn = false; + var isCharSetOn = false; + var isNotCharSetOn = false; + var lastWildCard = -1; + var patternIndex = 0; + var set = new List(); + var p = '\0'; + bool endOfPattern; - for (int i = 0; i < str.Length; i++) - { - char c = str[i]; - endOfPattern = (patternIndex >= pattern.Length); - if (!endOfPattern) + for (var i = 0; i < str.Length; i++) { - p = pattern[patternIndex]; - - if (!isWildCardOn && p == '%') - { - lastWildCard = patternIndex; - isWildCardOn = true; - while (patternIndex < pattern.Length && - pattern[patternIndex] == '%') + var c = str[i]; + endOfPattern = (patternIndex >= pattern.Length); + if (!endOfPattern) { - patternIndex++; - } - if (patternIndex >= pattern.Length) p = '\0'; - else p = pattern[patternIndex]; - } - else if (p == '_') - { - isCharWildCardOn = true; - patternIndex++; - } - else if (p == '[') - { - if (pattern[++patternIndex] == '^') - { - isNotCharSetOn = true; - patternIndex++; + p = pattern[patternIndex]; + + if (!isWildCardOn && p == '%') + { + lastWildCard = patternIndex; + isWildCardOn = true; + while (patternIndex < pattern.Length && + pattern[patternIndex] == '%') + { + patternIndex++; + } + p = patternIndex >= pattern.Length ? '\0' : pattern[patternIndex]; + } + else if (p == '_') + { + isCharWildCardOn = true; + patternIndex++; + } + else if (p == '[') + { + if (pattern[++patternIndex] == '^') + { + isNotCharSetOn = true; + patternIndex++; + } + else isCharSetOn = true; + + set.Clear(); + if (pattern[patternIndex + 1] == '-' && pattern[patternIndex + 3] == ']') + { + var start = char.ToUpper(pattern[patternIndex]); + patternIndex += 2; + var end = char.ToUpper(pattern[patternIndex]); + if (start <= end) + { + for (var ci = start; ci <= end; ci++) + { + set.Add(ci); + } + } + patternIndex++; + } + + while (patternIndex < pattern.Length && + pattern[patternIndex] != ']') + { + set.Add(pattern[patternIndex]); + patternIndex++; + } + patternIndex++; + } } - else isCharSetOn = true; - set.Clear(); - if (pattern[patternIndex + 1] == '-' && pattern[patternIndex + 3] == ']') + if (isWildCardOn) { - char start = char.ToUpper(pattern[patternIndex]); - patternIndex += 2; - char end = char.ToUpper(pattern[patternIndex]); - if (start <= end) - { - for (char ci = start; ci <= end; ci++) + if (char.ToUpper(c) == char.ToUpper(p)) { - set.Add(ci); + isWildCardOn = false; + patternIndex++; } - } - patternIndex++; } - - while (patternIndex < pattern.Length && - pattern[patternIndex] != ']') + else if (isCharWildCardOn) { - set.Add(pattern[patternIndex]); - patternIndex++; + isCharWildCardOn = false; } - patternIndex++; - } - } - - if (isWildCardOn) - { - if (char.ToUpper(c) == char.ToUpper(p)) - { - isWildCardOn = false; - patternIndex++; - } - } - else if (isCharWildCardOn) - { - isCharWildCardOn = false; - } - else if (isCharSetOn || isNotCharSetOn) - { - bool charMatch = (set.Contains(char.ToUpper(c))); - if ((isNotCharSetOn && charMatch) || (isCharSetOn && !charMatch)) - { - if (lastWildCard >= 0) patternIndex = lastWildCard; - else + else if (isCharSetOn || isNotCharSetOn) { - isMatch = false; - break; + var charMatch = (set.Contains(char.ToUpper(c))); + if ((isNotCharSetOn && charMatch) || (isCharSetOn && !charMatch)) + { + if (lastWildCard >= 0) patternIndex = lastWildCard; + else + { + isMatch = false; + break; + } + } + isNotCharSetOn = isCharSetOn = false; } - } - isNotCharSetOn = isCharSetOn = false; - } - else - { - if (char.ToUpper(c) == char.ToUpper(p)) - { - patternIndex++; - } - else - { - if (lastWildCard >= 0) patternIndex = lastWildCard; else { - isMatch = false; - break; + if (char.ToUpper(c) == char.ToUpper(p)) + { + patternIndex++; + } + else + { + if (lastWildCard >= 0) patternIndex = lastWildCard; + else + { + isMatch = false; + break; + } + } } - } } - } - endOfPattern = (patternIndex >= pattern.Length); + endOfPattern = (patternIndex >= pattern.Length); - if (isMatch && !endOfPattern) - { - bool isOnlyWildCards = true; - for (int i = patternIndex; i < pattern.Length; i++) + if (isMatch && !endOfPattern) { - if (pattern[i] != '%') - { - isOnlyWildCards = false; - break; - } + var isOnlyWildCards = true; + for (var i = patternIndex; i < pattern.Length; i++) + { + if (pattern[i] != '%') + { + isOnlyWildCards = false; + break; + } + } + if (isOnlyWildCards) endOfPattern = true; } - if (isOnlyWildCards) endOfPattern = true; - } - return isMatch && endOfPattern; + return isMatch && endOfPattern; } - } } diff --git a/Specification/src/Ardalis.Specification/Evaluators/WhereEvaluator.cs b/Specification/src/Ardalis.Specification/Evaluators/WhereEvaluator.cs index 135fa261..f64b17a8 100644 --- a/Specification/src/Ardalis.Specification/Evaluators/WhereEvaluator.cs +++ b/Specification/src/Ardalis.Specification/Evaluators/WhereEvaluator.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class WhereEvaluator : IEvaluator, IInMemoryEvaluator { - public class WhereEvaluator : IEvaluator, IInMemoryEvaluator - { private WhereEvaluator() { } public static WhereEvaluator Instance { get; } = new WhereEvaluator(); @@ -12,22 +12,21 @@ private WhereEvaluator() { } public IQueryable GetQuery(IQueryable query, ISpecification specification) where T : class { - foreach (var info in specification.WhereExpressions) - { - query = query.Where(info.Filter); - } + foreach (var info in specification.WhereExpressions) + { + query = query.Where(info.Filter); + } - return query; + return query; } public IEnumerable Evaluate(IEnumerable query, ISpecification specification) { - foreach (var info in specification.WhereExpressions) - { - query = query.Where(info.FilterFunc); - } + foreach (var info in specification.WhereExpressions) + { + query = query.Where(info.FilterFunc); + } - return query; + return query; } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/ConcurrentSelectorsException.cs b/Specification/src/Ardalis.Specification/Exceptions/ConcurrentSelectorsException.cs index a145c7aa..4ea7c4e2 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/ConcurrentSelectorsException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/ConcurrentSelectorsException.cs @@ -1,19 +1,18 @@ using System; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class ConcurrentSelectorsException : Exception { - public class ConcurrentSelectorsException : Exception - { - private const string message = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!"; + private const string _message = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!"; public ConcurrentSelectorsException() - : base(message) + : base(_message) { } public ConcurrentSelectorsException(Exception innerException) - : base(message, innerException) + : base(_message, innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/DuplicateOrderChainException.cs b/Specification/src/Ardalis.Specification/Exceptions/DuplicateOrderChainException.cs index 1ecaf6f1..98a98c70 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/DuplicateOrderChainException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/DuplicateOrderChainException.cs @@ -1,22 +1,18 @@ using System; -using System.Collections.Generic; -using System.Data; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class DuplicateOrderChainException : Exception { - public class DuplicateOrderChainException : Exception - { - private const string message = "The specification contains more than one Order chain!"; + private const string _message = "The specification contains more than one Order chain!"; public DuplicateOrderChainException() - : base(message) + : base(_message) { } public DuplicateOrderChainException(Exception innerException) - : base(message, innerException) + : base(_message, innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/DuplicateSkipException.cs b/Specification/src/Ardalis.Specification/Exceptions/DuplicateSkipException.cs index e83edcd5..114da8c0 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/DuplicateSkipException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/DuplicateSkipException.cs @@ -1,21 +1,18 @@ using System; -using System.Collections.Generic; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class DuplicateSkipException : Exception { - public class DuplicateSkipException : Exception - { - private const string message = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"; + private const string _message = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"; public DuplicateSkipException() - : base(message) + : base(_message) { } public DuplicateSkipException(Exception innerException) - : base(message, innerException) + : base(_message, innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/DuplicateTakeException.cs b/Specification/src/Ardalis.Specification/Exceptions/DuplicateTakeException.cs index 7a1cc2c5..31c751d2 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/DuplicateTakeException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/DuplicateTakeException.cs @@ -1,21 +1,18 @@ using System; -using System.Collections.Generic; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class DuplicateTakeException : Exception { - public class DuplicateTakeException : Exception - { - private const string message = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"; + private const string _message = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"; public DuplicateTakeException() - : base(message) + : base(_message) { } public DuplicateTakeException(Exception innerException) - : base(message, innerException) + : base(_message, innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/InvalidSearchPatternException.cs b/Specification/src/Ardalis.Specification/Exceptions/InvalidSearchPatternException.cs index 21e685d5..1bde3b0c 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/InvalidSearchPatternException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/InvalidSearchPatternException.cs @@ -1,21 +1,18 @@ using System; -using System.Collections.Generic; -using System.Text; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class InvalidSearchPatternException : Exception { - public class InvalidSearchPatternException : Exception - { - private const string message = "Invalid search pattern: "; + private const string _message = "Invalid search pattern: "; public InvalidSearchPatternException(string searchPattern) - : base($"{message}{searchPattern}") + : base($"{_message}{searchPattern}") { } public InvalidSearchPatternException(string searchPattern, Exception innerException) - : base($"{message}{searchPattern}", innerException) + : base($"{_message}{searchPattern}", innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Exceptions/SelectorNotFoundException.cs b/Specification/src/Ardalis.Specification/Exceptions/SelectorNotFoundException.cs index e8baf32e..df01a31a 100644 --- a/Specification/src/Ardalis.Specification/Exceptions/SelectorNotFoundException.cs +++ b/Specification/src/Ardalis.Specification/Exceptions/SelectorNotFoundException.cs @@ -1,19 +1,18 @@ using System; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class SelectorNotFoundException : Exception { - public class SelectorNotFoundException : Exception - { - private const string message = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!"; + private const string _message = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!"; public SelectorNotFoundException() - : base(message) + : base(_message) { } public SelectorNotFoundException(Exception innerException) - : base(message, innerException) + : base(_message, innerException) { } - } } diff --git a/Specification/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs b/Specification/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs index 2967c88c..e119afcf 100644 --- a/Specification/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs +++ b/Specification/src/Ardalis.Specification/Expressions/IncludeExpressionInfo.cs @@ -1,13 +1,13 @@ using System; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Encapsulates data needed to build Include/ThenInclude query. +/// +public class IncludeExpressionInfo { - /// - /// Encapsulates data needed to build Include/ThenInclude query. - /// - public class IncludeExpressionInfo - { /// /// If is , represents a related entity that should be included. /// If is , represents a related entity that should be included as part of the previously included entity. @@ -41,20 +41,20 @@ private IncludeExpressionInfo(LambdaExpression expression, IncludeTypeEnum includeType) { - _ = expression ?? throw new ArgumentNullException(nameof(expression)); - _ = entityType ?? throw new ArgumentNullException(nameof(entityType)); - _ = propertyType ?? throw new ArgumentNullException(nameof(propertyType)); + _ = expression ?? throw new ArgumentNullException(nameof(expression)); + _ = entityType ?? throw new ArgumentNullException(nameof(entityType)); + _ = propertyType ?? throw new ArgumentNullException(nameof(propertyType)); - if (includeType == IncludeTypeEnum.ThenInclude) - { - _ = previousPropertyType ?? throw new ArgumentNullException(nameof(previousPropertyType)); - } + if (includeType == IncludeTypeEnum.ThenInclude) + { + _ = previousPropertyType ?? throw new ArgumentNullException(nameof(previousPropertyType)); + } - this.LambdaExpression = expression; - this.EntityType = entityType; - this.PropertyType = propertyType; - this.PreviousPropertyType = previousPropertyType; - this.Type = includeType; + LambdaExpression = expression; + EntityType = entityType; + PropertyType = propertyType; + PreviousPropertyType = previousPropertyType; + Type = includeType; } /// @@ -86,5 +86,4 @@ public IncludeExpressionInfo(LambdaExpression expression, : this(expression, entityType, propertyType, previousPropertyType, IncludeTypeEnum.ThenInclude) { } - } } diff --git a/Specification/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs b/Specification/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs index 0140e3d9..5055182f 100644 --- a/Specification/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs +++ b/Specification/src/Ardalis.Specification/Expressions/OrderExpressionInfo.cs @@ -1,15 +1,15 @@ using System; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Encapsulates data needed to perform sorting. +/// +/// Type of the entity to apply sort on. +public class OrderExpressionInfo { - /// - /// Encapsulates data needed to perform sorting. - /// - /// Type of the entity to apply sort on. - public class OrderExpressionInfo - { - private readonly Lazy> keySelectorFunc; + private readonly Lazy> _keySelectorFunc; /// /// Creates instance of . @@ -19,12 +19,12 @@ public class OrderExpressionInfo /// If is null. public OrderExpressionInfo(Expression> keySelector, OrderTypeEnum orderType) { - _ = keySelector ?? throw new ArgumentNullException(nameof(keySelector)); + _ = keySelector ?? throw new ArgumentNullException(nameof(keySelector)); - this.KeySelector = keySelector; - this.OrderType = orderType; + KeySelector = keySelector; + OrderType = orderType; - this.keySelectorFunc = new Lazy>(this.KeySelector.Compile); + _keySelectorFunc = new Lazy>(KeySelector.Compile); } /// @@ -40,6 +40,5 @@ public OrderExpressionInfo(Expression> keySelector, OrderTypeEn /// /// Compiled . /// - public Func KeySelectorFunc => this.keySelectorFunc.Value; - } + public Func KeySelectorFunc => _keySelectorFunc.Value; } diff --git a/Specification/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs b/Specification/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs index d0774802..d7231503 100644 --- a/Specification/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs +++ b/Specification/src/Ardalis.Specification/Expressions/SearchExpressionInfo.cs @@ -1,15 +1,15 @@ using System; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Encapsulates data needed to perform 'SQL LIKE' operation. +/// +/// Type of the source from which search target should be selected. +public class SearchExpressionInfo { - /// - /// Encapsulates data needed to perform 'SQL LIKE' operation. - /// - /// Type of the source from which search target should be selected. - public class SearchExpressionInfo - { - private readonly Lazy> selectorFunc; + private readonly Lazy> _selectorFunc; /// /// Creates instance of . @@ -18,17 +18,17 @@ public class SearchExpressionInfo /// The value to use for the SQL LIKE. /// The index used to group sets of Selectors and SearchTerms together. /// If is null. - /// If is null or empty. + /// If is null or empty. public SearchExpressionInfo(Expression> selector, string searchTerm, int searchGroup = 1) { - _ = selector ?? throw new ArgumentNullException(nameof(selector)); - if (string.IsNullOrEmpty(searchTerm)) throw new ArgumentException(nameof(searchTerm)); + _ = selector ?? throw new ArgumentNullException(nameof(selector)); + if (string.IsNullOrEmpty(searchTerm)) throw new ArgumentException("The search term can not be null or empty."); - this.Selector = selector; - this.SearchTerm = searchTerm; - this.SearchGroup = searchGroup; + Selector = selector; + SearchTerm = searchTerm; + SearchGroup = searchGroup; - this.selectorFunc = new Lazy>(this.Selector.Compile); + _selectorFunc = new Lazy>(Selector.Compile); } /// @@ -49,6 +49,5 @@ public SearchExpressionInfo(Expression> selector, string searchT /// /// Compiled . /// - public Func SelectorFunc => this.selectorFunc.Value; - } + public Func SelectorFunc => _selectorFunc.Value; } diff --git a/Specification/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs b/Specification/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs index 68202424..1291315d 100644 --- a/Specification/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs +++ b/Specification/src/Ardalis.Specification/Expressions/WhereExpressionInfo.cs @@ -1,15 +1,15 @@ using System; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Encapsulates data needed to perform filtering. +/// +/// Type of the entity to apply filter on. +public class WhereExpressionInfo { - /// - /// Encapsulates data needed to perform filtering. - /// - /// Type of the entity to apply filter on. - public class WhereExpressionInfo - { - private readonly Lazy> filterFunc; + private readonly Lazy> _filterFunc; /// /// Creates instance of . @@ -18,11 +18,11 @@ public class WhereExpressionInfo /// If is null. public WhereExpressionInfo(Expression> filter) { - _ = filter ?? throw new ArgumentNullException(nameof(filter)); + _ = filter ?? throw new ArgumentNullException(nameof(filter)); - this.Filter = filter; + Filter = filter; - this.filterFunc = new Lazy>(this.Filter.Compile); + _filterFunc = new Lazy>(Filter.Compile); } /// @@ -33,6 +33,5 @@ public WhereExpressionInfo(Expression> filter) /// /// Compiled . /// - public Func FilterFunc => this.filterFunc.Value; - } + public Func FilterFunc => _filterFunc.Value; } diff --git a/Specification/src/Ardalis.Specification/IEntity.cs b/Specification/src/Ardalis.Specification/IEntity.cs index da137948..464d6291 100644 --- a/Specification/src/Ardalis.Specification/IEntity.cs +++ b/Specification/src/Ardalis.Specification/IEntity.cs @@ -1,7 +1,6 @@ -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public interface IEntity { - public interface IEntity - { TId Id { get; set; } - } } diff --git a/Specification/src/Ardalis.Specification/IReadRepositoryBase.cs b/Specification/src/Ardalis.Specification/IReadRepositoryBase.cs index 5867f84d..89ea8e8a 100644 --- a/Specification/src/Ardalis.Specification/IReadRepositoryBase.cs +++ b/Specification/src/Ardalis.Specification/IReadRepositoryBase.cs @@ -3,17 +3,17 @@ using System.Threading; using System.Threading.Tasks; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// +/// A can be used to query instances of . +/// An (or derived) is used to encapsulate the LINQ queries against the database. +/// +/// +/// The type of entity being operated on by this repository. +public interface IReadRepositoryBase where T : class { - /// - /// - /// A can be used to query instances of . - /// An (or derived) is used to encapsulate the LINQ queries against the database. - /// - /// - /// The type of entity being operated on by this repository. - public interface IReadRepositoryBase where T : class - { /// /// Finds an entity with the given primary key value. /// @@ -176,9 +176,8 @@ public interface IReadRepositoryBase where T : class /// /// The encapsulated query logic. /// - /// Returns an IAsyncEnumerable which can be enumerated asynchronously. + /// Returns an IAsyncEnumerable which can be enumerated asynchronously. /// IAsyncEnumerable AsAsyncEnumerable(ISpecification specification); #endif - } } diff --git a/Specification/src/Ardalis.Specification/IRepositoryBase.cs b/Specification/src/Ardalis.Specification/IRepositoryBase.cs index 4ab72502..896d854b 100644 --- a/Specification/src/Ardalis.Specification/IRepositoryBase.cs +++ b/Specification/src/Ardalis.Specification/IRepositoryBase.cs @@ -2,18 +2,18 @@ using System.Threading; using System.Threading.Tasks; -namespace Ardalis.Specification -{ +namespace Ardalis.Specification; + - /// - /// - /// A can be used to query and save instances of . - /// An (or derived) is used to encapsulate the LINQ queries against the database. - /// - /// - /// The type of entity being operated on by this repository. - public interface IRepositoryBase : IReadRepositoryBase where T : class - { +/// +/// +/// A can be used to query and save instances of . +/// An (or derived) is used to encapsulate the LINQ queries against the database. +/// +/// +/// The type of entity being operated on by this repository. +public interface IRepositoryBase : IReadRepositoryBase where T : class +{ /// /// Adds an entity in the database. /// @@ -32,7 +32,6 @@ public interface IRepositoryBase : IReadRepositoryBase where T : class /// /// /// A task that represents the asynchronous operation. - /// The task result contains the . /// Task> AddRangeAsync(IEnumerable entities, CancellationToken cancellationToken = default); @@ -70,5 +69,4 @@ public interface IRepositoryBase : IReadRepositoryBase where T : class /// /// A task that represents the asynchronous operation. Task SaveChangesAsync(CancellationToken cancellationToken = default); - } } diff --git a/Specification/src/Ardalis.Specification/ISingleResultSpecification.cs b/Specification/src/Ardalis.Specification/ISingleResultSpecification.cs index 407d69eb..7fbaebde 100644 --- a/Specification/src/Ardalis.Specification/ISingleResultSpecification.cs +++ b/Specification/src/Ardalis.Specification/ISingleResultSpecification.cs @@ -1,31 +1,30 @@ using System; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// A marker interface for specifications that are meant to return a single entity. Used to constrain methods +/// that accept a Specification and return a single result rather than a collection of results. +/// +[Obsolete("Use ISingleResultSpecification instead. This interface will be removed in a future version of Ardalis.Specification.")] +public interface ISingleResultSpecification { - /// - /// A marker interface for specifications that are meant to return a single entity. Used to constrain methods - /// that accept a Specification and return a single result rather than a collection of results. - /// - [Obsolete("Use ISingleResultSpecification instead. This interface will be removed in a future version of Ardalis.Specification.")] - public interface ISingleResultSpecification - { - } +} - /// - /// Encapsulates query logic for . It is meant to return a single result. - /// - /// The type being queried against. - public interface ISingleResultSpecification : ISpecification//, ISingleResultSpecification - { - } +/// +/// Encapsulates query logic for . It is meant to return a single result. +/// +/// The type being queried against. +public interface ISingleResultSpecification : ISpecification//, ISingleResultSpecification +{ +} - /// - /// Encapsulates query logic for , - /// and projects the result into . It is meant to return a single result. - /// - /// The type being queried against. - /// The type of the result. - public interface ISingleResultSpecification : ISpecification//, ISingleResultSpecification - { - } +/// +/// Encapsulates query logic for , +/// and projects the result into . It is meant to return a single result. +/// +/// The type being queried against. +/// The type of the result. +public interface ISingleResultSpecification : ISpecification//, ISingleResultSpecification +{ } diff --git a/Specification/src/Ardalis.Specification/ISpecification.cs b/Specification/src/Ardalis.Specification/ISpecification.cs index d0ed1609..a28a9b0e 100644 --- a/Specification/src/Ardalis.Specification/ISpecification.cs +++ b/Specification/src/Ardalis.Specification/ISpecification.cs @@ -2,17 +2,17 @@ using System.Collections.Generic; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +/// Encapsulates query logic for , +/// and projects the result into . +/// +/// The type being queried against. +/// The type of the result. +public interface ISpecification : ISpecification { - /// - /// Encapsulates query logic for , - /// and projects the result into . - /// - /// The type being queried against. - /// The type of the result. - public interface ISpecification : ISpecification - { - ISpecificationBuilder Query { get; } + new ISpecificationBuilder Query { get; } /// /// The Select transform function to apply to the element. @@ -30,14 +30,14 @@ public interface ISpecification : ISpecification new Func, IEnumerable>? PostProcessingAction { get; } new IEnumerable Evaluate(IEnumerable entities); - } - - /// - /// Encapsulates query logic for . - /// - /// The type being queried against. - public interface ISpecification - { +} + +/// +/// Encapsulates query logic for . +/// +/// The type being queried against. +public interface ISpecification +{ ISpecificationBuilder Query { get; } /// @@ -151,5 +151,4 @@ public interface ISpecification /// The entity to be validated /// bool IsSatisfiedBy(T entity); - } } diff --git a/Specification/src/Ardalis.Specification/IncludeTypeEnum.cs b/Specification/src/Ardalis.Specification/IncludeTypeEnum.cs index 7a99c3bb..83176da2 100644 --- a/Specification/src/Ardalis.Specification/IncludeTypeEnum.cs +++ b/Specification/src/Ardalis.Specification/IncludeTypeEnum.cs @@ -1,12 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public enum IncludeTypeEnum { - public enum IncludeTypeEnum - { Include = 1, ThenInclude = 2 - } } diff --git a/Specification/src/Ardalis.Specification/OrderTypeEnum.cs b/Specification/src/Ardalis.Specification/OrderTypeEnum.cs index d645c398..0291805b 100644 --- a/Specification/src/Ardalis.Specification/OrderTypeEnum.cs +++ b/Specification/src/Ardalis.Specification/OrderTypeEnum.cs @@ -1,17 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +/// +/// Whether to (subsequently) sort ascending or descending. +/// +public enum OrderTypeEnum { - /// - /// Whether to (subsequently) sort ascending or descending. - /// - public enum OrderTypeEnum - { OrderBy = 1, OrderByDescending = 2, ThenBy = 3, ThenByDescending = 4 - } } diff --git a/Specification/src/Ardalis.Specification/SingleResultSpecification.cs b/Specification/src/Ardalis.Specification/SingleResultSpecification.cs index ce055704..19b6f4ed 100644 --- a/Specification/src/Ardalis.Specification/SingleResultSpecification.cs +++ b/Specification/src/Ardalis.Specification/SingleResultSpecification.cs @@ -1,12 +1,11 @@ -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +public class SingleResultSpecification : Specification, ISingleResultSpecification { - /// - public class SingleResultSpecification : Specification, ISingleResultSpecification - { - } +} - /// - public class SingleResultSpecification : Specification, ISingleResultSpecification - { - } +/// +public class SingleResultSpecification : Specification, ISingleResultSpecification +{ } diff --git a/Specification/src/Ardalis.Specification/Specification.cs b/Specification/src/Ardalis.Specification/Specification.cs index 02019ae9..7f0c42e2 100644 --- a/Specification/src/Ardalis.Specification/Specification.cs +++ b/Specification/src/Ardalis.Specification/Specification.cs @@ -2,11 +2,11 @@ using System.Collections.Generic; using System.Linq.Expressions; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +/// +public class Specification : Specification, ISpecification { - /// - public class Specification : Specification, ISpecification - { public new virtual ISpecificationBuilder Query { get; } protected Specification() @@ -17,12 +17,12 @@ protected Specification() protected Specification(IInMemorySpecificationEvaluator inMemorySpecificationEvaluator) : base(inMemorySpecificationEvaluator) { - this.Query = new SpecificationBuilder(this); + Query = new SpecificationBuilder(this); } public new virtual IEnumerable Evaluate(IEnumerable entities) { - return Evaluator.Evaluate(entities, this); + return Evaluator.Evaluate(entities, this); } /// @@ -33,11 +33,11 @@ protected Specification(IInMemorySpecificationEvaluator inMemorySpecificationEva /// public new Func, IEnumerable>? PostProcessingAction { get; internal set; } = null; - } +} - /// - public class Specification : ISpecification - { +/// +public class Specification : ISpecification +{ protected IInMemorySpecificationEvaluator Evaluator { get; } protected ISpecificationValidator Validator { get; } public virtual ISpecificationBuilder Query { get; } @@ -59,21 +59,21 @@ protected Specification(ISpecificationValidator specificationValidator) protected Specification(IInMemorySpecificationEvaluator inMemorySpecificationEvaluator, ISpecificationValidator specificationValidator) { - this.Evaluator = inMemorySpecificationEvaluator; - this.Validator = specificationValidator; - this.Query = new SpecificationBuilder(this); + Evaluator = inMemorySpecificationEvaluator; + Validator = specificationValidator; + Query = new SpecificationBuilder(this); } /// public virtual IEnumerable Evaluate(IEnumerable entities) { - return Evaluator.Evaluate(entities, this); + return Evaluator.Evaluate(entities, this); } /// public virtual bool IsSatisfiedBy(T entity) { - return Validator.IsValid(entity, this); + return Validator.IsValid(entity, this); } /// @@ -122,5 +122,4 @@ public virtual bool IsSatisfiedBy(T entity) /// public bool IgnoreQueryFilters { get; internal set; } = false; - } } diff --git a/Specification/src/Ardalis.Specification/Validators/ISpecificationValidator.cs b/Specification/src/Ardalis.Specification/Validators/ISpecificationValidator.cs index c7b2914f..701546ef 100644 --- a/Specification/src/Ardalis.Specification/Validators/ISpecificationValidator.cs +++ b/Specification/src/Ardalis.Specification/Validators/ISpecificationValidator.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public interface ISpecificationValidator { - public interface ISpecificationValidator - { bool IsValid(T entity, ISpecification specification); - } } diff --git a/Specification/src/Ardalis.Specification/Validators/IValidator.cs b/Specification/src/Ardalis.Specification/Validators/IValidator.cs index 952a088b..31bb718b 100644 --- a/Specification/src/Ardalis.Specification/Validators/IValidator.cs +++ b/Specification/src/Ardalis.Specification/Validators/IValidator.cs @@ -1,11 +1,6 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public interface IValidator { - public interface IValidator - { bool IsValid(T entity, ISpecification specification); - } } diff --git a/Specification/src/Ardalis.Specification/Validators/SearchValidator.cs b/Specification/src/Ardalis.Specification/Validators/SearchValidator.cs index 1cec047f..80079ad1 100644 --- a/Specification/src/Ardalis.Specification/Validators/SearchValidator.cs +++ b/Specification/src/Ardalis.Specification/Validators/SearchValidator.cs @@ -1,23 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class SearchValidator : IValidator { - public class SearchValidator : IValidator - { private SearchValidator() { } public static SearchValidator Instance { get; } = new SearchValidator(); public bool IsValid(T entity, ISpecification specification) { - foreach (var searchGroup in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) - { - if (searchGroup.Any(c => c.SelectorFunc(entity).Like(c.SearchTerm)) == false) return false; - } + foreach (var searchGroup in specification.SearchCriterias.GroupBy(x => x.SearchGroup)) + { + if (searchGroup.Any(c => c.SelectorFunc(entity).Like(c.SearchTerm)) == false) return false; + } - return true; + return true; } - } } diff --git a/Specification/src/Ardalis.Specification/Validators/SpecificationValidator.cs b/Specification/src/Ardalis.Specification/Validators/SpecificationValidator.cs index 6ec6498e..b2ff6a2c 100644 --- a/Specification/src/Ardalis.Specification/Validators/SpecificationValidator.cs +++ b/Specification/src/Ardalis.Specification/Validators/SpecificationValidator.cs @@ -1,37 +1,34 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification +namespace Ardalis.Specification; + +public class SpecificationValidator : ISpecificationValidator { - public class SpecificationValidator : ISpecificationValidator - { // Will use singleton for default configuration. Yet, it can be instantiated if necessary, with default or provided validators. public static SpecificationValidator Default { get; } = new SpecificationValidator(); - private readonly List validators = new List(); + private readonly List _validators = new(); public SpecificationValidator() { - this.validators.AddRange(new IValidator[] - { - WhereValidator.Instance, - SearchValidator.Instance - }); + _validators.AddRange(new IValidator[] + { + WhereValidator.Instance, + SearchValidator.Instance + }); } public SpecificationValidator(IEnumerable validators) { - this.validators.AddRange(validators); + _validators.AddRange(validators); } public virtual bool IsValid(T entity, ISpecification specification) { - foreach (var partialValidator in validators) - { - if (partialValidator.IsValid(entity, specification) == false) return false; - } + foreach (var partialValidator in _validators) + { + if (partialValidator.IsValid(entity, specification) == false) return false; + } - return true; + return true; } - } } diff --git a/Specification/src/Ardalis.Specification/Validators/WhereValidator.cs b/Specification/src/Ardalis.Specification/Validators/WhereValidator.cs index 4b31f3a1..2068d9d1 100644 --- a/Specification/src/Ardalis.Specification/Validators/WhereValidator.cs +++ b/Specification/src/Ardalis.Specification/Validators/WhereValidator.cs @@ -1,22 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification; -namespace Ardalis.Specification +public class WhereValidator : IValidator { - public class WhereValidator : IValidator - { private WhereValidator() { } public static WhereValidator Instance { get; } = new WhereValidator(); public bool IsValid(T entity, ISpecification specification) { - foreach (var info in specification.WhereExpressions) - { - if (info.FilterFunc(entity) == false) return false; - } + foreach (var info in specification.WhereExpressions) + { + if (info.FilterFunc(entity) == false) return false; + } - return true; + return true; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj b/Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj index b10bf14a..21d6c491 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj +++ b/Specification/tests/Ardalis.Specification.UnitTests/Ardalis.Specification.UnitTests.csproj @@ -1,29 +1,35 @@  - net6.0;net472 - false + net7.0;net472 + 11.0 enable - 9.0 + false - - runtime; build; native; contentfiles; analyzers + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + + + + + + 1701;1702;1591;1573;1712;0618 + diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/IncludableBuilderExtensions_ThenInclude.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/IncludableBuilderExtensions_ThenInclude.cs index 41a7817c..a48ef27f 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/IncludableBuilderExtensions_ThenInclude.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/IncludableBuilderExtensions_ThenInclude.cs @@ -1,55 +1,49 @@ using System.Collections.Generic; -using System.Linq; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class IncludableBuilderExtensions_ThenInclude { - public class IncludableBuilderExtensions_ThenInclude - { [Fact] public void AppendIncludeExpressionInfoToListWithTypeThenInclude_GivenThenIncludeExpression() { - var spec = new StoreIncludeCompanyThenCountrySpec(); + var spec = new StoreIncludeCompanyThenCountrySpec(); - var includeExpressions = spec.IncludeExpressions.ToList(); + var includeExpressions = spec.IncludeExpressions.ToList(); - // The list must have two items, since ThenInclude can be applied once the first level is applied. - includeExpressions.Should().HaveCount(2); + // The list must have two items, since ThenInclude can be applied once the first level is applied. + includeExpressions.Should().HaveCount(2); - includeExpressions[1].Type.Should().Be(IncludeTypeEnum.ThenInclude); + includeExpressions[1].Type.Should().Be(IncludeTypeEnum.ThenInclude); } [Fact] public void AddsNothingToList_GivenDiscardedIncludeChain() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.IncludeExpressions.Should().BeEmpty(); + spec.IncludeExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenThenIncludeExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); + var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); - spec.IncludeExpressions.Should().HaveCount(1); - spec.IncludeExpressions.First().Type.Should().Be(IncludeTypeEnum.Include); - spec.IncludeExpressions.Where(x => x.Type == IncludeTypeEnum.ThenInclude).Should().BeEmpty(); + spec.IncludeExpressions.Should().HaveCount(1); + spec.IncludeExpressions.First().Type.Should().Be(IncludeTypeEnum.Include); + spec.IncludeExpressions.Where(x => x.Type == IncludeTypeEnum.ThenInclude).Should().BeEmpty(); } [Fact] public void ThenInclude_Append_IncludeExpressionInfo_With_EnumerablePreviousPropertyType() { - var spec = new StoreIncludeCompanyThenStoresSpec(); + var spec = new StoreIncludeCompanyThenStoresSpec(); - var includeExpressions = spec.IncludeExpressions.ToList(); + var includeExpressions = spec.IncludeExpressions.ToList(); - includeExpressions.Should().HaveCount(3); + includeExpressions.Should().HaveCount(3); - includeExpressions[2].PreviousPropertyType.Should().Be(typeof(IEnumerable)); + includeExpressions[2].PreviousPropertyType.Should().Be(typeof(IEnumerable)); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenBy.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenBy.cs index 6b7502df..7a759f89 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenBy.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenBy.cs @@ -1,46 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; - -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class OrderedBuilderExtensions_ThenBy { - public class OrderedBuilderExtensions_ThenBy - { [Fact] public void AppendOrderExpressionToListWithThenByType_GivenThenByExpression() { - var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(1); + var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(1); - var orderExpressions = spec.OrderExpressions.ToList(); + var orderExpressions = spec.OrderExpressions.ToList(); - // The list must have two items, since Then can be applied once the first level is applied. - orderExpressions.Should().HaveCount(2); + // The list must have two items, since Then can be applied once the first level is applied. + orderExpressions.Should().HaveCount(2); - orderExpressions[1].OrderType.Should().Be(OrderTypeEnum.ThenBy); + orderExpressions[1].OrderType.Should().Be(OrderTypeEnum.ThenBy); } [Fact] public void AddsNothingToList_GivenDiscardedOrderChain() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenThenByExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); + var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); - spec.OrderExpressions.Should().HaveCount(2); - spec.OrderExpressions.First().OrderType.Should().Be(OrderTypeEnum.OrderBy); - spec.OrderExpressions.Skip(1).First().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); - spec.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.ThenBy).Should().BeEmpty(); + spec.OrderExpressions.Should().HaveCount(2); + spec.OrderExpressions.First().OrderType.Should().Be(OrderTypeEnum.OrderBy); + spec.OrderExpressions.Skip(1).First().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); + spec.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.ThenBy).Should().BeEmpty(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenByDescending.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenByDescending.cs index 0de28e3d..fe67a494 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenByDescending.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/OrderedBuilderExtensions_ThenByDescending.cs @@ -1,46 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; - -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class OrderedBuilderExtensions_ThenByDescending { - public class OrderedBuilderExtensions_ThenByDescending - { [Fact] public void AppendsOrderExpressionToListWithThenByDescendingType_GivenThenByDescendingExpression() { - var spec = new StoresByCompanyOrderedDescByNameThenByDescIdSpec(1); + var spec = new StoresByCompanyOrderedDescByNameThenByDescIdSpec(1); - var orderExpressions = spec.OrderExpressions.ToList(); + var orderExpressions = spec.OrderExpressions.ToList(); - // The list must have two items, since Then can be applied once the first level is applied. - orderExpressions.Should().HaveCount(2); + // The list must have two items, since Then can be applied once the first level is applied. + orderExpressions.Should().HaveCount(2); - orderExpressions[1].OrderType.Should().Be(OrderTypeEnum.ThenByDescending); + orderExpressions[1].OrderType.Should().Be(OrderTypeEnum.ThenByDescending); } [Fact] public void AddsNothingToList_GivenDiscardedOrderChain() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenThenByDescendingExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); + var spec = new CompanyByIdWithFalseConditionsForInnerChains(1); - spec.OrderExpressions.Should().HaveCount(2); - spec.OrderExpressions.First().OrderType.Should().Be(OrderTypeEnum.OrderBy); - spec.OrderExpressions.Skip(1).First().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); - spec.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.ThenByDescending).Should().BeEmpty(); + spec.OrderExpressions.Should().HaveCount(2); + spec.OrderExpressions.First().OrderType.Should().Be(OrderTypeEnum.OrderBy); + spec.OrderExpressions.Skip(1).First().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); + spec.OrderExpressions.Where(x => x.OrderType == OrderTypeEnum.ThenByDescending).Should().BeEmpty(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTracking.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTracking.cs index dc0ec496..3caf3983 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTracking.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTracking.cs @@ -1,41 +1,36 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests.BuilderTests; -namespace Ardalis.Specification.UnitTests.BuilderTests +public class SpecificationBuilderExtensions_AsNoTracking { - public class SpecificationBuilderExtensions_AsNoTracking - { [Fact] public void DoesNothing_GivenSpecWithoutAsNoTracking() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.AsNoTracking.Should().Be(false); + spec.AsNoTracking.Should().Be(false); } [Fact] public void DoesNothing_GivenAsNoTrackingWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.AsNoTracking.Should().Be(false); + spec.AsNoTracking.Should().Be(false); } [Fact] public void FlagsAsNoTracking_GivenSpecWithAsNoTracking() { - var spec = new CompanyByIdAsUntrackedSpec(1); + var spec = new CompanyByIdAsUntrackedSpec(1); - spec.AsNoTracking.Should().Be(true); + spec.AsNoTracking.Should().Be(true); } [Fact] public void FlagsAsNoTracking_GivenSpecWithAsTrackingAndEndWithAsNoTracking() { - var spec = new CompanyByIdWithAsTrackingAsUntrackedSpec(1); + var spec = new CompanyByIdWithAsTrackingAsUntrackedSpec(1); - spec.AsNoTracking.Should().Be(true); + spec.AsNoTracking.Should().Be(true); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution.cs index 3fb6ff80..d6a20829 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution.cs @@ -1,41 +1,36 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests.BuilderTests; -namespace Ardalis.Specification.UnitTests.BuilderTests +public class SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution { - public class SpecificationBuilderExtensions_AsNoTrackingWithIdentityResolution - { [Fact] public void DoesNothing_GivenSpecWithoutAsNoTrackingWithIdentityResolution() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.AsNoTrackingWithIdentityResolution.Should().Be(false); + spec.AsNoTrackingWithIdentityResolution.Should().Be(false); } [Fact] public void DoesNothing_GivenAsNoTrackingWithIdentityResolutionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.AsNoTrackingWithIdentityResolution.Should().Be(false); + spec.AsNoTrackingWithIdentityResolution.Should().Be(false); } [Fact] public void FlagsAsNoTracking_GivenSpecWithAsNoTrackingWithIdentityResolution() { - var spec = new CompanyByIdAsUntrackedWithIdentityResolutionSpec(1); + var spec = new CompanyByIdAsUntrackedWithIdentityResolutionSpec(1); - spec.AsNoTrackingWithIdentityResolution.Should().Be(true); + spec.AsNoTrackingWithIdentityResolution.Should().Be(true); } [Fact] public void FlagsAsNoTracking_GivenSpecWithAsTrackingAndEndWithAsNoTrackingWithIdentityResolution() { - var spec = new CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec(1); + var spec = new CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec(1); - spec.AsNoTrackingWithIdentityResolution.Should().Be(true); + spec.AsNoTrackingWithIdentityResolution.Should().Be(true); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsSplitQuery.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsSplitQuery.cs index 97b688d1..58260597 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsSplitQuery.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsSplitQuery.cs @@ -1,33 +1,28 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests.BuilderTests; -namespace Ardalis.Specification.UnitTests.BuilderTests +public class SpecificationBuilderExtensions_AsSplitQuery { - public class SpecificationBuilderExtensions_AsSplitQuery - { [Fact] public void DoesNothing_GivenSpecWithoutAsSplitQuery() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.AsSplitQuery.Should().Be(false); + spec.AsSplitQuery.Should().Be(false); } [Fact] public void DoesNothing_GivenAsSplitQueryWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.AsSplitQuery.Should().Be(false); + spec.AsSplitQuery.Should().Be(false); } [Fact] public void FlagsAsNoTracking_GivenSpecWithAsSplitQuery() { - var spec = new CompanyByIdAsSplitQuery(1); + var spec = new CompanyByIdAsSplitQuery(1); - spec.AsSplitQuery.Should().Be(true); + spec.AsSplitQuery.Should().Be(true); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs index 9785bac0..a8b2c75b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_AsTracking.cs @@ -1,41 +1,36 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests.BuilderTests; -namespace Ardalis.Specification.UnitTests.BuilderTests +public class SpecificationBuilderExtensions_AsTracking { - public class SpecificationBuilderExtensions_AsTracking - { [Fact] public void DoesNothing_GivenSpecWithoutAsTracking() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.AsTracking.Should().Be(false); + spec.AsTracking.Should().Be(false); } [Fact] public void DoesNothing_GivenAsTrackingWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.AsTracking.Should().Be(false); + spec.AsTracking.Should().Be(false); } [Fact] public void FlagsAsTracking_GivenSpecWithAsTracking() { - var spec = new CompanyByIdAsTrackedSpec(1); + var spec = new CompanyByIdAsTrackedSpec(1); - spec.AsTracking.Should().Be(true); + spec.AsTracking.Should().Be(true); } [Fact] public void FlagsAsTracking_GivenSpecWithAsNoTrackingAndEndWithAsTracking() { - var spec = new CompanyByIdWithAsNoTrackingAsTrackedSpec(1); + var spec = new CompanyByIdWithAsNoTrackingAsTrackedSpec(1); - spec.AsTracking.Should().Be(true); + spec.AsTracking.Should().Be(true); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IgnoreQueryFilters.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IgnoreQueryFilters.cs index be66b0ec..74e79f25 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IgnoreQueryFilters.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IgnoreQueryFilters.cs @@ -1,33 +1,28 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests.BuilderTests; -namespace Ardalis.Specification.UnitTests.BuilderTests +public class SpecificationBuilderExtensions_IgnoreQueryFilters { - public class SpecificationBuilderExtensions_IgnoreQueryFilters - { [Fact] public void DoesNothing_GivenSpecWithoutIgnoreQueryFilters() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.IgnoreQueryFilters.Should().Be(false); + spec.IgnoreQueryFilters.Should().Be(false); } [Fact] public void DoesNothing_GivenIgnoreQueryFiltersWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.IgnoreQueryFilters.Should().Be(false); + spec.IgnoreQueryFilters.Should().Be(false); } [Fact] public void FlagsIgnoreQueryFilters_GivenSpecWithIgnoreQueryFilters() { - var spec = new CompanyByIdIgnoreQueryFilters(1); + var spec = new CompanyByIdIgnoreQueryFilters(1); - spec.IgnoreQueryFilters.Should().Be(true); + spec.IgnoreQueryFilters.Should().Be(true); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Include.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Include.cs index 988eed52..a1e12aeb 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Include.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Include.cs @@ -1,39 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_Include { - public class SpecificationBuilderExtensions_Include - { [Fact] public void AddsNothingToList_GivenNoIncludeExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.IncludeExpressions.Should().BeEmpty(); + spec.IncludeExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenIncludeExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.IncludeExpressions.Should().BeEmpty(); + spec.IncludeExpressions.Should().BeEmpty(); } [Fact] public void AddsIncludeExpressionInfoToListWithTypeInclude_GivenIncludeExpression() { - var spec = new StoreIncludeAddressSpec(); + var spec = new StoreIncludeAddressSpec(); - spec.IncludeExpressions.Should().ContainSingle(); - spec.IncludeExpressions.Single().Type.Should().Be(IncludeTypeEnum.Include); + spec.IncludeExpressions.Should().ContainSingle(); + spec.IncludeExpressions.Single().Type.Should().Be(IncludeTypeEnum.Include); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IncludeString.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IncludeString.cs index 01274505..eaf1c44f 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IncludeString.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_IncludeString.cs @@ -1,41 +1,31 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_IncludeString { - public class SpecificationBuilderExtensions_IncludeString - { [Fact] public void AddsNothingToList_GivenNoIncludeStringExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.WhereExpressions.Should().BeEmpty(); + spec.WhereExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenIncludeStringWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.IncludeStrings.Should().BeEmpty(); + spec.IncludeStrings.Should().BeEmpty(); } [Fact] public void AddsIncludeStringToList_GivenString() { - var spec = new StoreIncludeCompanyThenCountryAsStringSpec(); + var spec = new StoreIncludeCompanyThenCountryAsStringSpec(); - var expected = $"{nameof(Company)}.{nameof(Company.Country)}"; + var expected = $"{nameof(Company)}.{nameof(Company.Country)}"; - spec.IncludeStrings.Should().ContainSingle(); - spec.IncludeStrings.Single().Should().Be(expected); + spec.IncludeStrings.Should().ContainSingle(); + spec.IncludeStrings.Single().Should().Be(expected); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderBy.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderBy.cs index c351b11c..e5b96c70 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderBy.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderBy.cs @@ -1,39 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_OrderBy { - public class SpecificationBuilderExtensions_OrderBy - { [Fact] public void AddsNothingToList_GivenNoOrderExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenOrderExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsOrderExpressionToListWithOrderByType_GivenOrderByExpression() { - var spec = new StoresOrderedSpecByName(); + var spec = new StoresOrderedSpecByName(); - spec.OrderExpressions.Should().ContainSingle(); - spec.OrderExpressions.Single().OrderType.Should().Be(OrderTypeEnum.OrderBy); + spec.OrderExpressions.Should().ContainSingle(); + spec.OrderExpressions.Single().OrderType.Should().Be(OrderTypeEnum.OrderBy); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderByDescending.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderByDescending.cs index 3a59828a..42f586e8 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderByDescending.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_OrderByDescending.cs @@ -1,39 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_OrderByDescending { - public class SpecificationBuilderExtensions_OrderByDescending - { [Fact] public void AddsNothingToList_GivenNoOrderExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenOrderExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.OrderExpressions.Should().BeEmpty(); + spec.OrderExpressions.Should().BeEmpty(); } [Fact] public void AddsOrderExpressionToListWithOrderByDescendingType_GivenOrderByDescendingExpression() { - var spec = new StoresOrderedDescendingByNameSpec(); + var spec = new StoresOrderedDescendingByNameSpec(); - spec.OrderExpressions.Should().ContainSingle(); - spec.OrderExpressions.Single().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); + spec.OrderExpressions.Should().ContainSingle(); + spec.OrderExpressions.Single().OrderType.Should().Be(OrderTypeEnum.OrderByDescending); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_PostProcessingAction.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_PostProcessingAction.cs index c4f22b0a..cd6bfb68 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_PostProcessingAction.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_PostProcessingAction.cs @@ -1,46 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; - -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SpecificationBuilderExtensions_PostProcessingAction { - public class SpecificationBuilderExtensions_PostProcessingAction - { [Fact] public void SetsNothing_GivenNoPostProcessingAction() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.PostProcessingAction.Should().BeNull(); + spec.PostProcessingAction.Should().BeNull(); } [Fact] public void SetsNothing_GivenSelectorSpecWithNoPostProcessingAction() { - var spec = new StoreNamesEmptySpec(); + var spec = new StoreNamesEmptySpec(); - spec.PostProcessingAction.Should().BeNull(); + spec.PostProcessingAction.Should().BeNull(); } [Fact] public void SetsPostProcessingPredicate_GivenPostProcessingAction() { - var spec = new StoreWithPostProcessingActionSpec(); + var spec = new StoreWithPostProcessingActionSpec(); - spec.PostProcessingAction.Should().NotBeNull(); + spec.PostProcessingAction.Should().NotBeNull(); } [Fact] public void SetsPostProcessingPredicate_GivenSelectorSpecWithPostProcessingAction() { - var spec = new StoreNamesWithPostProcessingActionSpec(); + var spec = new StoreNamesWithPostProcessingActionSpec(); - spec.PostProcessingAction.Should().NotBeNull(); + spec.PostProcessingAction.Should().NotBeNull(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Search.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Search.cs index be6a3492..cb32a075 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Search.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Search.cs @@ -1,65 +1,55 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_Search { - public class SpecificationBuilderExtensions_Search - { [Fact] public void AddsNothingToList_GivenNoWhereExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.SearchCriterias.Should().BeEmpty(); + spec.SearchCriterias.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenSearchExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.SearchCriterias.Should().BeEmpty(); + spec.SearchCriterias.Should().BeEmpty(); } [Fact] public void AddsOneCriteriaWithDefaultGroupToList_GivenOneSearchExpressionWithNoGroup() { - var spec = new StoreSearchByNameSpec("test"); + var spec = new StoreSearchByNameSpec("test"); - spec.SearchCriterias.Should().ContainSingle(); - spec.SearchCriterias.Single().SearchTerm.Should().Be("%test%"); - spec.SearchCriterias.Single().SearchGroup.Should().Be(1); + spec.SearchCriterias.Should().ContainSingle(); + spec.SearchCriterias.Single().SearchTerm.Should().Be("%test%"); + spec.SearchCriterias.Single().SearchGroup.Should().Be(1); } [Fact] public void AddsTwoCriteriaWithSameGroupToList_GivenTwoSearchExpressionWithNoGroup() { - var spec = new StoreSearchByNameOrCitySpec("test"); + var spec = new StoreSearchByNameOrCitySpec("test"); - var criterias = spec.SearchCriterias.ToList(); + var criterias = spec.SearchCriterias.ToList(); - criterias.Should().HaveCount(2); - criterias.ForEach(x => x.SearchTerm.Should().Be("%test%")); - criterias.ForEach(x => x.SearchGroup.Should().Be(1)); + criterias.Should().HaveCount(2); + criterias.ForEach(x => x.SearchTerm.Should().Be("%test%")); + criterias.ForEach(x => x.SearchGroup.Should().Be(1)); } [Fact] public void AddsTwoCriteriaWithDifferentGroupToList_GivenTwoSearchExpressionWithDistinctGroup() { - var spec = new StoreSearchByNameAndCitySpec("test"); + var spec = new StoreSearchByNameAndCitySpec("test"); - var criterias = spec.SearchCriterias.ToList(); + var criterias = spec.SearchCriterias.ToList(); - criterias.Should().HaveCount(2); - criterias.ForEach(x => x.SearchTerm.Should().Be("%test%")); - criterias[0].SearchGroup.Should().Be(1); - criterias[1].SearchGroup.Should().Be(2); + criterias.Should().HaveCount(2); + criterias.ForEach(x => x.SearchTerm.Should().Be("%test%")); + criterias[0].SearchGroup.Should().Be(1); + criterias[1].SearchGroup.Should().Be(2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Select.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Select.cs index b23e3028..d979a9a8 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Select.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Select.cs @@ -1,30 +1,20 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_Select { - public class SpecificationBuilderExtensions_Select - { [Fact] public void SetsNothing_GivenNoSelectExpression() { - var spec = new StoreNamesEmptySpec(); + var spec = new StoreNamesEmptySpec(); - spec.Selector.Should().BeNull(); + spec.Selector.Should().BeNull(); } [Fact] public void SetsSelector_GivenSelectExpression() { - var spec = new StoreNamesSpec(); + var spec = new StoreNamesSpec(); - spec.Selector.Should().NotBeNull(); + spec.Selector.Should().NotBeNull(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_SelectMany.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_SelectMany.cs index f3c714f3..18ffd968 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_SelectMany.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_SelectMany.cs @@ -1,25 +1,20 @@ -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; +namespace Ardalis.Specification.UnitTests; -namespace Ardalis.Specification.UnitTests +public class SpecificationBuilderExtensions_SelectMany { - public class SpecificationBuilderExtensions_SelectMany - { [Fact] public void SetsNothing_GivenNoSelectManyExpression() { - var spec = new StoreProductNamesEmptySpec(); + var spec = new StoreProductNamesEmptySpec(); - spec.SelectorMany.Should().BeNull(); + spec.SelectorMany.Should().BeNull(); } [Fact] public void SetsSelectorMany_GivenSelectManyExpression() { - var spec = new StoreProductNamesSpec(); + var spec = new StoreProductNamesSpec(); - spec.SelectorMany.Should().NotBeNull(); + spec.SelectorMany.Should().NotBeNull(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Skip.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Skip.cs index 9a1ceea4..afffca84 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Skip.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Skip.cs @@ -1,39 +1,35 @@ using System; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SpecificationBuilderExtensions_Skip { - public class SpecificationBuilderExtensions_Skip - { [Fact] public void SetsSkipProperty_GivenValue() { - var skip = 1; + var skip = 1; - var spec = new StoreNamesPaginatedSpec(skip, 10); + var spec = new StoreNamesPaginatedSpec(skip, 10); - spec.Skip.Should() - .Be(skip); + spec.Skip.Should() + .Be(skip); } [Fact] public void DoesNothing_GivenSkipWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.Skip.Should().BeNull(); + spec.Skip.Should().BeNull(); } [Fact] public void ThrowsDuplicateSkipException_GivenSkipUsedMoreThanOnce() { - Action sutAction = () => new StoreDuplicateSkipSpec(); + Action sutAction = () => new StoreDuplicateSkipSpec(); - sutAction.Should() - .Throw() - .WithMessage("Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"); + sutAction.Should() + .Throw() + .WithMessage("Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Take.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Take.cs index 4e238011..4c976e4b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Take.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Take.cs @@ -1,37 +1,33 @@ using System; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SpecificationBuilderExtensions_Take { - public class SpecificationBuilderExtensions_Take - { [Fact] public void SetsTakeProperty_GivenValue() { - var take = 10; - var spec = new StoreNamesPaginatedSpec(0, take); + var take = 10; + var spec = new StoreNamesPaginatedSpec(0, take); - spec.Take.Should().Be(take); + spec.Take.Should().Be(take); } [Fact] public void DoesNothing_GivenTakeWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.Take.Should().BeNull(); + spec.Take.Should().BeNull(); } [Fact] public void ThrowsDuplicateTakeException_GivenTakeUsedMoreThanOnce() { - Action sutAction = () => new StoreDuplicateTakeSpec(); + Action sutAction = () => new StoreDuplicateTakeSpec(); - sutAction.Should() - .Throw() - .WithMessage("Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"); + sutAction.Should() + .Throw() + .WithMessage("Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Where.cs b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Where.cs index 97f3c64c..7a14457d 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Where.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/BuilderTests/SpecificationBuilderExtensions_Where.cs @@ -1,46 +1,36 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; - -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SpecificationBuilderExtensions_Where { - public class SpecificationBuilderExtensions_Where - { [Fact] public void AddsNothingToList_GivenNoWhereExpression() { - var spec = new StoreEmptySpec(); + var spec = new StoreEmptySpec(); - spec.WhereExpressions.Should().BeEmpty(); + spec.WhereExpressions.Should().BeEmpty(); } [Fact] public void AddsNothingToList_GivenWhereExpressionWithFalseCondition() { - var spec = new CompanyByIdWithFalseConditions(1); + var spec = new CompanyByIdWithFalseConditions(1); - spec.WhereExpressions.Should().BeEmpty(); + spec.WhereExpressions.Should().BeEmpty(); } [Fact] public void AddsOneExpressionToList_GivenOneWhereExpression() { - var spec = new StoreByIdSpec(1); + var spec = new StoreByIdSpec(1); - spec.WhereExpressions.Should().ContainSingle(); + spec.WhereExpressions.Should().ContainSingle(); } [Fact] public void AddsTwoExpressionsToList_GivenTwoWhereExpressions() { - var spec = new StoreByIdAndNameSpec(1, "name"); + var spec = new StoreByIdAndNameSpec(1, "name"); - spec.WhereExpressions.Should().HaveCount(2); + spec.WhereExpressions.Should().HaveCount(2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/InMemorySpecificationEvaluatorTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/InMemorySpecificationEvaluatorTests.cs index e518f0fe..d856c4d5 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/InMemorySpecificationEvaluatorTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/InMemorySpecificationEvaluatorTests.cs @@ -1,156 +1,146 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Ardalis.Specification.UnitTests.Fixture; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; -using Xunit; - -namespace Ardalis.Specification.UnitTests +using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; +using System; + +namespace Ardalis.Specification.UnitTests; + +public class InMemorySpecificationEvaluatorTests { - public class InMemorySpecificationEvaluatorTests - { [Fact] public void ReturnsStoreWithId10_GivenStoreByIdSpec() { - var spec = new StoreByIdSpec(10); + var spec = new StoreByIdSpec(10); - var store = spec.Evaluate(StoreSeed.Get()).FirstOrDefault(); + var store = spec.Evaluate(StoreSeed.Get()).FirstOrDefault(); - store?.Id.Should().Be(10); + store?.Id.Should().Be(10); } [Fact] public void ReturnsStoreWithIdFrom15To30_GivenStoresByIdListSpec() { - var ids = Enumerable.Range(15, 16); - var spec = new StoresByIdListSpec(ids); + var ids = Enumerable.Range(15, 16); + var spec = new StoresByIdListSpec(ids); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.Count().Should().Be(16); - stores.OrderBy(x => x.Id).First().Id.Should().Be(15); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); + stores.Count().Should().Be(16); + stores.OrderBy(x => x.Id).First().Id.Should().Be(15); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(30); } [Fact] public void ReturnsSecondPageOfStoreNames_GivenStoreNamesPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoreNamesPaginatedSpec(skip, take); + var spec = new StoreNamesPaginatedSpec(skip, take); - var storeNames = spec.Evaluate(StoreSeed.Get()); + var storeNames = spec.Evaluate(StoreSeed.Get()); - storeNames.Count().Should().Be(take); - storeNames.First().Should().Be("Store 11"); - storeNames.Last().Should().Be("Store 20"); + storeNames.Count().Should().Be(take); + storeNames.First().Should().Be("Store 11"); + storeNames.Last().Should().Be("Store 20"); } [Fact] public void ReturnsSecondPageOfStores_GivenStoresPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresPaginatedSpec(skip, take); + var spec = new StoresPaginatedSpec(skip, take); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.Count().Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(11); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); + stores.Count().Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(11); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(20); } [Fact] public void ReturnsOrderStoresByNameDescForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameSpec() { - var spec = new StoresByCompanyOrderedDescByNameSpec(2); + var spec = new StoresByCompanyOrderedDescByNameSpec(2); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID); } [Fact] public void ReturnsOrderStoresByNameDescThenByIdForCompanyWithId2_GivenStoresByCompanyOrderedDescByNameThenByIdSpec() { - var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); + var spec = new StoresByCompanyOrderedDescByNameThenByIdSpec(2); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.First().Id.Should().Be(99); - stores.Last().Id.Should().Be(98); + stores.First().Id.Should().Be(99); + stores.Last().Id.Should().Be(98); } [Fact] public void ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedOrderedDescByNameSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedOrderedDescByNameSpec(2, skip, take); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.Count().Should().Be(take); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); + stores.Count().Should().Be(take); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID); } [Fact] public void ReturnsSecondPageOfStoresForCompanyWithId2_GivenStoresByCompanyPaginatedSpec() { - int take = 10; // pagesize 10 - int skip = (2 - 1) * 10; // page 2 + var take = 10; // pagesize 10 + var skip = (2 - 1) * 10; // page 2 - var spec = new StoresByCompanyPaginatedSpec(2, skip, take); + var spec = new StoresByCompanyPaginatedSpec(2, skip, take); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.Count().Should().Be(take); - stores.OrderBy(x => x.Id).First().Id.Should().Be(61); - stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); + stores.Count().Should().Be(take); + stores.OrderBy(x => x.Id).First().Id.Should().Be(61); + stores.OrderBy(x => x.Id).Last().Id.Should().Be(70); } [Fact] public void ReturnsOrderedStores_GivenStoresOrderedSpecByName() { - var spec = new StoresOrderedSpecByName(); + var spec = new StoresOrderedSpecByName(); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_LAST_ID); } [Fact] public void ReturnsOrderedStores_GivenStoresOrderedDescendingByNameSpec() { - var spec = new StoresOrderedDescendingByNameSpec(); + var spec = new StoresOrderedDescendingByNameSpec(); - var stores = spec.Evaluate(StoreSeed.Get()); + var stores = spec.Evaluate(StoreSeed.Get()); - stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); - stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); + stores.First().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_FIRST_ID); + stores.Last().Id.Should().Be(StoreSeed.ORDERED_BY_NAME_DESC_LAST_ID); } [Fact] public void ThrowsDuplicateOrderChainException_GivenSpecWithMultipleOrderChains() { - var spec = new StoresOrderedTwoChainsSpec(); + var spec = new StoresOrderedTwoChainsSpec(); - Action sutAction = () => spec.Evaluate(StoreSeed.Get()); + Action sutAction = () => spec.Evaluate(StoreSeed.Get()); - sutAction.Should() - .Throw() - .WithMessage("The specification contains more than one Order chain!"); + sutAction.Should() + .Throw() + .WithMessage("The specification contains more than one Order chain!"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchEvaluator_Evaluate.cs b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchEvaluator_Evaluate.cs index 9760fc22..9d9da4d3 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchEvaluator_Evaluate.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchEvaluator_Evaluate.cs @@ -1,24 +1,18 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SearchEvaluator_Evaluate { - public class SearchEvaluator_Evaluate + private static readonly List _people = new() { - public static List Data = new List - { - new Person("James"), - new Person("Robert"), - new Person("Mary"), - new Person("Linda"), - new Person("Michael"), - new Person("David"), - }; + new Person("James"), + new Person("Robert"), + new Person("Mary"), + new Person("Linda"), + new Person("Michael"), + new Person("David"), + }; [Theory] [InlineData("%mes", 1)] @@ -29,27 +23,26 @@ public class SearchEvaluator_Evaluate [InlineData("_[IA]%", 5)] public void ReturnsFilteredList_GivenSearchExpression(string searchTerm, int expectedCount) { - var result = SearchEvaluator.Instance.Evaluate(Data, new PersonSpecification(searchTerm)); + var result = SearchEvaluator.Instance.Evaluate(_people, new PersonSpecification(searchTerm)); - result.Should().HaveCount(expectedCount); + result.Should().HaveCount(expectedCount); } - } +} - public class PersonSpecification : Specification - { +public class PersonSpecification : Specification +{ public PersonSpecification(string searchTerm) { - Query.Search(x => x.Name, searchTerm); + Query.Search(x => x.Name, searchTerm); } - } +} - public class Person - { +public class Person +{ public string Name { get; } public Person(string name) { - Name = name; + Name = name; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchExtension_Like.cs b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchExtension_Like.cs index 137389d7..6c79d78e 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchExtension_Like.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/EvaluatorTests/SearchExtension_Like.cs @@ -1,15 +1,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests.EvaluatorTests +namespace Ardalis.Specification.UnitTests.EvaluatorTests; + +public class SearchExtension_Like { - public class SearchExtension_Like - { [Theory] [InlineData(true, "%", "")] [InlineData(true, "%", " ")] @@ -80,9 +74,9 @@ public class SearchExtension_Like [InlineData(false, "_Stuff_.txt_", "Stuff3.txt4")] public void ReturnsExpectedResult_GivenPatternAndInput(bool expectedResult, string pattern, string input) { - var result = input.Like(pattern); + var result = input.Like(pattern); - result.Should().Be(expectedResult); + result.Should().Be(expectedResult); } [Theory] @@ -90,9 +84,8 @@ public void ReturnsExpectedResult_GivenPatternAndInput(bool expectedResult, stri [InlineData("[]", "asd")] public void ShouldThrowInvalidSearchPattern_GivenInvalidPattern(string pattern, string input) { - Action action = () => input.Like(pattern); + Action action = () => input.Like(pattern); - action.Should().Throw().WithMessage($"Invalid search pattern: {pattern}"); + action.Should().Throw().WithMessage($"Invalid search pattern: {pattern}"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/ConcurrentSelectorsExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/ConcurrentSelectorsExceptionTests.cs index 86fa0067..9c81ce3c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/ConcurrentSelectorsExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/ConcurrentSelectorsExceptionTests.cs @@ -1,28 +1,25 @@ using System; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class ConcurrentSelectorsExceptionTests { - public class ConcurrentSelectorsExceptionTests - { - private const string defaultMessage = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!"; + private const string _defaultMessage = "Concurrent specification selector transforms defined. Ensure only one of the Select() or SelectMany() transforms is used in the same specification!"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new ConcurrentSelectorsException(); + Action action = () => throw new ConcurrentSelectorsException(); - action.Should().Throw().WithMessage(defaultMessage); + action.Should().Throw().WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new ConcurrentSelectorsException(inner); + var inner = new Exception("test"); + Action action = () => throw new ConcurrentSelectorsException(inner); - action.Should().Throw().WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw().WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateOrderChainExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateOrderChainExceptionTests.cs index ef5a30d3..7b3555c8 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateOrderChainExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateOrderChainExceptionTests.cs @@ -1,30 +1,25 @@ using System; -using System.Collections.Generic; -using System.Text; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class DuplicateOrderChainExceptionTests { - public class DuplicateOrderChainExceptionTests - { - private const string defaultMessage = "The specification contains more than one Order chain!"; + private const string _defaultMessage = "The specification contains more than one Order chain!"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new DuplicateOrderChainException(); + Action action = () => throw new DuplicateOrderChainException(); - action.Should().Throw().WithMessage(defaultMessage); + action.Should().Throw().WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new DuplicateOrderChainException(inner); + var inner = new Exception("test"); + Action action = () => throw new DuplicateOrderChainException(inner); - action.Should().Throw().WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw().WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateSkipExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateSkipExceptionTests.cs index 6d3f4c95..e3955abf 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateSkipExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateSkipExceptionTests.cs @@ -1,30 +1,25 @@ using System; -using System.Collections.Generic; -using System.Text; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class DuplicateSkipExceptionTests { - public class DuplicateSkipExceptionTests - { - private const string defaultMessage = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"; + private const string _defaultMessage = "Duplicate use of Skip(). Ensure you don't use Skip() more than once in the same specification!"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new DuplicateSkipException(); + Action action = () => throw new DuplicateSkipException(); - action.Should().Throw().WithMessage(defaultMessage); + action.Should().Throw().WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new DuplicateSkipException(inner); + var inner = new Exception("test"); + Action action = () => throw new DuplicateSkipException(inner); - action.Should().Throw().WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw().WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateTakeExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateTakeExceptionTests.cs index 147646c8..5dc9c0bc 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateTakeExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/DuplicateTakeExceptionTests.cs @@ -1,30 +1,25 @@ using System; -using System.Collections.Generic; -using System.Text; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class DuplicateTakeExceptionTests { - public class DuplicateTakeExceptionTests - { - private const string defaultMessage = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"; + private const string _defaultMessage = "Duplicate use of Take(). Ensure you don't use Take() more than once in the same specification!"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new DuplicateTakeException(); + Action action = () => throw new DuplicateTakeException(); - action.Should().Throw().WithMessage(defaultMessage); + action.Should().Throw().WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new DuplicateTakeException(inner); + var inner = new Exception("test"); + Action action = () => throw new DuplicateTakeException(inner); - action.Should().Throw().WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw().WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/InvalidSearchPatternExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/InvalidSearchPatternExceptionTests.cs index 3c0fa1ff..4a28fdef 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/InvalidSearchPatternExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/InvalidSearchPatternExceptionTests.cs @@ -1,31 +1,26 @@ using System; -using System.Collections.Generic; -using System.Text; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class InvalidSearchPatternExceptionTests { - public class InvalidSearchPatternExceptionTests - { - private const string defaultMessage = "Invalid search pattern: " + pattern; - private const string pattern = "x"; + private const string _defaultMessage = "Invalid search pattern: " + _pattern; + private const string _pattern = "x"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new InvalidSearchPatternException(pattern); + Action action = () => throw new InvalidSearchPatternException(_pattern); - action.Should().Throw(pattern).WithMessage(defaultMessage); + action.Should().Throw(_pattern).WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new InvalidSearchPatternException(pattern, inner); + var inner = new Exception("test"); + Action action = () => throw new InvalidSearchPatternException(_pattern, inner); - action.Should().Throw(pattern).WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw(_pattern).WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/SelectorNotFoundExceptionTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/SelectorNotFoundExceptionTests.cs index 4b54d141..5a4aec05 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/SelectorNotFoundExceptionTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ExceptionTests/SelectorNotFoundExceptionTests.cs @@ -1,28 +1,25 @@ using System; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class SelectorNotFoundExceptionTests { - public class SelectorNotFoundExceptionTests - { - private const string defaultMessage = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!"; + private const string _defaultMessage = "The specification must have a selector transform defined. Ensure either Select() or SelectMany() is used in the specification!"; [Fact] public void ThrowWithDefaultConstructor() { - Action action = () => throw new SelectorNotFoundException(); + Action action = () => throw new SelectorNotFoundException(); - action.Should().Throw().WithMessage(defaultMessage); + action.Should().Throw().WithMessage(_defaultMessage); } [Fact] public void ThrowWithInnerException() { - Exception inner = new Exception("test"); - Action action = () => throw new SelectorNotFoundException(inner); + var inner = new Exception("test"); + Action action = () => throw new SelectorNotFoundException(inner); - action.Should().Throw().WithMessage(defaultMessage).WithInnerException().WithMessage("test"); + action.Should().Throw().WithMessage(_defaultMessage).WithInnerException().WithMessage("test"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Address.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Address.cs index 8018c28f..93b8763e 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Address.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Address.cs @@ -1,11 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification.UnitTests.Fixture.Entities; -namespace Ardalis.Specification.UnitTests.Fixture.Entities +public class Address { - public class Address - { public int Id { get; set; } public string? Street { get; set; } @@ -14,7 +10,6 @@ public class Address public object GetSomethingFromAddress() { - return new object(); + return new object(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Company.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Company.cs index ac748568..f4dd0588 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Company.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Company.cs @@ -1,11 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities +namespace Ardalis.Specification.UnitTests.Fixture.Entities; + +public class Company { - public class Company - { public int Id { get; set; } public string? Name { get; set; } @@ -13,5 +11,4 @@ public class Company public Country? Country { get; set; } public List Stores { get; set; } = new List(); - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Country.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Country.cs index 6fb115e9..0a778f4a 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Country.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Country.cs @@ -1,14 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities +namespace Ardalis.Specification.UnitTests.Fixture.Entities; + +public class Country { - public class Country - { public int Id { get; set; } public string? Name { get; set; } public List Companies { get; set; } = new List(); - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Product.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Product.cs index acda3ac9..bc78bf00 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Product.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Product.cs @@ -1,15 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.Specification.UnitTests.Fixture.Entities; -namespace Ardalis.Specification.UnitTests.Fixture.Entities +public class Product { - public class Product - { public int Id { get; set; } public string? Name { get; set; } public int StoreId { get; set; } public Store? Store { get; set; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/AddressSeed.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/AddressSeed.cs index c82320ee..c5cdfc2b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/AddressSeed.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/AddressSeed.cs @@ -1,28 +1,27 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds +namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +public class AddressSeed { - public class AddressSeed - { +#pragma warning disable IDE1006 // Naming Styles public const string VALID_STREET_FOR_STOREID1 = "Street 1"; +#pragma warning restore IDE1006 // Naming Styles public static List
Get() { - var addresses = new List
(); + var addresses = new List
(); - for (int i = 1; i <= 100; i++) - { - addresses.Add(new Address() + for (var i = 1; i <= 100; i++) { - Id = i, - Street = $"Street {i}", - StoreId = i - }); - } + addresses.Add(new Address() + { + Id = i, + Street = $"Street {i}", + StoreId = i + }); + } - return addresses; + return addresses; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CompanySeed.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CompanySeed.cs index 69a91eab..b38c6bf9 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CompanySeed.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CompanySeed.cs @@ -1,40 +1,40 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds +namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +public class CompanySeed { - public class CompanySeed - { +#pragma warning disable IDE1006 // Naming Styles public const int VALID_COMPANY_ID = 1; public const string VALID_COMPANY_NAME = "Company 1"; +#pragma warning restore IDE1006 // Naming Styles public static List Get() { - var companies = new List(); - - companies.Add(new Company() + var companies = new List + { + new Company() { Id = 1, Name = "Company 1", CountryId = 1, - }); + }, - companies.Add(new Company() + new Company() { Id = 2, Name = "Company 2", CountryId = 2, - }); + }, - companies.Add(new Company() + new Company() { Id = 3, Name = "Company 3", CountryId = 1, - }); + } + }; - return companies; + return companies; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CountrySeed.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CountrySeed.cs index aaf83868..084d161c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CountrySeed.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/CountrySeed.cs @@ -1,28 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds +namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +public class CountrySeed { - public class CountrySeed - { public static List Get() { - var countries = new List(); + var countries = new List(); - countries.Add(new Country() - { - Id = 1, - Name = "Country 1", - }); + countries.Add(new Country() + { + Id = 1, + Name = "Country 1", + }); - countries.Add(new Country() - { - Id = 2, - Name = "Country 2", - }); + countries.Add(new Country() + { + Id = 2, + Name = "Country 2", + }); - return countries; + return countries; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/ProductSeed.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/ProductSeed.cs index ffef31c1..2c75ae09 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/ProductSeed.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/ProductSeed.cs @@ -1,33 +1,34 @@ using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds +namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +public class ProductSeed { - public class ProductSeed - { +#pragma warning disable IDE1006 // Naming Styles public const int TOTAL_PRODUCT_COUNT = 100; public const string VALID_PRODUCT_NAME = "Product 1"; +#pragma warning restore IDE1006 // Naming Styles public static List Get() { - var products = new List(); + var products = new List(); - for (int i = 1; i < TOTAL_PRODUCT_COUNT; i = i + 2) - { - products.Add(new Product() - { - Id = i, - Name = $"Product {i}", - StoreId = i, - }); - products.Add(new Product() + for (var i = 1; i < TOTAL_PRODUCT_COUNT; i += 2) { - Id = i + 1, - Name = $"Product {i + 1}", - StoreId = i, - }); - } + products.Add(new Product() + { + Id = i, + Name = $"Product {i}", + StoreId = i, + }); + products.Add(new Product() + { + Id = i + 1, + Name = $"Product {i + 1}", + StoreId = i, + }); + } - return products; + return products; } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/StoreSeed.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/StoreSeed.cs index 95429d9e..b5f2f180 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/StoreSeed.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Seeds/StoreSeed.cs @@ -1,12 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds +namespace Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +public class StoreSeed { - public class StoreSeed - { +#pragma warning disable IDE1006 // Naming Styles public const int VALID_STORE_ID = 1; public const string VALID_STORE_NAME = "Store 1"; public const string VALID_STORE_City = "City 1"; @@ -28,50 +26,50 @@ public class StoreSeed public const int ORDERED_BY_NAME_DESC_FOR_COMPANY2_LAST_ID = 98; public const int ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_FIRST_ID = 89; public const int ORDERED_BY_NAME_DESC_FOR_COMPANY2_PAGE2_LAST_ID = 80; +#pragma warning restore IDE1006 // Naming Styles public static List Get() { - var stores = new List(); + var stores = new List(); - for (int i = 1; i <= 50; i++) - { - stores.Add(new Store() + for (var i = 1; i <= 50; i++) { - Id = i, - Name = $"Store {i}", - City = $"City {i}", - AddressId = i, - CompanyId = 1, - }); - } - for (int i = 51; i <= 100; i++) - { - stores.Add(new Store() + stores.Add(new Store() + { + Id = i, + Name = $"Store {i}", + City = $"City {i}", + AddressId = i, + CompanyId = 1, + }); + } + for (var i = 51; i <= 100; i++) { - Id = i, - Name = $"Store {i}", - City = $"City {i}", - AddressId = i, - CompanyId = 2, - }); - } + stores.Add(new Store() + { + Id = i, + Name = $"Store {i}", + City = $"City {i}", + AddressId = i, + CompanyId = 2, + }); + } - stores[49 - 1].Name = "ZZZ"; - stores[48 - 1].Name = "AAA"; - stores[99 - 1].Name = "YYY"; - stores[98 - 1].Name = "BBB"; + stores[49 - 1].Name = "ZZZ"; + stores[48 - 1].Name = "AAA"; + stores[99 - 1].Name = "YYY"; + stores[98 - 1].Name = "BBB"; - stores[100 - 1].Name = "Store 999"; + stores[100 - 1].Name = "Store 999"; - stores[50 - 1].City = "ABCDEFGH"; - stores[50 - 1].Name = "ABCEFGH"; + stores[50 - 1].City = "ABCDEFGH"; + stores[50 - 1].Name = "ABCEFGH"; - return stores; + return stores; } internal static IQueryable AsQueryable() { - return Get().AsQueryable(); + return Get().AsQueryable(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Store.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Store.cs index 6775a0eb..29e4b3b0 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Store.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Entities/Store.cs @@ -1,9 +1,9 @@ using System.Collections.Generic; -namespace Ardalis.Specification.UnitTests.Fixture.Entities +namespace Ardalis.Specification.UnitTests.Fixture.Entities; + +public class Store { - public class Store - { public int Id { get; set; } public string? Name { get; set; } public string? City { get; set; } @@ -16,9 +16,8 @@ public class Store public List Products { get; set; } = new List(); - public object GetSomethingFromStore() + public static object GetSomethingFromStore() { - return new object(); + return new object(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsSplitQuery.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsSplitQuery.cs index 142ff1cd..7bf8fa03 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsSplitQuery.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsSplitQuery.cs @@ -1,15 +1,12 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdAsSplitQuery : Specification, ISingleResultSpecification { - public class CompanyByIdAsSplitQuery : Specification, ISingleResultSpecification - { public CompanyByIdAsSplitQuery(int id) { - Query.Where(company => company.Id == id) - .Include(x => x.Stores) - .ThenInclude(x => x.Products) - .AsSplitQuery(); + Query.Where(company => company.Id == id) + .Include(x => x.Stores) + .ThenInclude(x => x.Products) + .AsSplitQuery(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsTrackedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsTrackedSpec.cs index 88efb470..9775a9bd 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsTrackedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsTrackedSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdAsTrackedSpec : Specification, ISingleResultSpecification { - public class CompanyByIdAsTrackedSpec : Specification, ISingleResultSpecification - { public CompanyByIdAsTrackedSpec(int id) { - Query.Where(company => company.Id == id).AsTracking(); + Query.Where(company => company.Id == id).AsTracking(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedSpec.cs index c927e1c1..362a76ae 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdAsUntrackedSpec : Specification, ISingleResultSpecification { - public class CompanyByIdAsUntrackedSpec : Specification, ISingleResultSpecification - { public CompanyByIdAsUntrackedSpec(int id) { - Query.Where(company => company.Id == id).AsNoTracking(); + Query.Where(company => company.Id == id).AsNoTracking(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedWithIdentityResolutionSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedWithIdentityResolutionSpec.cs index 8da92f0e..26e73bdd 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedWithIdentityResolutionSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdAsUntrackedWithIdentityResolutionSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdAsUntrackedWithIdentityResolutionSpec : Specification, ISingleResultSpecification { - public class CompanyByIdAsUntrackedWithIdentityResolutionSpec : Specification, ISingleResultSpecification - { public CompanyByIdAsUntrackedWithIdentityResolutionSpec(int id) { - Query.Where(company => company.Id == id).AsNoTrackingWithIdentityResolution(); + Query.Where(company => company.Id == id).AsNoTrackingWithIdentityResolution(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIgnoreQueryFilters.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIgnoreQueryFilters.cs index 3514d637..84107c8b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIgnoreQueryFilters.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIgnoreQueryFilters.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdIgnoreQueryFilters : Specification, ISingleResultSpecification { - public class CompanyByIdIgnoreQueryFilters : Specification, ISingleResultSpecification - { public CompanyByIdIgnoreQueryFilters(int id) { - Query.Where(company => company.Id == id).IgnoreQueryFilters(); + Query.Where(company => company.Id == id).IgnoreQueryFilters(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeAddressSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeAddressSpec.cs index 2d655299..00136d09 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeAddressSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeAddressSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdIncludeStoresThenIncludeAddressSpec : Specification, ISingleResultSpecification { - public class CompanyByIdIncludeStoresThenIncludeAddressSpec : Specification, ISingleResultSpecification - { public CompanyByIdIncludeStoresThenIncludeAddressSpec(int id) { - Query.Where(x => x.Id == id) - .Include(x => x.Stores) - .ThenInclude(x => x.Address); + Query.Where(x => x.Id == id) + .Include(x => x.Stores) + .ThenInclude(x => x.Address); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeProductsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeProductsSpec.cs index 85948f8b..5e492a84 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeProductsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdIncludeStoresThenIncludeProductsSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdIncludeStoresThenIncludeProductsSpec : Specification, ISingleResultSpecification { - public class CompanyByIdIncludeStoresThenIncludeProductsSpec : Specification, ISingleResultSpecification - { public CompanyByIdIncludeStoresThenIncludeProductsSpec(int id) { - Query.Where(x => x.Id == id) - .Include(x => x.Stores) - .ThenInclude(x => x.Products); + Query.Where(x => x.Id == id) + .Include(x => x.Stores) + .ThenInclude(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdSpec.cs index 9ffc3458..4e3b3123 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdSpec : Specification, ISingleResultSpecification { - public class CompanyByIdSpec : Specification, ISingleResultSpecification - { public CompanyByIdSpec(int id) { - Query.Where(company => company.Id == id); + Query.Where(company => company.Id == id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsNoTrackingAsTrackedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsNoTrackingAsTrackedSpec.cs index 342e3f59..5b08268e 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsNoTrackingAsTrackedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsNoTrackingAsTrackedSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdWithAsNoTrackingAsTrackedSpec : Specification, ISingleResultSpecification { - public class CompanyByIdWithAsNoTrackingAsTrackedSpec : Specification, ISingleResultSpecification - { public CompanyByIdWithAsNoTrackingAsTrackedSpec(int id) { - Query.Where(company => company.Id == id).AsNoTracking().AsNoTrackingWithIdentityResolution().AsTracking(); + Query.Where(company => company.Id == id).AsNoTracking().AsNoTrackingWithIdentityResolution().AsTracking(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedSpec.cs index 16201d6d..a7db5988 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdWithAsTrackingAsUntrackedSpec : Specification, ISingleResultSpecification { - public class CompanyByIdWithAsTrackingAsUntrackedSpec : Specification, ISingleResultSpecification - { public CompanyByIdWithAsTrackingAsUntrackedSpec(int id) { - Query.Where(company => company.Id == id).AsTracking().AsNoTracking(); + Query.Where(company => company.Id == id).AsTracking().AsNoTracking(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec.cs index f4fd1197..1427bf25 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec : Specification, + ISingleResultSpecification { - public class CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec : Specification, - ISingleResultSpecification - { public CompanyByIdWithAsTrackingAsUntrackedWithIdentityResolutionSpec(int id) { - Query.Where(company => company.Id == id).AsTracking().AsNoTrackingWithIdentityResolution(); + Query.Where(company => company.Id == id).AsTracking().AsNoTrackingWithIdentityResolution(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditions.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditions.cs index 3be772f7..6b6af030 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditions.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditions.cs @@ -1,29 +1,26 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdWithFalseConditions : Specification, ISingleResultSpecification { - public class CompanyByIdWithFalseConditions : Specification, ISingleResultSpecification - { public CompanyByIdWithFalseConditions(int id) { - Query.Where(x => x.Id == id, false) - .OrderBy(x => x.Id, false) - .ThenBy(x => x.Name) - .ThenByDescending(x => x.Name) - .OrderByDescending(x => x.Id, false) - .ThenBy(x => x.Name) - .ThenByDescending(x => x.Name) - .Include(x => x.Stores, false) - .ThenInclude(x => x.Products) - .Include(nameof(Store), false) - .Take(10, false) - .Skip(10, false) - .AsNoTracking(false) - .AsNoTrackingWithIdentityResolution(false) - .AsSplitQuery(false) - .IgnoreQueryFilters(false) - .Search(x => x.Name!, "asd", false) - .EnableCache(nameof(CompanyByIdWithFalseConditions), false, id); + Query.Where(x => x.Id == id, false) + .OrderBy(x => x.Id, false) + .ThenBy(x => x.Name) + .ThenByDescending(x => x.Name) + .OrderByDescending(x => x.Id, false) + .ThenBy(x => x.Name) + .ThenByDescending(x => x.Name) + .Include(x => x.Stores, false) + .ThenInclude(x => x.Products) + .Include(nameof(Store), false) + .Take(10, false) + .Skip(10, false) + .AsNoTracking(false) + .AsNoTrackingWithIdentityResolution(false) + .AsSplitQuery(false) + .IgnoreQueryFilters(false) + .Search(x => x.Name!, "asd", false) + .EnableCache(nameof(CompanyByIdWithFalseConditions), false, id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditionsForInnerChains.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditionsForInnerChains.cs index 4d64b3ba..196a8c96 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditionsForInnerChains.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/CompanyByIdWithFalseConditionsForInnerChains.cs @@ -1,30 +1,27 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyByIdWithFalseConditionsForInnerChains : Specification, ISingleResultSpecification { - public class CompanyByIdWithFalseConditionsForInnerChains : Specification, ISingleResultSpecification - { public CompanyByIdWithFalseConditionsForInnerChains(int id) { - Query.Where(x => x.Id == id, false) - .OrderBy(x => x.Id) - .ThenBy(x => x.Name, false) - .ThenByDescending(x => x.Name) - .OrderByDescending(x => x.Id) - .ThenByDescending(x => x.Name, false) - .ThenBy(x => x.Name) - .Include(x => x.Stores) - .ThenInclude(x => x.Products, false) - .ThenInclude(x => x.Store) - .Include(nameof(Store), false) - .Take(10, false) - .Skip(10, false) - .AsNoTracking(false) - .AsNoTrackingWithIdentityResolution(false) - .AsSplitQuery(false) - .IgnoreQueryFilters(false) - .Search(x => x.Name!, "asd", false) - .EnableCache(nameof(CompanyByIdWithFalseConditions), false, id); + Query.Where(x => x.Id == id, false) + .OrderBy(x => x.Id) + .ThenBy(x => x.Name, false) + .ThenByDescending(x => x.Name) + .OrderByDescending(x => x.Id) + .ThenByDescending(x => x.Name, false) + .ThenBy(x => x.Name) + .Include(x => x.Stores) + .ThenInclude(x => x.Products, false) + .ThenInclude(x => x.Store) + .Include(nameof(Store), false) + .Take(10, false) + .Skip(10, false) + .AsNoTracking(false) + .AsNoTrackingWithIdentityResolution(false) + .AsSplitQuery(false) + .IgnoreQueryFilters(false) + .Search(x => x.Name!, "asd", false) + .EnableCache(nameof(CompanyByIdWithFalseConditions), false, id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdAndNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdAndNameSpec.cs index 959acd2c..febc3719 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdAndNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdAndNameSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdAndNameSpec : Specification { - public class StoreByIdAndNameSpec : Specification - { - public StoreByIdAndNameSpec(int Id, string name) + public StoreByIdAndNameSpec(int id, string name) { - Query.Where(x => x.Id == Id) - .Where(x => x.Name == name); + Query.Where(x => x.Id == id) + .Where(x => x.Name == name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressAndProductsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressAndProductsSpec.cs index 7534700f..48f88a45 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressAndProductsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressAndProductsSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdIncludeAddressAndProductsSpec : Specification, ISingleResultSpecification { - public class StoreByIdIncludeAddressAndProductsSpec : Specification, ISingleResultSpecification - { public StoreByIdIncludeAddressAndProductsSpec(int id) { - Query.Where(x => x.Id == id); - Query.Include(x => x.Address); - Query.Include(x => x.Products); + Query.Where(x => x.Id == id); + Query.Include(x => x.Address); + Query.Include(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressSpec.cs index 37f50348..b3d2be23 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeAddressSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdIncludeAddressSpec : Specification, ISingleResultSpecification { - public class StoreByIdIncludeAddressSpec : Specification, ISingleResultSpecification - { public StoreByIdIncludeAddressSpec(int id) { - Query.Where(x => x.Id == id) - .Include(x => x.Address); + Query.Where(x => x.Id == id) + .Include(x => x.Address); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec.cs index 8c55bb14..767e4279 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec : Specification, ISingleResultSpecification { - public class StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec : Specification, ISingleResultSpecification - { public StoreByIdIncludeCompanyAndCountryAndStoresForCompanySpec(int id) { - Query.Where(x => x.Id == id); - Query.Include(x => x.Company).ThenInclude(x => x!.Country); - Query.Include(x => x.Company).ThenInclude(x => x!.Stores).ThenInclude(x => x.Products); + Query.Where(x => x.Id == id); + Query.Include(x => x.Company).ThenInclude(x => x!.Country); + Query.Include(x => x.Company).ThenInclude(x => x!.Stores).ThenInclude(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsSpec.cs index a8bcb0b7..ecfe7995 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdIncludeProductsSpec : Specification, ISingleResultSpecification { - public class StoreByIdIncludeProductsSpec : Specification, ISingleResultSpecification - { public StoreByIdIncludeProductsSpec(int id) { - Query.Where(x => x.Id == id) - .Include(x => x.Products); + Query.Where(x => x.Id == id) + .Include(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsUsingStringSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsUsingStringSpec.cs index 97272316..befc9a81 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsUsingStringSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdIncludeProductsUsingStringSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdIncludeProductsUsingStringSpec : Specification, ISingleResultSpecification { - public class StoreByIdIncludeProductsUsingStringSpec : Specification, ISingleResultSpecification - { public StoreByIdIncludeProductsUsingStringSpec(int id) { - Query.Where(x => x.Id == id) - .Include(nameof(Store.Products)); + Query.Where(x => x.Id == id) + .Include(nameof(Store.Products)); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameAndCitySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameAndCitySpec.cs index e5125d9e..b5875851 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameAndCitySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameAndCitySpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdSearchByNameAndCitySpec : Specification { - public class StoreByIdSearchByNameAndCitySpec : Specification - { public StoreByIdSearchByNameAndCitySpec(int id, string searchTerm) { - Query.Where(x => x.Id == id) - .Search(x => x.Name!, "%" + searchTerm + "%", 1) - .Search(x => x.City!, "%" + searchTerm + "%", 2); + Query.Where(x => x.Id == id) + .Search(x => x.Name!, "%" + searchTerm + "%", 1) + .Search(x => x.City!, "%" + searchTerm + "%", 2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameOrCitySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameOrCitySpec.cs index 626b6d4c..b9b50326 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameOrCitySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSearchByNameOrCitySpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdSearchByNameOrCitySpec : Specification { - public class StoreByIdSearchByNameOrCitySpec : Specification - { public StoreByIdSearchByNameOrCitySpec(int id, string searchTerm) { - Query.Where(x => x.Id == id) - .Search(x => x.Name!, "%" + searchTerm + "%") - .Search(x => x.City!, "%" + searchTerm + "%"); + Query.Where(x => x.Id == id) + .Search(x => x.Name!, "%" + searchTerm + "%") + .Search(x => x.City!, "%" + searchTerm + "%"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSpec.cs index 275d92ec..4815d145 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreByIdSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreByIdSpec : Specification { - public class StoreByIdSpec : Specification - { - public StoreByIdSpec(int Id) + public StoreByIdSpec(int id) { - Query.Where(x => x.Id == Id); + Query.Where(x => x.Id == id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateSkipSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateSkipSpec.cs index 30269ad6..321a5658 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateSkipSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateSkipSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreDuplicateSkipSpec : Specification { - public class StoreDuplicateSkipSpec : Specification - { public StoreDuplicateSkipSpec() { - Query.Skip(1) - .Skip(2); + Query.Skip(1) + .Skip(2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateTakeSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateTakeSpec.cs index 7c57fcab..2f2e755c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateTakeSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreDuplicateTakeSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreDuplicateTakeSpec : Specification { - public class StoreDuplicateTakeSpec : Specification - { public StoreDuplicateTakeSpec() { - Query.Take(1) - .Take(2); + Query.Take(1) + .Take(2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreEmptySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreEmptySpec.cs index 4d0bc45b..f6430750 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreEmptySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreEmptySpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreEmptySpec : Specification { - public class StoreEmptySpec : Specification - { public StoreEmptySpec() { } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesEmptySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesEmptySpec.cs index 2593d0b9..54568e4e 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesEmptySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesEmptySpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreNamesEmptySpec : Specification { - public class StoreNamesEmptySpec : Specification - { public StoreNamesEmptySpec() { } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesPaginatedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesPaginatedSpec.cs index f15756f4..cbccbb12 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesPaginatedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesPaginatedSpec.cs @@ -1,16 +1,13 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreNamesPaginatedSpec : Specification { - public class StoreNamesPaginatedSpec : Specification - { public StoreNamesPaginatedSpec(int skip, int take) { - Query.OrderBy(x => x.Id) - .Skip(skip) - .Take(take); + Query.OrderBy(x => x.Id) + .Skip(skip) + .Take(take); - Query.Select(x => x.Name); + Query.Select(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesSpec.cs index 38a3ad4c..df3972be 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreNamesSpec : Specification { - public class StoreNamesSpec : Specification - { public StoreNamesSpec() { - Query.Select(x => x.Name); + Query.Select(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesWithPostProcessingActionSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesWithPostProcessingActionSpec.cs index 44d85f05..ec7e175b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesWithPostProcessingActionSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreNamesWithPostProcessingActionSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreNamesWithPostProcessingActionSpec : Specification { - public class StoreNamesWithPostProcessingActionSpec : Specification - { public StoreNamesWithPostProcessingActionSpec() { - Query.Select(x => x.Name) - .PostProcessingAction(x => x); + Query.Select(x => x.Name) + .PostProcessingAction(x => x); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesEmptySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesEmptySpec.cs index 56d82fa2..95dad8be 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesEmptySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesEmptySpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreProductNamesEmptySpec : Specification { - public class StoreProductNamesEmptySpec : Specification - { public StoreProductNamesEmptySpec() { } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesSpec.cs index 401c35bd..9a342987 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreProductNamesSpec.cs @@ -1,13 +1,9 @@ -using System.Linq; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreProductNamesSpec : Specification { - public class StoreProductNamesSpec : Specification - { public StoreProductNamesSpec() { - Query.SelectMany(s => s.Products.Select(p => p.Name)); + Query.SelectMany(s => s.Products.Select(p => p.Name)); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameAndCitySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameAndCitySpec.cs index 3efccacc..2b52eb71 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameAndCitySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameAndCitySpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreSearchByNameAndCitySpec : Specification { - public class StoreSearchByNameAndCitySpec : Specification - { public StoreSearchByNameAndCitySpec(string searchTerm) { - Query.Search(x => x.Name!, "%" + searchTerm + "%", 1) - .Search(x => x.City!, "%" + searchTerm + "%", 2); + Query.Search(x => x.Name!, "%" + searchTerm + "%", 1) + .Search(x => x.City!, "%" + searchTerm + "%", 2); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameOrCitySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameOrCitySpec.cs index 643f0a1c..e2d9d4d7 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameOrCitySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameOrCitySpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreSearchByNameOrCitySpec : Specification { - public class StoreSearchByNameOrCitySpec : Specification - { public StoreSearchByNameOrCitySpec(string searchTerm) { - Query.Search(x => x.Name!, "%" + searchTerm + "%") - .Search(x => x.City!, "%" + searchTerm + "%"); + Query.Search(x => x.Name!, "%" + searchTerm + "%") + .Search(x => x.City!, "%" + searchTerm + "%"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameSpec.cs index 3cd31ca6..8f5e69d2 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreSearchByNameSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreSearchByNameSpec : Specification { - public class StoreSearchByNameSpec : Specification - { public StoreSearchByNameSpec(string searchTerm) { - Query.Search(x => x.Name!, "%" + searchTerm + "%"); + Query.Search(x => x.Name!, "%" + searchTerm + "%"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreWithPostProcessingActionSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreWithPostProcessingActionSpec.cs index a8d93937..8649b60f 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreWithPostProcessingActionSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoreWithPostProcessingActionSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreWithPostProcessingActionSpec : Specification { - public class StoreWithPostProcessingActionSpec : Specification - { public StoreWithPostProcessingActionSpec() { - Query.PostProcessingAction(x => x); + Query.PostProcessingAction(x => x); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameSpec.cs index 1d805b02..96def25b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresByCompanyOrderedDescByNameSpec : Specification { - public class StoresByCompanyOrderedDescByNameSpec : Specification - { public StoresByCompanyOrderedDescByNameSpec(int companyId) { - Query.Where(x => x.CompanyId == companyId) - .OrderByDescending(x => x.Name); + Query.Where(x => x.CompanyId == companyId) + .OrderByDescending(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByDescIdSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByDescIdSpec.cs index 5e7e2581..6839be4d 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByDescIdSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByDescIdSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresByCompanyOrderedDescByNameThenByDescIdSpec : Specification { - public class StoresByCompanyOrderedDescByNameThenByDescIdSpec : Specification - { public StoresByCompanyOrderedDescByNameThenByDescIdSpec(int companyId) { - Query.Where(x => x.CompanyId == companyId) - .OrderByDescending(x => x.Name) - .ThenByDescending(x => x.Id); + Query.Where(x => x.CompanyId == companyId) + .OrderByDescending(x => x.Name) + .ThenByDescending(x => x.Id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByIdSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByIdSpec.cs index fbf6f944..d80d5ee7 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByIdSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyOrderedDescByNameThenByIdSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresByCompanyOrderedDescByNameThenByIdSpec : Specification { - public class StoresByCompanyOrderedDescByNameThenByIdSpec : Specification - { public StoresByCompanyOrderedDescByNameThenByIdSpec(int companyId) { - Query.Where(x => x.CompanyId == companyId) - .OrderByDescending(x => x.Name) - .ThenBy(x => x.Id); + Query.Where(x => x.CompanyId == companyId) + .OrderByDescending(x => x.Name) + .ThenBy(x => x.Id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedOrderedDescByNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedOrderedDescByNameSpec.cs index 21e171ba..844aa480 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedOrderedDescByNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedOrderedDescByNameSpec.cs @@ -1,19 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; - -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresByCompanyPaginatedOrderedDescByNameSpec : Specification { - public class StoresByCompanyPaginatedOrderedDescByNameSpec : Specification - { public StoresByCompanyPaginatedOrderedDescByNameSpec(int companyId, int skip, int take) { - Query.Where(x => x.CompanyId == companyId) - .Skip(skip) - .Take(take) - .OrderByDescending(x => x.Name); + Query.Where(x => x.CompanyId == companyId) + .Skip(skip) + .Take(take) + .OrderByDescending(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedSpec.cs index a7520152..1fca13cf 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByCompanyPaginatedSpec.cs @@ -1,15 +1,12 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresByCompanyPaginatedSpec : Specification { - public class StoresByCompanyPaginatedSpec : Specification - { public StoresByCompanyPaginatedSpec(int companyId, int skip, int take) { - Query.Where(x => x.CompanyId == companyId) - .OrderBy(x => x.CompanyId) - .Skip(skip) - .Take(take); + Query.Where(x => x.CompanyId == companyId) + .OrderBy(x => x.CompanyId) + .Skip(skip) + .Take(take); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByIdListSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByIdListSpec.cs index 7e389f9b..d7a0432c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByIdListSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresByIdListSpec.cs @@ -1,15 +1,12 @@ using System; using System.Collections.Generic; -using System.Linq; -using Ardalis.Specification.UnitTests.Fixture.Entities; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +namespace Ardalis.Specification.UnitTests.Fixture.Specs; + +public class StoresByIdListSpec : Specification { - public class StoresByIdListSpec : Specification - { - public StoresByIdListSpec(IEnumerable Ids) + public StoresByIdListSpec(IEnumerable ids) { - Query.Where(x => Ids.Contains(x.Id)); + Query.Where(x => ids.Contains(x.Id)); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedDescendingByNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedDescendingByNameSpec.cs index cbd3632e..4c458706 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedDescendingByNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedDescendingByNameSpec.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresOrderedDescendingByNameSpec : Specification { - public class StoresOrderedDescendingByNameSpec : Specification - { public StoresOrderedDescendingByNameSpec() { - Query.OrderByDescending(x => x.Name); + Query.OrderByDescending(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedSpecByName.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedSpecByName.cs index de1d9dcb..f8822376 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedSpecByName.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedSpecByName.cs @@ -1,12 +1,9 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresOrderedSpecByName : Specification { - public class StoresOrderedSpecByName : Specification - { public StoresOrderedSpecByName() { - Query.OrderBy(x => x.Name); + Query.OrderBy(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedTwoChainsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedTwoChainsSpec.cs index 1de74c49..49eefd34 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedTwoChainsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresOrderedTwoChainsSpec.cs @@ -1,13 +1,10 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresOrderedTwoChainsSpec : Specification { - public class StoresOrderedTwoChainsSpec : Specification - { public StoresOrderedTwoChainsSpec() { - Query.OrderBy(x => x.Name) - .OrderBy(x => x.Id); + Query.OrderBy(x => x.Name) + .OrderBy(x => x.Id); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresPaginatedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresPaginatedSpec.cs index 62fb5834..57432b23 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresPaginatedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/Specs/StoresPaginatedSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoresPaginatedSpec : Specification { - public class StoresPaginatedSpec : Specification - { public StoresPaginatedSpec(int skip, int take) { - Query.OrderBy(s => s.Id) - .Skip(skip) - .Take(take); + Query.OrderBy(s => s.Id) + .Skip(skip) + .Take(take); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/CompanyIncludeFilteredStoresSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/CompanyIncludeFilteredStoresSpec.cs index 4a1c614a..e39dd17b 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/CompanyIncludeFilteredStoresSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/CompanyIncludeFilteredStoresSpec.cs @@ -1,17 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class CompanyIncludeFilteredStoresSpec : Specification { - public class CompanyIncludeFilteredStoresSpec : Specification - { public CompanyIncludeFilteredStoresSpec(int id) { - Query.Where(x => x.Id == id) - .Include(x => x.Stores.Where(s => s.Id == 1)); + Query.Where(x => x.Id == id) + .Include(x => x.Stores.Where(s => s.Id == 1)); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressAndProductsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressAndProductsSpec.cs index eeb278b5..54ab0aec 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressAndProductsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressAndProductsSpec.cs @@ -1,16 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeAddressAndProductsSpec : Specification { - public class StoreIncludeAddressAndProductsSpec : Specification - { public StoreIncludeAddressAndProductsSpec() { - Query.Include(x => x.Products) - .Include(x => x!.Address); + Query.Include(x => x.Products) + .Include(x => x!.Address); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressSpec.cs index 7190c595..7f38dcd8 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeAddressSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeAddressSpec : Specification { - public class StoreIncludeAddressSpec : Specification - { public StoreIncludeAddressSpec() { - Query.Include(x => x.Address); + Query.Include(x => x.Address); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyCountryDotSeparatedSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyCountryDotSeparatedSpec.cs index a38ac825..61e3d00c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyCountryDotSeparatedSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyCountryDotSeparatedSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeCompanyCountryDotSeparatedSpec : Specification { - public class StoreIncludeCompanyCountryDotSeparatedSpec : Specification - { public StoreIncludeCompanyCountryDotSeparatedSpec() { - Query.Include(x => x.Company!.Country); + Query.Include(x => x.Company!.Country); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountryAsStringSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountryAsStringSpec.cs index 1d06bb3c..788630a3 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountryAsStringSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountryAsStringSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeCompanyThenCountryAsStringSpec : Specification { - public class StoreIncludeCompanyThenCountryAsStringSpec : Specification - { public StoreIncludeCompanyThenCountryAsStringSpec() { - Query.Include($"{nameof(Company)}.{nameof(Company.Country)}"); + Query.Include($"{nameof(Company)}.{nameof(Company.Country)}"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountrySpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountrySpec.cs index efbadcf0..2dec8e07 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountrySpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenCountrySpec.cs @@ -1,16 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeCompanyThenCountrySpec : Specification { - public class StoreIncludeCompanyThenCountrySpec : Specification - { public StoreIncludeCompanyThenCountrySpec() { - Query.Include(x => x.Company) - .ThenInclude(x => x!.Country); + Query.Include(x => x.Company) + .ThenInclude(x => x!.Country); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenNameSpec.cs index a41f8979..1787a365 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenNameSpec.cs @@ -1,16 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeCompanyThenNameSpec : Specification { - public class StoreIncludeCompanyThenNameSpec : Specification - { public StoreIncludeCompanyThenNameSpec() { - Query.Include(x => x.Company) - .ThenInclude(x => x!.Name); + Query.Include(x => x.Company) + .ThenInclude(x => x!.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenStoresSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenStoresSpec.cs index c23482a6..3488b0f7 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenStoresSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeCompanyThenStoresSpec.cs @@ -1,14 +1,11 @@ -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeCompanyThenStoresSpec : Specification { - public class StoreIncludeCompanyThenStoresSpec : Specification - { public StoreIncludeCompanyThenStoresSpec() { - Query.Include(x => x.Company) - .ThenInclude(x => x!.Stores) - .ThenInclude(x => x.Products); + Query.Include(x => x.Company) + .ThenInclude(x => x!.Stores) + .ThenInclude(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodOfNavigationSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodOfNavigationSpec.cs index fe8f7af6..67dce23c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodOfNavigationSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodOfNavigationSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeMethodOfNavigationSpec : Specification { - public class StoreIncludeMethodOfNavigationSpec : Specification - { public StoreIncludeMethodOfNavigationSpec() { - Query.Include(x => x.Address!.GetSomethingFromAddress()); + Query.Include(x => x.Address!.GetSomethingFromAddress()); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodSpec.cs index 5b7599de..817270af 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeMethodSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeMethodSpec : Specification { - public class StoreIncludeMethodSpec : Specification - { public StoreIncludeMethodSpec() { - Query.Include(x => x.GetSomethingFromStore()); + Query.Include(x => Store.GetSomethingFromStore()); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeNameSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeNameSpec.cs index 9ace7e9e..60dfaf7a 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeNameSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeNameSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeNameSpec : Specification { - public class StoreIncludeNameSpec : Specification - { public StoreIncludeNameSpec() { - Query.Include(x => x.Name); + Query.Include(x => x.Name); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeProductsSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeProductsSpec.cs index 44a235bd..841c889c 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeProductsSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreIncludeProductsSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreIncludeProductsSpec : Specification { - public class StoreIncludeProductsSpec : Specification - { public StoreIncludeProductsSpec() { - Query.Include(x => x.Products); + Query.Include(x => x.Products); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreWithFaultyIncludeSpec.cs b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreWithFaultyIncludeSpec.cs index 35dabccd..51e092d1 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreWithFaultyIncludeSpec.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/Fixture/SpecsForIncludeTests/StoreWithFaultyIncludeSpec.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.Specification.UnitTests.Fixture.Entities; +namespace Ardalis.Specification.UnitTests.Fixture.Specs; -namespace Ardalis.Specification.UnitTests.Fixture.Specs +public class StoreWithFaultyIncludeSpec : Specification { - public class StoreWithFaultyIncludeSpec : Specification - { public StoreWithFaultyIncludeSpec() { - Query.Include(x => x.Id == 1 && x.Name == "Something"); + Query.Include(x => x.Id == 1 && x.Name == "Something"); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/GlobalUsings.cs b/Specification/tests/Ardalis.Specification.UnitTests/GlobalUsings.cs new file mode 100644 index 00000000..9553d65e --- /dev/null +++ b/Specification/tests/Ardalis.Specification.UnitTests/GlobalUsings.cs @@ -0,0 +1,5 @@ +global using Ardalis.Specification.UnitTests.Fixture.Entities; +global using Ardalis.Specification.UnitTests.Fixture.Specs; +global using FluentAssertions; +global using System.Linq; +global using Xunit; diff --git a/Specification/tests/Ardalis.Specification.UnitTests/IncludeExpressionInfoTests.cs b/Specification/tests/Ardalis.Specification.UnitTests/IncludeExpressionInfoTests.cs index b4e930ab..b45ef907 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/IncludeExpressionInfoTests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/IncludeExpressionInfoTests.cs @@ -1,54 +1,50 @@ using System; using System.Linq.Expressions; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using FluentAssertions; -using Xunit; -namespace Ardalis.Specification.UnitTests +namespace Ardalis.Specification.UnitTests; + +public class IncludeExpressionInfoTests { - public class IncludeExpressionInfoTests - { - private readonly Expression> expr; + private readonly Expression> _expr; public IncludeExpressionInfoTests() { - expr = x => x.Country!; + _expr = x => x.Country!; } [Fact] public void ThrowsArgumentNullException_GivenNullForLambdaExpression() { - Action sutAction = () => new IncludeExpressionInfo(null!, typeof(Company), typeof(Country)); + Action sutAction = () => new IncludeExpressionInfo(null!, typeof(Company), typeof(Country)); - sutAction.Should() - .Throw(); + sutAction.Should() + .Throw(); } [Fact] public void ThrowsArgumentNullException_GivenNullForEntityType() { - Action sutAction = () => new IncludeExpressionInfo(expr, null!, typeof(Country)); + Action sutAction = () => new IncludeExpressionInfo(_expr, null!, typeof(Country)); - sutAction.Should() - .Throw(); + sutAction.Should() + .Throw(); } [Fact] public void ThrowsArgumentNullException_GivenNullForPropertyType() { - Action sutAction = () => new IncludeExpressionInfo(expr, typeof(Company), null!); + Action sutAction = () => new IncludeExpressionInfo(_expr, typeof(Company), null!); - sutAction.Should() - .Throw(); + sutAction.Should() + .Throw(); } [Fact] public void ThrowsArgumentNullException_GivenNullForPreviousPropertyType() { - Action sutAction = () => new IncludeExpressionInfo(expr, typeof(Company), typeof(Country), null!); + Action sutAction = () => new IncludeExpressionInfo(_expr, typeof(Company), typeof(Country), null!); - sutAction.Should() - .Throw(); + sutAction.Should() + .Throw(); } - } } diff --git a/Specification/tests/Ardalis.Specification.UnitTests/ValidatorTests/SpecificationValidator_Tests.cs b/Specification/tests/Ardalis.Specification.UnitTests/ValidatorTests/SpecificationValidator_Tests.cs index e9d4b309..4f6baadd 100644 --- a/Specification/tests/Ardalis.Specification.UnitTests/ValidatorTests/SpecificationValidator_Tests.cs +++ b/Specification/tests/Ardalis.Specification.UnitTests/ValidatorTests/SpecificationValidator_Tests.cs @@ -1,98 +1,89 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Ardalis.Specification.UnitTests.Fixture.Entities; -using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; -using Ardalis.Specification.UnitTests.Fixture.Specs; -using FluentAssertions; - -namespace Ardalis.Specification.UnitTests.ValidatorTests +using Ardalis.Specification.UnitTests.Fixture.Entities.Seeds; + +namespace Ardalis.Specification.UnitTests.ValidatorTests; + +public class SpecificationValidator_Tests { - public class SpecificationValidator_Tests - { - Store store = StoreSeed.Get().Single(x => x.Id == StoreSeed.VALID_Search_ID); + private readonly Store _store = StoreSeed.Get().Single(x => x.Id == StoreSeed.VALID_Search_ID); public void ReturnsTrue_GivenStoreByIdSearchByNameAndCitySpec_WithValidValues() { - var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Name_Key); + var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Name_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeTrue(); + result.Should().BeTrue(); } public void ReturnsFalse_GivenStoreByIdSearchByNameAndCitySpec_WithInvalidId() { - var spec = new StoreByIdSearchByNameAndCitySpec(1, StoreSeed.VALID_Search_City_Name_Key); + var spec = new StoreByIdSearchByNameAndCitySpec(1, StoreSeed.VALID_Search_City_Name_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeFalse(); + result.Should().BeFalse(); } public void ReturnsFalse_GivenStoreByIdSearchByNameAndCitySpec_WithInvalidNameSearchString() { - var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Key); + var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeFalse(); + result.Should().BeFalse(); } public void ReturnsFalse_GivenStoreByIdSearchByNameAndCitySpec_WithInvalidCitySearchString() { - var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_Name_Key); + var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_Name_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeFalse(); + result.Should().BeFalse(); } public void ReturnsFalse_StoreByIdSearchByNameAndCitySpecSpec_WithInvalidCityAndNameSearchString() { - var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, "random"); + var spec = new StoreByIdSearchByNameAndCitySpec(StoreSeed.VALID_Search_ID, "random"); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeFalse(); + result.Should().BeFalse(); } public void ReturnsTrue_StoreByIdSearchByNameOrCitySpecSpec_WithValidValues() { - var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Name_Key); + var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Name_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeTrue(); + result.Should().BeTrue(); } public void ReturnsTrue_StoreByIdSearchByNameOrCitySpecSpec_WithInvalidNameSearchString() { - var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Key); + var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_City_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeTrue(); + result.Should().BeTrue(); } public void ReturnsTrue_StoreByIdSearchByNameOrCitySpecSpec_WithInvalidCitySearchString() { - var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_Name_Key); + var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, StoreSeed.VALID_Search_Name_Key); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeTrue(); + result.Should().BeTrue(); } public void ReturnsFalse_StoreByIdSearchByNameOrCitySpecSpec_WithInvalidCityAndNameSearchString() { - var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, "random"); + var spec = new StoreByIdSearchByNameOrCitySpec(StoreSeed.VALID_Search_ID, "random"); - var result = spec.IsSatisfiedBy(store); + var result = spec.IsSatisfiedBy(_store); - result.Should().BeFalse(); + result.Should().BeFalse(); } - } } diff --git a/docker-compose.yml b/docker-compose.yml index e8d222de..90f2e300 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,7 +13,7 @@ services: - databaseEFCore databaseEFCore: - image: mcr.microsoft.com/mssql/server:2019-CU3-ubuntu-18.04 + image: mcr.microsoft.com/mssql/server:2022-latest environment: SA_PASSWORD: "P@ssW0rd!" ACCEPT_EULA: "Y" diff --git a/sample/Ardalis.SampleApp.Core/Ardalis.SampleApp.Core.csproj b/sample/Ardalis.SampleApp.Core/Ardalis.SampleApp.Core.csproj index 9edec766..ca9121ea 100644 --- a/sample/Ardalis.SampleApp.Core/Ardalis.SampleApp.Core.csproj +++ b/sample/Ardalis.SampleApp.Core/Ardalis.SampleApp.Core.csproj @@ -2,6 +2,7 @@ net6.0 + 11.0 diff --git a/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Customer.cs b/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Customer.cs index 0607c5a3..82f2ef97 100644 --- a/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Customer.cs +++ b/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Customer.cs @@ -1,55 +1,54 @@ -using System.Collections.Generic; -using System.Linq; -using Ardalis.GuardClauses; +using Ardalis.GuardClauses; using Ardalis.SampleApp.Core.Interfaces; +using System.Collections.Generic; +using System.Linq; + +namespace Ardalis.SampleApp.Core.Entities.CustomerAggregate; -namespace Ardalis.SampleApp.Core.Entities.CustomerAggregate +public class Customer : IAggregateRoot { - public class Customer : IAggregateRoot - { public int Id { get; private set; } public string Name { get; private set; } public string Email { get; private set; } public string Address { get; private set; } public IEnumerable Stores => _stores.AsEnumerable(); - private readonly List _stores = new List(); + private readonly List _stores = new(); public Customer(string name, string email, string address) { - Guard.Against.NullOrEmpty(name, nameof(name)); - Guard.Against.NullOrEmpty(email, nameof(email)); + Guard.Against.NullOrEmpty(name, nameof(name)); + Guard.Against.NullOrEmpty(email, nameof(email)); - this.Name = name; - this.Email = email; - this.Address = address; + Name = name; + Email = email; + Address = address; } public Store GetStore(int storeId) { - var store = Stores.FirstOrDefault(x => x.Id == storeId); + var store = Stores.FirstOrDefault(x => x.Id == storeId); - Guard.Against.Null(store, nameof(store)); + Guard.Against.Null(store, nameof(store)); - return store; + return store; } public Store AddStore(Store store) { - Guard.Against.Null(store, nameof(store)); + Guard.Against.Null(store, nameof(store)); - // Do some other operation while adding it. + // Do some other operation while adding it. - _stores.Add(store); + _stores.Add(store); - return store; + return store; } public void DeleteStore(int storeID) { - var store = GetStore(storeID); + var store = GetStore(storeID); - _stores.Remove(store); + _stores.Remove(store); } - } } diff --git a/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Store.cs b/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Store.cs index 7c2a7ee2..d23ce684 100644 --- a/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Store.cs +++ b/sample/Ardalis.SampleApp.Core/Entities/CustomerAggregate/Store.cs @@ -1,9 +1,9 @@ using Ardalis.GuardClauses; -namespace Ardalis.SampleApp.Core.Entities.CustomerAggregate +namespace Ardalis.SampleApp.Core.Entities.CustomerAggregate; + +public class Store { - public class Store - { public int Id { get; private set; } public string Name { get; private set; } public string Address { get; private set; } @@ -12,10 +12,9 @@ public class Store public Store(string name, string address) { - Guard.Against.NullOrEmpty(name, nameof(name)); + Guard.Against.NullOrEmpty(name, nameof(name)); - this.Name = name; - this.Address = address; + this.Name = name; + this.Address = address; } - } } diff --git a/sample/Ardalis.SampleApp.Core/Entities/Seeds/CustomerSeed.cs b/sample/Ardalis.SampleApp.Core/Entities/Seeds/CustomerSeed.cs index 6370e33a..52186909 100644 --- a/sample/Ardalis.SampleApp.Core/Entities/Seeds/CustomerSeed.cs +++ b/sample/Ardalis.SampleApp.Core/Entities/Seeds/CustomerSeed.cs @@ -1,24 +1,23 @@ -using System.Collections.Generic; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using System.Collections.Generic; -namespace Ardalis.SampleApp.Core.Entities.Seeds +namespace Ardalis.SampleApp.Core.Entities.Seeds; + +public static class CustomerSeed { - public static class CustomerSeed - { public static List Seed() { - List output = new List(); + List output = new List(); - for (int i = 1; i <= 100000; i++) - { - var customer = new Customer($"Customer{i}", $"Email{i}@local", $"Customer{i} address"); - customer.AddStore(new Store($"Store{i}-1", $"Store{i}-1 address")); - customer.AddStore(new Store($"Store{i}-2", $"Store{i}-2 address")); + for (int i = 1; i <= 100000; i++) + { + var customer = new Customer($"Customer{i}", $"Email{i}@local", $"Customer{i} address"); + customer.AddStore(new Store($"Store{i}-1", $"Store{i}-1 address")); + customer.AddStore(new Store($"Store{i}-2", $"Store{i}-2 address")); - output.Add(customer); - } + output.Add(customer); + } - return output; + return output; } - } } diff --git a/sample/Ardalis.SampleApp.Core/Interfaces/IAggregateRoot.cs b/sample/Ardalis.SampleApp.Core/Interfaces/IAggregateRoot.cs index 0b98eb7b..13cf2d12 100644 --- a/sample/Ardalis.SampleApp.Core/Interfaces/IAggregateRoot.cs +++ b/sample/Ardalis.SampleApp.Core/Interfaces/IAggregateRoot.cs @@ -1,6 +1,5 @@ -namespace Ardalis.SampleApp.Core.Interfaces +namespace Ardalis.SampleApp.Core.Interfaces; + +public interface IAggregateRoot { - public interface IAggregateRoot - { - } } diff --git a/sample/Ardalis.SampleApp.Core/Interfaces/ICustomerRepository.cs b/sample/Ardalis.SampleApp.Core/Interfaces/ICustomerRepository.cs index fb77ec20..41f00172 100644 --- a/sample/Ardalis.SampleApp.Core/Interfaces/ICustomerRepository.cs +++ b/sample/Ardalis.SampleApp.Core/Interfaces/ICustomerRepository.cs @@ -1,12 +1,11 @@ -using System.Collections.Generic; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using System.Collections.Generic; using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; -namespace Ardalis.SampleApp.Core.Interfaces +namespace Ardalis.SampleApp.Core.Interfaces; + +public interface ICustomerRepository { - public interface ICustomerRepository - { // This is just to demonstrate that at anytime you can create custom repositories, and use to create some complex queries working directly with EF or your ORM. Task> GetCustomers(string addressSearchTerm); - } } diff --git a/sample/Ardalis.SampleApp.Core/Interfaces/IRepository.cs b/sample/Ardalis.SampleApp.Core/Interfaces/IRepository.cs index 27b1013d..1e6aa7ba 100644 --- a/sample/Ardalis.SampleApp.Core/Interfaces/IRepository.cs +++ b/sample/Ardalis.SampleApp.Core/Interfaces/IRepository.cs @@ -1,14 +1,13 @@ using Ardalis.Specification; -namespace Ardalis.SampleApp.Core.Interfaces +namespace Ardalis.SampleApp.Core.Interfaces; + +/// +public interface IRepository : IRepositoryBase where T : class, IAggregateRoot { - /// - public interface IRepository : IRepositoryBase where T : class, IAggregateRoot - { - } +} - /// - public interface IReadRepository : IReadRepositoryBase where T : class, IAggregateRoot - { - } +/// +public interface IReadRepository : IReadRepositoryBase where T : class, IAggregateRoot +{ } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameSpec.cs b/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameSpec.cs index 5f255d0f..f91178ae 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameSpec.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameSpec.cs @@ -1,18 +1,17 @@ using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.Specification; -namespace Ardalis.SampleApp.Core.Specifications +namespace Ardalis.SampleApp.Core.Specifications; + +/// +/// This specification expects customer names to be unique - change the base type if you want to support multiple results +/// +public class CustomerByNameSpec : SingleResultSpecification { - /// - /// This specification expects customer names to be unique - change the base type if you want to support multiple results - /// - public class CustomerByNameSpec : SingleResultSpecification - { public CustomerByNameSpec(string name) { - Query.Where(x => x.Name == name) - .OrderBy(x => x.Name) - .ThenByDescending(x => x.Address); + Query.Where(x => x.Name == name) + .OrderBy(x => x.Name) + .ThenByDescending(x => x.Address); } - } } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameWithStoresSpec.cs b/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameWithStoresSpec.cs index 844eb23c..a6e6f80f 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameWithStoresSpec.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/CustomerByNameWithStoresSpec.cs @@ -1,18 +1,17 @@ using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.Specification; -namespace Ardalis.SampleApp.Core.Specifications +namespace Ardalis.SampleApp.Core.Specifications; + +/// +/// This specification expects customer names to be unique - change the base type if you want to support multiple results +/// +public class CustomerByNameWithStoresSpec : SingleResultSpecification { - /// - /// This specification expects customer names to be unique - change the base type if you want to support multiple results - /// - public class CustomerByNameWithStoresSpec : SingleResultSpecification - { public CustomerByNameWithStoresSpec(string name) { - Query.Where(x => x.Name == name) - .Include(x => x.Stores) - .EnableCache(nameof(CustomerByNameWithStoresSpec), name); + Query.Where(x => x.Name == name) + .Include(x => x.Stores) + .EnableCache(nameof(CustomerByNameWithStoresSpec), name); } - } } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/CustomerSpec.cs b/sample/Ardalis.SampleApp.Core/Specifications/CustomerSpec.cs index d6184c55..97964b1a 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/CustomerSpec.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/CustomerSpec.cs @@ -2,33 +2,32 @@ using Ardalis.SampleApp.Core.Specifications.Filters; using Ardalis.Specification; -namespace Ardalis.SampleApp.Core.Specifications +namespace Ardalis.SampleApp.Core.Specifications; + +/// +/// This specification expects 0 to many results +/// +public class CustomerSpec : Specification { - /// - /// This specification expects 0 to many results - /// - public class CustomerSpec : Specification - { public CustomerSpec(CustomerFilter filter) { - Query.OrderBy(x => x.Name) - .ThenByDescending(x => x.Address); + Query.OrderBy(x => x.Name) + .ThenByDescending(x => x.Address); - if (filter.LoadChildren) - Query.Include(x => x.Stores); + if (filter.LoadChildren) + Query.Include(x => x.Stores); - if (filter.IsPagingEnabled) - Query.Skip(PaginationHelper.CalculateSkip(filter)) - .Take(PaginationHelper.CalculateTake(filter)); + if (filter.IsPagingEnabled) + Query.Skip(PaginationHelper.CalculateSkip(filter)) + .Take(PaginationHelper.CalculateTake(filter)); - if (!string.IsNullOrEmpty(filter.Name)) - Query.Where(x => x.Name == filter.Name); + if (!string.IsNullOrEmpty(filter.Name)) + Query.Where(x => x.Name == filter.Name); - if (!string.IsNullOrEmpty(filter.Email)) - Query.Where(x => x.Email == filter.Email); + if (!string.IsNullOrEmpty(filter.Email)) + Query.Where(x => x.Email == filter.Email); - if (!string.IsNullOrEmpty(filter.Address)) - Query.Search(x => x.Address, "%" + filter.Address + "%"); + if (!string.IsNullOrEmpty(filter.Address)) + Query.Search(x => x.Address, "%" + filter.Address + "%"); } - } } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/Filters/BaseFilter.cs b/sample/Ardalis.SampleApp.Core/Specifications/Filters/BaseFilter.cs index 5cbae747..c767a762 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/Filters/BaseFilter.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/Filters/BaseFilter.cs @@ -1,15 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.SampleApp.Core.Specifications.Filters; -namespace Ardalis.SampleApp.Core.Specifications.Filters +public class BaseFilter { - public class BaseFilter - { public bool LoadChildren { get; set; } public bool IsPagingEnabled { get; set; } public int Page { get; set; } public int PageSize { get; set; } - } } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/Filters/CustomerFilter.cs b/sample/Ardalis.SampleApp.Core/Specifications/Filters/CustomerFilter.cs index 10c4a13d..ae5b0d03 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/Filters/CustomerFilter.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/Filters/CustomerFilter.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Text; +namespace Ardalis.SampleApp.Core.Specifications.Filters; -namespace Ardalis.SampleApp.Core.Specifications.Filters +public class CustomerFilter : BaseFilter { - public class CustomerFilter : BaseFilter - { public string Name { get; set; } public string Email { get; set; } public string Address { get; set; } - } } diff --git a/sample/Ardalis.SampleApp.Core/Specifications/PaginationHelper.cs b/sample/Ardalis.SampleApp.Core/Specifications/PaginationHelper.cs index 19dac0e9..a8b81b92 100644 --- a/sample/Ardalis.SampleApp.Core/Specifications/PaginationHelper.cs +++ b/sample/Ardalis.SampleApp.Core/Specifications/PaginationHelper.cs @@ -1,30 +1,29 @@ using Ardalis.SampleApp.Core.Specifications.Filters; -namespace Ardalis.SampleApp.Core.Specifications +namespace Ardalis.SampleApp.Core.Specifications; + +public static class PaginationHelper { - public static class PaginationHelper - { public static int DefaultPage => 1; public static int DefaultPageSize => 10; public static int CalculateTake(int pageSize) { - return pageSize <= 0 ? DefaultPageSize : pageSize; + return pageSize <= 0 ? DefaultPageSize : pageSize; } public static int CalculateSkip(int pageSize, int page) { - page = page <= 0 ? DefaultPage : page; + page = page <= 0 ? DefaultPage : page; - return CalculateTake(pageSize) * (page - 1); + return CalculateTake(pageSize) * (page - 1); } public static int CalculateTake(BaseFilter baseFilter) { - return CalculateTake(baseFilter.PageSize); + return CalculateTake(baseFilter.PageSize); } public static int CalculateSkip(BaseFilter baseFilter) { - return CalculateSkip(baseFilter.PageSize, baseFilter.Page); + return CalculateSkip(baseFilter.PageSize, baseFilter.Page); } - } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/Ardalis.SampleApp.Infrastructure.csproj b/sample/Ardalis.SampleApp.Infrastructure/Ardalis.SampleApp.Infrastructure.csproj index 14b55fac..7bf8883f 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/Ardalis.SampleApp.Infrastructure.csproj +++ b/sample/Ardalis.SampleApp.Infrastructure/Ardalis.SampleApp.Infrastructure.csproj @@ -2,6 +2,7 @@ net6.0 + 11.0 diff --git a/sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs b/sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs index 7775098d..2d2339a2 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/Data/CachedCustomerRepository.cs @@ -1,158 +1,158 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Interfaces; +using Ardalis.SampleApp.Core.Interfaces; using Ardalis.Specification; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Infrastructure.Data; /// public class CachedRepository : IReadRepository where T : class, IAggregateRoot { - private readonly IMemoryCache _cache; - private readonly ILogger> _logger; - private readonly MyRepository _sourceRepository; - private MemoryCacheEntryOptions _cacheOptions; - - public CachedRepository(IMemoryCache cache, - ILogger> logger, - MyRepository sourceRepository) - { - _cache = cache; - _logger = logger; - _sourceRepository = sourceRepository; - - _cacheOptions = new MemoryCacheEntryOptions() - .SetAbsoluteExpiration(relative: TimeSpan.FromSeconds(10)); - } - - /// - public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specification) - { - return _sourceRepository.AsAsyncEnumerable(specification); - } - - /// - public Task AnyAsync(Specification.ISpecification specification, CancellationToken cancellationToken = default) - { - // TODO: Add Caching - return _sourceRepository.AnyAsync(specification, cancellationToken); - } - - /// - public Task AnyAsync(CancellationToken cancellationToken = default) - { - // TODO: Add Caching - return _sourceRepository.AnyAsync(cancellationToken); - } - - /// - public Task CountAsync(Specification.ISpecification specification, CancellationToken cancellationToken = default) - { - // TODO: Add Caching - return _sourceRepository.CountAsync(specification, cancellationToken); - } - - /// - public Task CountAsync(CancellationToken cancellationToken = default) - { - // TODO: Add Caching - return _sourceRepository.CountAsync(cancellationToken); - } - - /// - public Task GetByIdAsync(int id, CancellationToken cancellationToken = default) - { - return _sourceRepository.GetByIdAsync(id, cancellationToken); - } - - /// - public Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) - { - return _sourceRepository.GetByIdAsync(id, cancellationToken); - } - - /// - public Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - if (specification.CacheEnabled) - { - string key = $"{specification.CacheKey}-GetBySpecAsync"; - _logger.LogInformation("Checking cache for " + key); - return _cache.GetOrCreate(key, entry => - { - entry.SetOptions(_cacheOptions); - _logger.LogWarning("Fetching source data for " + key); + private readonly IMemoryCache _cache; + private readonly ILogger> _logger; + private readonly MyRepository _sourceRepository; + private readonly MemoryCacheEntryOptions _cacheOptions; + + public CachedRepository(IMemoryCache cache, + ILogger> logger, + MyRepository sourceRepository) + { + _cache = cache; + _logger = logger; + _sourceRepository = sourceRepository; + + _cacheOptions = new MemoryCacheEntryOptions() + .SetAbsoluteExpiration(relative: TimeSpan.FromSeconds(10)); + } + + /// + public virtual IAsyncEnumerable AsAsyncEnumerable(ISpecification specification) + { + return _sourceRepository.AsAsyncEnumerable(specification); + } + + /// + public Task AnyAsync(Specification.ISpecification specification, CancellationToken cancellationToken = default) + { + // TODO: Add Caching + return _sourceRepository.AnyAsync(specification, cancellationToken); + } + + /// + public Task AnyAsync(CancellationToken cancellationToken = default) + { + // TODO: Add Caching + return _sourceRepository.AnyAsync(cancellationToken); + } + + /// + public Task CountAsync(Specification.ISpecification specification, CancellationToken cancellationToken = default) + { + // TODO: Add Caching + return _sourceRepository.CountAsync(specification, cancellationToken); + } + + /// + public Task CountAsync(CancellationToken cancellationToken = default) + { + // TODO: Add Caching + return _sourceRepository.CountAsync(cancellationToken); + } + + /// + public Task GetByIdAsync(int id, CancellationToken cancellationToken = default) + { + return _sourceRepository.GetByIdAsync(id, cancellationToken); + } + + /// + public Task GetByIdAsync(TId id, CancellationToken cancellationToken = default) + { + return _sourceRepository.GetByIdAsync(id, cancellationToken); + } + + /// + public Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + if (specification.CacheEnabled) + { + var key = $"{specification.CacheKey}-GetBySpecAsync"; + _logger.LogInformation("Checking cache for {cache_key}", key); + return _cache.GetOrCreate(key, entry => + { + entry.SetOptions(_cacheOptions); + _logger.LogWarning("Fetching source data for {cache_key}", key); + return _sourceRepository.FirstOrDefaultAsync(specification, cancellationToken); + }); + } return _sourceRepository.FirstOrDefaultAsync(specification, cancellationToken); - }); - } - return _sourceRepository.FirstOrDefaultAsync(specification, cancellationToken); - } - - /// - public Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - /// - public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - return await _sourceRepository.FirstOrDefaultAsync(specification, cancellationToken); - } - - /// - public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) - { - return await _sourceRepository.SingleOrDefaultAsync(specification, cancellationToken); - } - - /// - public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) - { - return await _sourceRepository.SingleOrDefaultAsync(specification, cancellationToken); - } - - /// - public Task> ListAsync(CancellationToken cancellationToken = default) - { - string key = $"{nameof(T)}-ListAsync"; - return _cache.GetOrCreate(key, entry => - { - entry.SetOptions(_cacheOptions); - return _sourceRepository.ListAsync(cancellationToken); - }); - } - - /// - public Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - if (specification.CacheEnabled) - { - string key = $"{specification.CacheKey}-ListAsync"; - _logger.LogInformation("Checking cache for " + key); - return _cache.GetOrCreate(key, entry => - { - entry.SetOptions(_cacheOptions); - _logger.LogWarning("Fetching source data for " + key); + } + + /// + public Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + /// + public virtual async Task FirstOrDefaultAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + return await _sourceRepository.FirstOrDefaultAsync(specification, cancellationToken); + } + + /// + public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) + { + return await _sourceRepository.SingleOrDefaultAsync(specification, cancellationToken); + } + + /// + public virtual async Task SingleOrDefaultAsync(ISingleResultSpecification specification, CancellationToken cancellationToken = default) + { + return await _sourceRepository.SingleOrDefaultAsync(specification, cancellationToken); + } + + /// + public Task> ListAsync(CancellationToken cancellationToken = default) + { + var key = $"{nameof(T)}-ListAsync"; + return _cache.GetOrCreate(key, entry => + { + entry.SetOptions(_cacheOptions); + return _sourceRepository.ListAsync(cancellationToken); + }); + } + + /// + public Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + if (specification.CacheEnabled) + { + var key = $"{specification.CacheKey}-ListAsync"; + _logger.LogInformation("Checking cache for {cache_key}", key); + return _cache.GetOrCreate(key, entry => + { + entry.SetOptions(_cacheOptions); + _logger.LogWarning("Fetching source data for {cache_key}", key); + return _sourceRepository.ListAsync(specification, cancellationToken); + }); + } return _sourceRepository.ListAsync(specification, cancellationToken); - }); - } - return _sourceRepository.ListAsync(specification, cancellationToken); - } - - /// - public Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } - - /// - public Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) - { - throw new NotImplementedException(); - } + } + + /// + public Task> ListAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + /// + public Task GetBySpecAsync(ISpecification specification, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/Data/CustomerRepository.cs b/sample/Ardalis.SampleApp.Infrastructure/Data/CustomerRepository.cs index e30926ee..d604f10d 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/Data/CustomerRepository.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/Data/CustomerRepository.cs @@ -1,28 +1,28 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.SampleApp.Core.Interfaces; using Ardalis.SampleApp.Infrastructure.DataAccess; using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Infrastructure.Data; // This is just to demonstrate that at anytime you can create custom repositories, and use to create some complex queries working directly with EF or your ORM. public class CustomerRepository : ICustomerRepository { - private readonly SampleDbContext dbContext; + private readonly SampleDbContext dbContext; - public CustomerRepository(SampleDbContext dbContext) - { - this.dbContext = dbContext; - } + public CustomerRepository(SampleDbContext dbContext) + { + this.dbContext = dbContext; + } - public Task> GetCustomers(string addressSearchTerm) - { - return dbContext.Customers - .Take(10) - .Where(x => EF.Functions.Like(x.Address, "%" + addressSearchTerm + "%")) - .ToListAsync(); - } + public Task> GetCustomers(string addressSearchTerm) + { + return dbContext.Customers + .Take(10) + .Where(x => EF.Functions.Like(x.Address, "%" + addressSearchTerm + "%")) + .ToListAsync(); + } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/Data/MyRepository.cs b/sample/Ardalis.SampleApp.Infrastructure/Data/MyRepository.cs index c4a26eba..f60f8279 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/Data/MyRepository.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/Data/MyRepository.cs @@ -7,12 +7,12 @@ namespace Ardalis.SampleApp.Infrastructure.Data; /// public class MyRepository : RepositoryBase, IRepository where T : class, IAggregateRoot { - private readonly SampleDbContext dbContext; + private readonly SampleDbContext dbContext; - public MyRepository(SampleDbContext dbContext) : base(dbContext) - { - this.dbContext = dbContext; - } + public MyRepository(SampleDbContext dbContext) : base(dbContext) + { + this.dbContext = dbContext; + } - // Not required to implement anything. Add additional functionalities if required. + // Not required to implement anything. Add additional functionalities if required. } diff --git a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/CustomerConfiguration.cs b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/CustomerConfiguration.cs index 7ccd7c8b..828fc518 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/CustomerConfiguration.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/CustomerConfiguration.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -9,13 +6,13 @@ namespace Ardalis.SampleApp.Infrastructure.DataAccess.Configurations; public class CustomerConfiguration : IEntityTypeConfiguration { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable(nameof(Customer)); + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable(nameof(Customer)); - builder.Metadata.FindNavigation(nameof(Customer.Stores)) - .SetPropertyAccessMode(PropertyAccessMode.Field); + builder.Metadata.FindNavigation(nameof(Customer.Stores)) + .SetPropertyAccessMode(PropertyAccessMode.Field); - builder.HasKey(x => x.Id); - } + builder.HasKey(x => x.Id); + } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/StoreConfiguration.cs b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/StoreConfiguration.cs index d847a1f2..380782e8 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/StoreConfiguration.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/Configurations/StoreConfiguration.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -9,10 +6,10 @@ namespace Ardalis.SampleApp.Infrastructure.DataAccess.Configurations; public class StoreConfiguration : IEntityTypeConfiguration { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable(nameof(Store)); + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable(nameof(Store)); - builder.HasKey(x => x.Id); - } + builder.HasKey(x => x.Id); + } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContext.cs b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContext.cs index 7333d199..c7d98f9a 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContext.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContext.cs @@ -6,18 +6,18 @@ namespace Ardalis.SampleApp.Infrastructure.DataAccess; public class SampleDbContext : DbContext { - public DbSet Customers { get; set; } + public DbSet Customers { get; set; } - public SampleDbContext(DbContextOptions options) - : base(options) - { - } + public SampleDbContext(DbContextOptions options) + : base(options) + { + } - protected override void OnModelCreating(ModelBuilder builder) - { - base.OnModelCreating(builder); + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); - builder.ApplyConfiguration(new CustomerConfiguration()); - builder.ApplyConfiguration(new StoreConfiguration()); - } + builder.ApplyConfiguration(new CustomerConfiguration()); + builder.ApplyConfiguration(new StoreConfiguration()); + } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContextSeed.cs b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContextSeed.cs index ed0f400d..5ecbfd68 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContextSeed.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/DataAccess/SampleDbContextSeed.cs @@ -1,44 +1,42 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Entities.Seeds; +using Ardalis.SampleApp.Core.Entities.Seeds; using Microsoft.EntityFrameworkCore; +using System; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Infrastructure.DataAccess; public class SampleDbContextSeed { - private readonly SampleDbContext dbContext; - - public SampleDbContextSeed(SampleDbContext dbContext) - { - this.dbContext = dbContext; - } + private readonly SampleDbContext dbContext; - public async Task SeedAsync(int retry = 0) - { - try + public SampleDbContextSeed(SampleDbContext dbContext) { - dbContext.Database.Migrate(); + this.dbContext = dbContext; + } - if (await dbContext.Customers.CountAsync() == 0) - { - foreach (var customer in CustomerSeed.Seed()) + public async Task SeedAsync(int retry = 0) + { + try { - dbContext.Customers.Add(customer); - } - } + dbContext.Database.Migrate(); - await dbContext.SaveChangesAsync(); + if (await dbContext.Customers.CountAsync() == 0) + { + foreach (var customer in CustomerSeed.Seed()) + { + dbContext.Customers.Add(customer); + } + } + await dbContext.SaveChangesAsync(); + + } + catch (Exception) + { + if (retry > 0) + { + await SeedAsync(retry - 1); + } + } } - catch (Exception) - { - if (retry > 0) - { - await SeedAsync(retry - 1); - } - } - } } diff --git a/sample/Ardalis.SampleApp.Infrastructure/Migrations/20201102224526_SampleDB-v1.cs b/sample/Ardalis.SampleApp.Infrastructure/Migrations/20201102224526_SampleDB-v1.cs index 905c6a77..637c74bd 100644 --- a/sample/Ardalis.SampleApp.Infrastructure/Migrations/20201102224526_SampleDB-v1.cs +++ b/sample/Ardalis.SampleApp.Infrastructure/Migrations/20201102224526_SampleDB-v1.cs @@ -4,56 +4,56 @@ namespace Ardalis.SampleApp.Infrastructure.Migrations; public partial class SampleDBv1 : Migration { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Customer", - columns: table => new - { - Id = table.Column(nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Name = table.Column(nullable: true), - Email = table.Column(nullable: true), - Address = table.Column(nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Customer", x => x.Id); - }); + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Customer", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(nullable: true), + Email = table.Column(nullable: true), + Address = table.Column(nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Customer", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "Store", - columns: table => new - { - Id = table.Column(nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - Name = table.Column(nullable: true), - Address = table.Column(nullable: true), - CustomerId = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Store", x => x.Id); - table.ForeignKey( - name: "FK_Store_Customer_CustomerId", - column: x => x.CustomerId, - principalTable: "Customer", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); + migrationBuilder.CreateTable( + name: "Store", + columns: table => new + { + Id = table.Column(nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(nullable: true), + Address = table.Column(nullable: true), + CustomerId = table.Column(nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Store", x => x.Id); + table.ForeignKey( + name: "FK_Store_Customer_CustomerId", + column: x => x.CustomerId, + principalTable: "Customer", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); - migrationBuilder.CreateIndex( - name: "IX_Store_CustomerId", - table: "Store", - column: "CustomerId"); - } + migrationBuilder.CreateIndex( + name: "IX_Store_CustomerId", + table: "Store", + column: "CustomerId"); + } - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Store"); + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Store"); - migrationBuilder.DropTable( - name: "Customer"); - } + migrationBuilder.DropTable( + name: "Customer"); + } } diff --git a/sample/Ardalis.SampleApp.Web/Ardalis.SampleApp.Web.csproj b/sample/Ardalis.SampleApp.Web/Ardalis.SampleApp.Web.csproj index bf4fb1db..ecd94194 100644 --- a/sample/Ardalis.SampleApp.Web/Ardalis.SampleApp.Web.csproj +++ b/sample/Ardalis.SampleApp.Web/Ardalis.SampleApp.Web.csproj @@ -1,7 +1,8 @@ - + net6.0 + 11.0 @@ -20,4 +21,9 @@ + + + 1701;1702;1591;1573;0612 + + diff --git a/sample/Ardalis.SampleApp.Web/AutomapperMaps.cs b/sample/Ardalis.SampleApp.Web/AutomapperMaps.cs index 28f86310..a1fdc69f 100644 --- a/sample/Ardalis.SampleApp.Web/AutomapperMaps.cs +++ b/sample/Ardalis.SampleApp.Web/AutomapperMaps.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.SampleApp.Core.Specifications.Filters; using Ardalis.SampleApp.Web.Models; using AutoMapper; @@ -12,12 +8,12 @@ namespace Ardalis.SampleApp.Web; public class AutomapperMaps : Profile { - public AutomapperMaps() - { - CreateMap().IncludeAllDerived().ReverseMap(); - CreateMap().ReverseMap(); + public AutomapperMaps() + { + CreateMap().IncludeAllDerived().ReverseMap(); + CreateMap().ReverseMap(); - CreateMap(); - CreateMap(); - } + CreateMap(); + CreateMap(); + } } diff --git a/sample/Ardalis.SampleApp.Web/Controllers/CustomersController.cs b/sample/Ardalis.SampleApp.Web/Controllers/CustomersController.cs index 967a480d..47162c72 100644 --- a/sample/Ardalis.SampleApp.Web/Controllers/CustomersController.cs +++ b/sample/Ardalis.SampleApp.Web/Controllers/CustomersController.cs @@ -1,8 +1,8 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Ardalis.SampleApp.Web.Interfaces; +using Ardalis.SampleApp.Web.Interfaces; using Ardalis.SampleApp.Web.Models; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Web.Controllers; @@ -10,35 +10,35 @@ namespace Ardalis.SampleApp.Web.Controllers; [Route("[controller]")] public class CustomersController : ControllerBase { - private readonly ICustomerUiService customerUiService; + private readonly ICustomerUiService customerUiService; - public CustomersController(ICustomerUiService customerUiService) - { - this.customerUiService = customerUiService; - } + public CustomersController(ICustomerUiService customerUiService) + { + this.customerUiService = customerUiService; + } - [HttpGet("{Id}")] - public Task Get(int Id) - { - return customerUiService.GetCustomer(Id); - } + [HttpGet("{Id}")] + public Task Get(int Id) + { + return customerUiService.GetCustomer(Id); + } - [HttpGet("{name}")] - public Task Get(string name) - { - return customerUiService.GetCustomer(name); - } + [HttpGet("{name}")] + public Task Get(string name) + { + return customerUiService.GetCustomer(name); + } - [HttpGet] - public Task> Get([FromQuery] CustomerFilterDto filter) - { - filter = filter ?? new CustomerFilterDto(); + [HttpGet] + public Task> Get([FromQuery] CustomerFilterDto filter) + { + filter = filter ?? new CustomerFilterDto(); - // Here you can decide if you want the collections as well + // Here you can decide if you want the collections as well - filter.LoadChildren = true; - filter.IsPagingEnabled = true; + filter.LoadChildren = true; + filter.IsPagingEnabled = true; - return customerUiService.GetCustomers(filter); - } + return customerUiService.GetCustomers(filter); + } } diff --git a/sample/Ardalis.SampleApp.Web/Interfaces/ICustomerUiService.cs b/sample/Ardalis.SampleApp.Web/Interfaces/ICustomerUiService.cs index 7dcff9ec..52727ec4 100644 --- a/sample/Ardalis.SampleApp.Web/Interfaces/ICustomerUiService.cs +++ b/sample/Ardalis.SampleApp.Web/Interfaces/ICustomerUiService.cs @@ -1,14 +1,14 @@ -using System.Collections.Generic; +using Ardalis.SampleApp.Web.Models; +using System.Collections.Generic; using System.Threading.Tasks; -using Ardalis.SampleApp.Web.Models; namespace Ardalis.SampleApp.Web.Interfaces; public interface ICustomerUiService { - Task GetCustomer(int customerId); - Task GetCustomer(string customerName); - Task GetCustomerWithStores(string customerName); + Task GetCustomer(int customerId); + Task GetCustomer(string customerName); + Task GetCustomerWithStores(string customerName); - Task> GetCustomers(CustomerFilterDto filterDto); + Task> GetCustomers(CustomerFilterDto filterDto); } diff --git a/sample/Ardalis.SampleApp.Web/Models/BaseFilterDto.cs b/sample/Ardalis.SampleApp.Web/Models/BaseFilterDto.cs index e66cb42d..c538991c 100644 --- a/sample/Ardalis.SampleApp.Web/Models/BaseFilterDto.cs +++ b/sample/Ardalis.SampleApp.Web/Models/BaseFilterDto.cs @@ -1,22 +1,18 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; +using System.ComponentModel; using System.Text.Json.Serialization; -using System.Threading.Tasks; namespace Ardalis.SampleApp.Web.Models; public class BaseFilterDto { - [Browsable(false)] - [JsonIgnore] - public bool LoadChildren { get; set; } = false; + [Browsable(false)] + [JsonIgnore] + public bool LoadChildren { get; set; } = false; - [Browsable(false)] - [JsonIgnore] - public bool IsPagingEnabled { get; set; } = true; + [Browsable(false)] + [JsonIgnore] + public bool IsPagingEnabled { get; set; } = true; - public int Page { get; set; } = 1; - public int PageSize { get; set; } = 10; + public int Page { get; set; } = 1; + public int PageSize { get; set; } = 10; } diff --git a/sample/Ardalis.SampleApp.Web/Models/CustomerDto.cs b/sample/Ardalis.SampleApp.Web/Models/CustomerDto.cs index ef030aed..456de349 100644 --- a/sample/Ardalis.SampleApp.Web/Models/CustomerDto.cs +++ b/sample/Ardalis.SampleApp.Web/Models/CustomerDto.cs @@ -1,16 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Collections.Generic; namespace Ardalis.SampleApp.Web.Models; public class CustomerDto { - public int Id { get; set; } - public string Name { get; set; } - public string Email { get; set; } - public string Address { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Address { get; set; } - public List Stores { get; set; } = new List(); + public List Stores { get; set; } = new List(); } diff --git a/sample/Ardalis.SampleApp.Web/Models/CustomerFilterDto.cs b/sample/Ardalis.SampleApp.Web/Models/CustomerFilterDto.cs index c2bcc00c..2dba8243 100644 --- a/sample/Ardalis.SampleApp.Web/Models/CustomerFilterDto.cs +++ b/sample/Ardalis.SampleApp.Web/Models/CustomerFilterDto.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text.Json.Serialization; -using System.Threading.Tasks; - -namespace Ardalis.SampleApp.Web.Models; +namespace Ardalis.SampleApp.Web.Models; public class CustomerFilterDto : BaseFilterDto { - public string Name { get; set; } - public string Email { get; set; } - public string Address { get; set; } + public string Name { get; set; } + public string Email { get; set; } + public string Address { get; set; } } diff --git a/sample/Ardalis.SampleApp.Web/Models/StoreDto.cs b/sample/Ardalis.SampleApp.Web/Models/StoreDto.cs index cfd8cdb5..f790a60f 100644 --- a/sample/Ardalis.SampleApp.Web/Models/StoreDto.cs +++ b/sample/Ardalis.SampleApp.Web/Models/StoreDto.cs @@ -1,13 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - -namespace Ardalis.SampleApp.Web.Models; +namespace Ardalis.SampleApp.Web.Models; public class StoreDto { - public int Id { get; set; } - public string Name { get; set; } - public string Address { get; set; } + public int Id { get; set; } + public string Name { get; set; } + public string Address { get; set; } } diff --git a/sample/Ardalis.SampleApp.Web/Pages/Index.cshtml.cs b/sample/Ardalis.SampleApp.Web/Pages/Index.cshtml.cs index 42678637..1c0d68cd 100644 --- a/sample/Ardalis.SampleApp.Web/Pages/Index.cshtml.cs +++ b/sample/Ardalis.SampleApp.Web/Pages/Index.cshtml.cs @@ -1,35 +1,35 @@ -using System.Collections.Generic; -using System.Diagnostics; -using System.Threading.Tasks; -using Ardalis.SampleApp.Core.Entities.CustomerAggregate; +using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.SampleApp.Core.Interfaces; using Ardalis.SampleApp.Core.Specifications; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; +using System.Collections.Generic; +using System.Diagnostics; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Web.Pages; public class IndexModel : PageModel { - private readonly IReadRepository _customerRepository; - private readonly ILogger _logger; + private readonly IReadRepository _customerRepository; + private readonly ILogger _logger; - public IndexModel(IReadRepository customerRepository, - ILogger logger) - { - _customerRepository = customerRepository; - _logger = logger; - } + public IndexModel(IReadRepository customerRepository, + ILogger logger) + { + _customerRepository = customerRepository; + _logger = logger; + } - public List Customers { get; set; } - public long ElapsedTimeMilliseconds { get; set; } + public List Customers { get; set; } + public long ElapsedTimeMilliseconds { get; set; } - public async Task OnGet() - { - var timer = Stopwatch.StartNew(); - var spec = new CustomerByNameWithStoresSpec(name: "Customer66"); - Customers = await _customerRepository.ListAsync(spec); - timer.Stop(); - ElapsedTimeMilliseconds = timer.ElapsedMilliseconds; - } + public async Task OnGet() + { + var timer = Stopwatch.StartNew(); + var spec = new CustomerByNameWithStoresSpec(name: "Customer66"); + Customers = await _customerRepository.ListAsync(spec); + timer.Stop(); + ElapsedTimeMilliseconds = timer.ElapsedMilliseconds; + } } diff --git a/sample/Ardalis.SampleApp.Web/Program.cs b/sample/Ardalis.SampleApp.Web/Program.cs index 42c02c07..cafa2469 100644 --- a/sample/Ardalis.SampleApp.Web/Program.cs +++ b/sample/Ardalis.SampleApp.Web/Program.cs @@ -1,42 +1,42 @@ -using System; -using System.Threading.Tasks; -using Ardalis.SampleApp.Infrastructure.DataAccess; +using Ardalis.SampleApp.Infrastructure.DataAccess; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using System; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Web; public class Program { - public static async Task Main(string[] args) - { - var host = CreateHostBuilder(args).Build(); - - using (var scope = host.Services.CreateScope()) + public static async Task Main(string[] args) { - var services = scope.ServiceProvider; - try - { - var dbContext = services.GetRequiredService(); + var host = CreateHostBuilder(args).Build(); - await new SampleDbContextSeed(dbContext).SeedAsync(); - } - catch (Exception) - { - } - } + using (var scope = host.Services.CreateScope()) + { + var services = scope.ServiceProvider; + try + { + var dbContext = services.GetRequiredService(); + + await new SampleDbContextSeed(dbContext).SeedAsync(); + } + catch (Exception) + { + } + } - host.Run(); - } + host.Run(); + } - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.ConfigureLogging(config => - config.AddConsole()); - webBuilder.UseStartup(); - }); + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.ConfigureLogging(config => + config.AddConsole()); + webBuilder.UseStartup(); + }); } diff --git a/sample/Ardalis.SampleApp.Web/Services/CustomerUiService.cs b/sample/Ardalis.SampleApp.Web/Services/CustomerUiService.cs index 018be348..1fe98986 100644 --- a/sample/Ardalis.SampleApp.Web/Services/CustomerUiService.cs +++ b/sample/Ardalis.SampleApp.Web/Services/CustomerUiService.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Threading.Tasks; -using Ardalis.GuardClauses; +using Ardalis.GuardClauses; using Ardalis.SampleApp.Core.Entities.CustomerAggregate; using Ardalis.SampleApp.Core.Interfaces; using Ardalis.SampleApp.Core.Specifications; @@ -8,56 +6,58 @@ using Ardalis.SampleApp.Web.Interfaces; using Ardalis.SampleApp.Web.Models; using AutoMapper; +using System.Collections.Generic; +using System.Threading.Tasks; namespace Ardalis.SampleApp.Web.Services; public class CustomerUiService : ICustomerUiService { - private readonly IMapper mapper; - private readonly IReadRepository customerRepository; + private readonly IMapper _mapper; + private readonly IReadRepository _customerRepository; - public CustomerUiService(IMapper mapper, - IReadRepository customerRepository) - { - this.mapper = mapper; - this.customerRepository = customerRepository; - } + public CustomerUiService(IMapper mapper, + IReadRepository customerRepository) + { + _mapper = mapper; + _customerRepository = customerRepository; + } - // Here I'm just writing various usages, not necessarily you'll need all of them. + // Here I'm just writing various usages, not necessarily you'll need all of them. - public async Task GetCustomer(int customerId) - { - var customer = await customerRepository.GetByIdAsync(customerId); + public async Task GetCustomer(int customerId) + { + var customer = await _customerRepository.GetByIdAsync(customerId); - Guard.Against.Null(customer, nameof(customer)); + Guard.Against.Null(customer, nameof(customer)); - return mapper.Map(customer); - } + return _mapper.Map(customer); + } - public async Task GetCustomer(string customerName) - { - var customer = await customerRepository.GetBySpecAsync(new CustomerByNameSpec(customerName)); + public async Task GetCustomer(string customerName) + { + var customer = await _customerRepository.GetBySpecAsync(new CustomerByNameSpec(customerName)); - Guard.Against.Null(customer, nameof(customer)); + Guard.Against.Null(customer, nameof(customer)); - return mapper.Map(customer); - } + return _mapper.Map(customer); + } - public async Task GetCustomerWithStores(string customerName) - { - var customer = await customerRepository.GetBySpecAsync(new CustomerByNameWithStoresSpec(customerName)); + public async Task GetCustomerWithStores(string customerName) + { + var customer = await _customerRepository.GetBySpecAsync(new CustomerByNameWithStoresSpec(customerName)); - Guard.Against.Null(customer, nameof(customer)); + Guard.Against.Null(customer, nameof(customer)); - return mapper.Map(customer); - } + return _mapper.Map(customer); + } - public async Task> GetCustomers(CustomerFilterDto filterDto) - { - var spec = new CustomerSpec(mapper.Map(filterDto)); - var customers = await customerRepository.ListAsync(spec); + public async Task> GetCustomers(CustomerFilterDto filterDto) + { + var spec = new CustomerSpec(_mapper.Map(filterDto)); + var customers = await _customerRepository.ListAsync(spec); - return mapper.Map>(customers); - } + return _mapper.Map>(customers); + } } diff --git a/sample/Ardalis.SampleApp.Web/Startup.cs b/sample/Ardalis.SampleApp.Web/Startup.cs index a1b2b70f..70b70f79 100644 --- a/sample/Ardalis.SampleApp.Web/Startup.cs +++ b/sample/Ardalis.SampleApp.Web/Startup.cs @@ -14,47 +14,47 @@ namespace Ardalis.SampleApp.Web; public class Startup { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } - public IConfiguration Configuration { get; } + public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection"))); + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("MyDbConnection"))); - services.AddAutoMapper(typeof(AutomapperMaps)); + services.AddAutoMapper(typeof(AutomapperMaps)); - services.AddScoped(typeof(IReadRepository<>), typeof(CachedRepository<>)); - services.AddScoped(typeof(MyRepository<>)); - services.AddScoped(); + services.AddScoped(typeof(IReadRepository<>), typeof(CachedRepository<>)); + services.AddScoped(typeof(MyRepository<>)); + services.AddScoped(); - // services.AddControllers(); - services.AddMvc(); - services.AddLogging(); - } + // services.AddControllers(); + services.AddMvc(); + services.AddLogging(); + } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - if (env.IsDevelopment()) + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - app.UseDeveloperExceptionPage(); - } + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } - app.UseHttpsRedirection(); + app.UseHttpsRedirection(); - app.UseRouting(); + app.UseRouting(); - app.UseAuthorization(); + app.UseAuthorization(); - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - endpoints.MapRazorPages(); - }); - } + app.UseEndpoints(endpoints => + { + endpoints.MapControllers(); + endpoints.MapRazorPages(); + }); + } }