From 2f23a65841e55458000a2886ab4ba343e483974d Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Fri, 24 Jan 2020 15:39:44 +0100 Subject: [PATCH 1/6] Cleanup of public overloads for `ILoggerFactory` Ensured consistent passing along of `ILoggerFactory` parameter downwards to base classes and dependencies. I ended up making them all non-optional, because it is easy to forget passing it (compiler does not warn). Callers can still pass `null` as a value, but that should be very rare, since ASP.NET Core includes built-in logging that is automatically registered. In the process, I added some missing namespaces in examples (types were in global namespace). `BaseJsonApiController` and `BaseJsonApiController`: - Made types abstract with protected constructors, fixed related tests - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards - Have overloads call each other, so that fields assignment occurs only once `JsonApiCmdController` and `JsonApiCmdController`, `JsonApiQueryController` and `JsonApiQueryController`: - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards - Changed `IResourceService<>` parameter into `IResourceQueryService<>` / `IResourceCmdService` (still allows to pass `IResourceService<>`, because it implements both) - Aligned parameter names with base class `JsonApiController` and `JsonApiController`: - Bugfix where loggerFactory was assigned the value `null` while passing it downwards! - Ensure all overloads have `ILoggerFactory` parameter and its value is passed downwards `DefaultResourceRepository` and `DefaultResourceRepository`: - Removed unneeded constructor overload `DefaultExceptionFilter`: - Added null conditional operator, because at logging time the logger is assumed to be possibly `null`. All unit tests succeed with these changes. --- .../Controllers/ArticlesController.cs | 9 +- .../Controllers/PeopleController.cs | 9 +- .../ModelsController.cs | 9 +- .../Controllers/ArticlesController.cs | 11 +- .../Controllers/CamelCasedModelsController.cs | 11 +- .../Controllers/PassportsController.cs | 7 +- .../Controllers/PeopleController.cs | 11 +- .../Controllers/PersonRolesController.cs | 11 +- .../Controllers/TodoCollectionsController.cs | 9 +- .../Controllers/TodoItemsController.cs | 11 +- .../Controllers/TodoItemsTestController.cs | 22 ++-- .../Controllers/UsersController.cs | 22 ++-- .../Services/CustomArticleService.cs | 13 +- .../Controllers/TodoItemsController.cs | 11 +- .../Controllers/ReportsController.cs | 15 +-- src/Examples/ReportsExample/Models/Report.cs | 23 ++-- .../ReportsExample/Services/ReportService.cs | 50 ++++---- .../Controllers/BaseJsonApiController.cs | 119 ++++++------------ .../Controllers/JsonApiCmdController.cs | 27 ++-- .../Controllers/JsonApiController.cs | 77 +++++------- .../Controllers/JsonApiQueryController.cs | 32 +++-- .../Data/DefaultResourceRepository.cs | 18 +-- .../Middleware/DefaultExceptionFilter.cs | 4 +- .../Services/DefaultResourceService.cs | 24 ++-- .../ServiceDiscoveryFacadeTests.cs | 16 +-- .../Data/EntityRepositoryTests.cs | 2 +- .../BaseJsonApiController_Tests.cs | 77 ++++++------ .../Data/DefaultEntityRepositoryTest.cs | 2 +- .../Services/EntityResourceService_Tests.cs | 2 +- 29 files changed, 287 insertions(+), 367 deletions(-) diff --git a/src/Examples/GettingStarted/Controllers/ArticlesController.cs b/src/Examples/GettingStarted/Controllers/ArticlesController.cs index 2bc928a46f..dd68a1f924 100644 --- a/src/Examples/GettingStarted/Controllers/ArticlesController.cs +++ b/src/Examples/GettingStarted/Controllers/ArticlesController.cs @@ -2,15 +2,16 @@ using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Services; +using Microsoft.Extensions.Logging; namespace GettingStarted { public class ArticlesController : JsonApiController
{ - public ArticlesController( - IJsonApiOptions jsonApiOptions, + public ArticlesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, IResourceService
resourceService) - : base(jsonApiOptions, resourceService) - { } + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/GettingStarted/Controllers/PeopleController.cs b/src/Examples/GettingStarted/Controllers/PeopleController.cs index 95eac64346..61712d19f7 100644 --- a/src/Examples/GettingStarted/Controllers/PeopleController.cs +++ b/src/Examples/GettingStarted/Controllers/PeopleController.cs @@ -2,15 +2,16 @@ using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Services; +using Microsoft.Extensions.Logging; namespace GettingStarted { public class PeopleController : JsonApiController { - public PeopleController( - IJsonApiOptions jsonApiOptions, + public PeopleController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, IResourceService resourceService) - : base(jsonApiOptions, resourceService) - { } + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs b/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs index 1b488ed383..d5a5002451 100644 --- a/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs +++ b/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs @@ -1,15 +1,16 @@ using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Services; +using Microsoft.Extensions.Logging; namespace GettingStarted.ResourceDefinitionExample { public class ModelsController : JsonApiController { - public ModelsController( - IJsonApiOptions jsonApiOptions, + public ModelsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, IResourceService resourceService) - : base(jsonApiOptions, resourceService) - { } + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs index faa533093c..12f99b9651 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs @@ -2,15 +2,16 @@ using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Services; using JsonApiDotNetCoreExample.Models; +using Microsoft.Extensions.Logging; namespace JsonApiDotNetCoreExample.Controllers { public class ArticlesController : JsonApiController
{ - public ArticlesController( - IJsonApiOptions jsonApiOptions, - IResourceService
resourceService) - : base(jsonApiOptions, resourceService) - { } + public ArticlesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService
resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs index 01db4c2720..8aa0fca727 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs @@ -8,11 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers { public class KebabCasedModelsController : JsonApiController { - public KebabCasedModelsController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public KebabCasedModelsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs index a040ff21e4..78599939bf 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs @@ -8,10 +8,9 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PassportsController : JsonApiController { - public PassportsController(IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory = null) - : base(jsonApiOptions, resourceService, loggerFactory) + public PassportsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) { } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs index 851b2cfc80..b8191708f0 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs @@ -8,11 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PeopleController : JsonApiController { - public PeopleController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public PeopleController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs index bee457a1cb..a8aa6348be 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs @@ -8,11 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PersonRolesController : JsonApiController { - public PersonRolesController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public PersonRolesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs index d300e24f46..0af7bc11e5 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs @@ -17,12 +17,9 @@ public class TodoCollectionsController : JsonApiController resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) + public TodoCollectionsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IDbContextResolver contextResolver, IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) { _dbResolver = contextResolver; } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs index 818e082db9..bc7cceabf2 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs @@ -8,11 +8,10 @@ namespace JsonApiDotNetCoreExample.Controllers { public class TodoItemsController : JsonApiController { - public TodoItemsController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public TodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs index 971f579b69..884af5d275 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs @@ -11,22 +11,20 @@ namespace JsonApiDotNetCoreExample.Controllers public abstract class AbstractTodoItemsController : JsonApiController where T : class, IIdentifiable { - protected AbstractTodoItemsController( - IJsonApiOptions jsonApiOptions, - IResourceService service, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, service, loggerFactory) - { } + protected AbstractTodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService service) + : base(jsonApiOptions, loggerFactory, service) + { + } } [Route("/abstract")] public class TodoItemsTestController : AbstractTodoItemsController { - public TodoItemsTestController( - IJsonApiOptions jsonApiOptions, - IResourceService service, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, service, loggerFactory) - { } + public TodoItemsTestController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService service) + : base(jsonApiOptions, loggerFactory, service) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs index de1996d5e5..93a143c6e8 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs @@ -8,21 +8,19 @@ namespace JsonApiDotNetCoreExample.Controllers { public class UsersController : JsonApiController { - public UsersController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public UsersController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } public class SuperUsersController : JsonApiController { - public SuperUsersController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public SuperUsersController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs b/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs index d34f32756d..9ba344a4f5 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs @@ -14,13 +14,12 @@ namespace JsonApiDotNetCoreExample.Services { public class CustomArticleService : DefaultResourceService
{ - public CustomArticleService(IEnumerable queryParameters, - IJsonApiOptions options, - IResourceRepository repository, - IResourceContextProvider provider, - IResourceHookExecutor hookExecutor = null, - ILoggerFactory loggerFactory = null) - : base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { } + public CustomArticleService(IEnumerable queryParameters, IJsonApiOptions options, + ILoggerFactory loggerFactory, IResourceRepository repository, + IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) + { + } public override async Task
GetAsync(int id) { diff --git a/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs b/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs index cf18987700..1d097def78 100644 --- a/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs +++ b/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs @@ -8,11 +8,10 @@ namespace NoEntityFrameworkExample.Controllers { public class TodoItemsController : JsonApiController { - public TodoItemsController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public TodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } } } diff --git a/src/Examples/ReportsExample/Controllers/ReportsController.cs b/src/Examples/ReportsExample/Controllers/ReportsController.cs index 77099fe380..4fcd903f38 100644 --- a/src/Examples/ReportsExample/Controllers/ReportsController.cs +++ b/src/Examples/ReportsExample/Controllers/ReportsController.cs @@ -2,19 +2,20 @@ using Microsoft.AspNetCore.Mvc; using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Services; -using JsonApiDotNetCore.Configuration; -using JsonApiDotNetCore.Internal.Contracts; - +using JsonApiDotNetCore.Configuration; +using Microsoft.Extensions.Logging; +using ReportsExample.Models; + namespace ReportsExample.Controllers { [Route("api/[controller]")] public class ReportsController : BaseJsonApiController { - public ReportsController( - IJsonApiOptions jsonApiOptions, + public ReportsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, IGetAllService getAll) - : base(jsonApiOptions, getAll: getAll) - { } + : base(jsonApiOptions, loggerFactory, getAll) + { + } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); diff --git a/src/Examples/ReportsExample/Models/Report.cs b/src/Examples/ReportsExample/Models/Report.cs index 241221cdaa..5bc93edb49 100644 --- a/src/Examples/ReportsExample/Models/Report.cs +++ b/src/Examples/ReportsExample/Models/Report.cs @@ -1,15 +1,18 @@ using JsonApiDotNetCore.Models; -public class Report : Identifiable +namespace ReportsExample.Models { - [Attr] - public string Title { get; set; } + public class Report : Identifiable + { + [Attr] + public string Title { get; set; } - [Attr] - public ComplexType ComplexType { get; set; } -} + [Attr] + public ComplexType ComplexType { get; set; } + } -public class ComplexType -{ - public string CompoundPropertyName { get; set; } -} \ No newline at end of file + public class ComplexType + { + public string CompoundPropertyName { get; set; } + } +} diff --git a/src/Examples/ReportsExample/Services/ReportService.cs b/src/Examples/ReportsExample/Services/ReportService.cs index 9e5348a612..542b276a5b 100644 --- a/src/Examples/ReportsExample/Services/ReportService.cs +++ b/src/Examples/ReportsExample/Services/ReportService.cs @@ -2,36 +2,40 @@ using System.Threading.Tasks; using JsonApiDotNetCore.Services; using Microsoft.Extensions.Logging; +using ReportsExample.Models; -public class ReportService : IGetAllService +namespace ReportsExample.Services { - private ILogger _logger; - - public ReportService(ILoggerFactory loggerFactory) + public class ReportService : IGetAllService { - _logger = loggerFactory.CreateLogger(); - } + private ILogger _logger; - public Task> GetAsync() - { - _logger.LogError("GetAsync"); + public ReportService(ILoggerFactory loggerFactory) + { + _logger = loggerFactory.CreateLogger(); + } - var task = new Task>(() => Get()); + public Task> GetAsync() + { + _logger.LogError("GetAsync"); + + var task = new Task>(() => Get()); - task.RunSynchronously(TaskScheduler.Default); + task.RunSynchronously(TaskScheduler.Default); - return task; - } + return task; + } - private IEnumerable Get() - { - return new List { - new Report { - Title = "My Report", - ComplexType = new ComplexType { - CompoundPropertyName = "value" + private IEnumerable Get() + { + return new List { + new Report { + Title = "My Report", + ComplexType = new ComplexType { + CompoundPropertyName = "value" + } } - } - }; + }; + } } -} \ No newline at end of file +} diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs index c241c5b81d..6f8db796e8 100644 --- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs @@ -9,10 +9,9 @@ namespace JsonApiDotNetCore.Controllers { - public class BaseJsonApiController - : JsonApiControllerMixin - where T : class, IIdentifiable + public abstract class BaseJsonApiController : JsonApiControllerMixin where T : class, IIdentifiable { + private readonly IJsonApiOptions _jsonApiOptions; private readonly IGetAllService _getAll; private readonly IGetByIdService _getById; private readonly IGetRelationshipService _getRelationship; @@ -22,63 +21,26 @@ public class BaseJsonApiController private readonly IUpdateRelationshipService _updateRelationships; private readonly IDeleteService _delete; private readonly ILogger> _logger; - private readonly IJsonApiOptions _jsonApiOptions; - - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - { - if (loggerFactory != null) - _logger = loggerFactory.CreateLogger>(); - else - _logger = new Logger>(new LoggerFactory()); - _jsonApiOptions = jsonApiOptions; - _getAll = resourceService; - _getById = resourceService; - _getRelationship = resourceService; - _getRelationships = resourceService; - _create = resourceService; - _update = resourceService; - _updateRelationships = resourceService; - _delete = resourceService; + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : this(jsonApiOptions, loggerFactory, resourceService, resourceService, resourceService, resourceService, + resourceService, resourceService, resourceService, resourceService) + { } - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceQueryService queryService = null, - IResourceCmdService cmdService = null) + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceQueryService queryService = null, IResourceCmdService cmdService = null) + : this(jsonApiOptions, loggerFactory, queryService, queryService, queryService, queryService, cmdService, + cmdService, cmdService, cmdService) { - _jsonApiOptions = jsonApiOptions; - _getAll = queryService; - _getById = queryService; - _getRelationship = queryService; - _getRelationships = queryService; - _create = cmdService; - _update = cmdService; - _updateRelationships = cmdService; - _delete = cmdService; } - /// - /// - /// - /// - /// - /// - /// - /// - /// - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IGetAllService getAll = null, - IGetByIdService getById = null, + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IGetAllService getAll = null, IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, - ICreateService create = null, - IUpdateService update = null, - IUpdateRelationshipService updateRelationships = null, + IGetRelationshipsService getRelationships = null, ICreateService create = null, + IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) { _jsonApiOptions = jsonApiOptions; @@ -90,6 +52,10 @@ public BaseJsonApiController( _update = update; _updateRelationships = updateRelationships; _delete = delete; + + _logger = loggerFactory != null + ? loggerFactory.CreateLogger>() + : new Logger>(new LoggerFactory()); } public virtual async Task GetAsync() @@ -193,32 +159,29 @@ public virtual async Task DeleteAsync(TId id) } } - public class BaseJsonApiController - : BaseJsonApiController - where T : class, IIdentifiable + public abstract class BaseJsonApiController : BaseJsonApiController where T : class, IIdentifiable { - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService - ) : base(jsonApiOptions, resourceService, resourceService) { } - - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceQueryService queryService = null, - IResourceCmdService cmdService = null - ) : base(jsonApiOptions, queryService, cmdService) { } - - - public BaseJsonApiController( - IJsonApiOptions jsonApiOptions, - IGetAllService getAll = null, - IGetByIdService getById = null, + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService, resourceService) + { + } + + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceQueryService queryService = null, IResourceCmdService cmdService = null) + : base(jsonApiOptions, loggerFactory, queryService, cmdService) + { + } + + protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IGetAllService getAll = null, IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, - ICreateService create = null, - IUpdateService update = null, - IUpdateRelationshipService updateRelationships = null, - IDeleteService delete = null - ) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { } + IGetRelationshipsService getRelationships = null, ICreateService create = null, + IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IDeleteService delete = null) + : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, + updateRelationships, delete) + { + } } } diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiCmdController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiCmdController.cs index d88ee0272d..335dc83e16 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiCmdController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiCmdController.cs @@ -3,27 +3,26 @@ using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Services; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; namespace JsonApiDotNetCore.Controllers { - public class JsonApiCmdController : JsonApiCmdController - where T : class, IIdentifiable + public class JsonApiCmdController : JsonApiCmdController where T : class, IIdentifiable { - public JsonApiCmdController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService) - : base(jsonApiOptions, resourceService) - { } + public JsonApiCmdController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceCmdService cmdService) + : base(jsonApiOptions, loggerFactory, cmdService) + { + } } - public class JsonApiCmdController - : BaseJsonApiController where T : class, IIdentifiable + public class JsonApiCmdController : BaseJsonApiController where T : class, IIdentifiable { - public JsonApiCmdController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService) - : base(jsonApiOptions, resourceService) - { } + public JsonApiCmdController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceCmdService cmdService) + : base(jsonApiOptions, loggerFactory, null, cmdService) + { + } [HttpPost] public override async Task PostAsync([FromBody] T entity) diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs index b61284ac0b..f023b76e80 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs @@ -9,27 +9,22 @@ namespace JsonApiDotNetCore.Controllers { public class JsonApiController : BaseJsonApiController where T : class, IIdentifiable { - /// - /// - /// - public JsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory = null) - : base(jsonApiOptions, resourceService, loggerFactory = null) - { } + public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } - public JsonApiController( - IJsonApiOptions jsonApiOptions, - IGetAllService getAll = null, - IGetByIdService getById = null, + public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IGetAllService getAll = null, IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, - ICreateService create = null, - IUpdateService update = null, - IUpdateRelationshipService updateRelationships = null, - IDeleteService delete = null - ) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { } + IGetRelationshipsService getRelationships = null, ICreateService create = null, + IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IDeleteService delete = null) + : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, + updateRelationships, delete) + { + } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); @@ -64,39 +59,23 @@ public override async Task PatchRelationshipsAsync( public override async Task DeleteAsync(TId id) => await base.DeleteAsync(id); } - /// - /// JsonApiController with int as default - /// - /// public class JsonApiController : JsonApiController where T : class, IIdentifiable { - /// - /// Base constructor with int as default - /// - /// - /// - /// - /// - public JsonApiController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory = null - ) - : base(jsonApiOptions, resourceService, loggerFactory) - { } + public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } - public JsonApiController( - IJsonApiOptions jsonApiOptions, - IGetAllService getAll = null, - IGetByIdService getById = null, + public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IGetAllService getAll = null, IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, - ICreateService create = null, - IUpdateService update = null, - IUpdateRelationshipService updateRelationships = null, - IDeleteService delete = null - ) : base(jsonApiOptions, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) { } - - + IGetRelationshipsService getRelationships = null, ICreateService create = null, + IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IDeleteService delete = null) + : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, + updateRelationships, delete) + { + } } } diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs index 1d49b984ea..96470f9531 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs @@ -3,31 +3,27 @@ using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Services; using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Logging; namespace JsonApiDotNetCore.Controllers { - public class JsonApiQueryController - : JsonApiQueryController where T : class, IIdentifiable + public class JsonApiQueryController : JsonApiQueryController where T : class, IIdentifiable { - public JsonApiQueryController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService) - : base(jsonApiOptions, resourceService) { } + public JsonApiQueryController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceQueryService queryService) + : base(jsonApiOptions, loggerFactory, queryService) + { + } } - public class JsonApiQueryController - : BaseJsonApiController where T : class, IIdentifiable + public class JsonApiQueryController : BaseJsonApiController where T : class, IIdentifiable { - public JsonApiQueryController( - IJsonApiOptions jsonApiContext, - IResourceQueryService resourceQueryService) - : base(jsonApiContext, resourceQueryService) { } - - - public JsonApiQueryController( - IJsonApiOptions jsonApiOptions, - IResourceService resourceService) - : base(jsonApiOptions, resourceService) { } + public JsonApiQueryController(IJsonApiOptions jsonApiContext, + ILoggerFactory loggerFactory, + IResourceQueryService queryService) + : base(jsonApiContext, loggerFactory, queryService) + { + } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); diff --git a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs index 079115d378..e556cbea5b 100644 --- a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs +++ b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs @@ -28,20 +28,12 @@ public class DefaultResourceRepository : IResourceRepository : DefaultResourceRepository, IResourceRepository where TResource : class, IIdentifiable { - public DefaultResourceRepository(ITargetedFields targetedFields, - IDbContextResolver contextResolver, - IResourceGraph resourceGraph, - IGenericServiceFactory genericServiceFactory) - : base(targetedFields, contextResolver, resourceGraph, genericServiceFactory) { } - public DefaultResourceRepository(ITargetedFields targetedFields, IDbContextResolver contextResolver, IResourceGraph resourceGraph, IGenericServiceFactory genericServiceFactory, - ILoggerFactory loggerFactory = null) + ILoggerFactory loggerFactory) : base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, loggerFactory) { } } } diff --git a/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs b/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs index b6c82b27e3..1880d70da5 100644 --- a/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs +++ b/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs @@ -1,4 +1,4 @@ -using JsonApiDotNetCore.Internal; +using JsonApiDotNetCore.Internal; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; @@ -14,7 +14,7 @@ public class DefaultExceptionFilter : ActionFilterAttribute, IExceptionFilter public DefaultExceptionFilter(ILoggerFactory loggerFactory) { - _logger = loggerFactory.CreateLogger(); + _logger = loggerFactory?.CreateLogger(); } public void OnException(ExceptionContext context) diff --git a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs index cb4328947a..05e15c0a0a 100644 --- a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs +++ b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs @@ -34,13 +34,9 @@ public class DefaultResourceService : private readonly ISparseFieldsService _sparseFieldsService; private readonly ResourceContext _currentRequestResource; - public DefaultResourceService( - IEnumerable queryParameters, - IJsonApiOptions options, - IResourceRepository repository, - IResourceContextProvider provider, - IResourceHookExecutor hookExecutor = null, - ILoggerFactory loggerFactory = null) + public DefaultResourceService(IEnumerable queryParameters, IJsonApiOptions options, + ILoggerFactory loggerFactory, IResourceRepository repository, + IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) { _includeService = queryParameters.FirstOrDefault(); _sparseFieldsService = queryParameters.FirstOrDefault(); @@ -54,7 +50,6 @@ public DefaultResourceService( _currentRequestResource = provider.GetResourceContext(); } - public virtual async Task CreateAsync(TResource entity) { entity = IsNull(_hookExecutor) ? entity : _hookExecutor.BeforeCreate(AsList(entity), ResourcePipeline.Post).SingleOrDefault(); @@ -357,12 +352,11 @@ public class DefaultResourceService : DefaultResourceService where TResource : class, IIdentifiable { - public DefaultResourceService(IEnumerable queryParameters, - IJsonApiOptions options, - IResourceRepository repository, - IResourceContextProvider provider, - IResourceHookExecutor hookExecutor = null, - ILoggerFactory loggerFactory = null) - : base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { } + public DefaultResourceService(IEnumerable queryParameters, IJsonApiOptions options, + ILoggerFactory loggerFactory, IResourceRepository repository, + IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) + { + } } } diff --git a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs index cf87a3f537..858ecbf3eb 100644 --- a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs +++ b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs @@ -34,6 +34,7 @@ public ServiceDiscoveryFacadeTests() dbResolverMock.Setup(m => m.GetContext()).Returns(new Mock().Object); TestModelRepository._dbContextResolver = dbResolverMock.Object; _services.AddSingleton(new JsonApiOptions()); + _services.AddSingleton(new LoggerFactory()); _services.AddScoped((_) => new Mock().Object); _services.AddScoped((_) => new Mock().Object); _services.AddScoped((_) => new Mock().Object); @@ -102,13 +103,12 @@ public class TestModelService : DefaultResourceService { private static IResourceRepository _repo = new Mock>().Object; - public TestModelService(IEnumerable queryParameters, - IJsonApiOptions options, - IResourceRepository repository, - IResourceContextProvider provider, - IResourceHookExecutor hookExecutor = null, - ILoggerFactory loggerFactory = null) - : base(queryParameters, options, repository, provider, hookExecutor, loggerFactory) { } + public TestModelService(IEnumerable queryParameters, IJsonApiOptions options, + ILoggerFactory loggerFactory, IResourceRepository repository, + IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) + { + } } public class TestModelRepository : DefaultResourceRepository @@ -118,7 +118,7 @@ public class TestModelRepository : DefaultResourceRepository public TestModelRepository(ITargetedFields targetedFields, IResourceGraph resourceGraph, IGenericServiceFactory genericServiceFactory) - : base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory) { } + : base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory, null) { } } } } diff --git a/test/IntegrationTests/Data/EntityRepositoryTests.cs b/test/IntegrationTests/Data/EntityRepositoryTests.cs index 8e45c13c30..c66bbcaeb0 100644 --- a/test/IntegrationTests/Data/EntityRepositoryTests.cs +++ b/test/IntegrationTests/Data/EntityRepositoryTests.cs @@ -154,7 +154,7 @@ public async Task Paging_PageNumberIsNegative_GiveBackReverseAmountOfIds(int pag contextResolverMock.Setup(m => m.GetContext()).Returns(context); var resourceGraph = new ResourceGraphBuilder().AddResource().Build(); var targetedFields = new Mock(); - var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph, null); + var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph, null, null); return (repository, targetedFields); } diff --git a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs index 06a95fd5e2..b7a84f1687 100644 --- a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs +++ b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs @@ -8,7 +8,7 @@ using JsonApiDotNetCore.Internal; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; - +using Microsoft.Extensions.Logging; namespace UnitTests { @@ -19,27 +19,46 @@ public class Resource : Identifiable [Attr] public string TestAttribute { get; set; } } + public class ResourceController : BaseJsonApiController + { + public ResourceController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) + { + } + + public ResourceController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + IGetAllService getAll = null, IGetByIdService getById = null, + IGetRelationshipService getRelationship = null, + IGetRelationshipsService getRelationships = null, + ICreateService create = null, IUpdateService update = null, + IUpdateRelationshipService updateRelationships = null, + IDeleteService delete = null) + : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, + update, updateRelationships, delete) + { + } + } + [Fact] public async Task GetAsync_Calls_Service() { // Arrange var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getAll: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, getAll: serviceMock.Object); // Act await controller.GetAsync(); // Assert serviceMock.Verify(m => m.GetAsync(), Times.Once); - } [Fact] public async Task GetAsync_Throws_405_If_No_Service() { // Arrange - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, null); + var controller = new ResourceController(new Mock().Object, null, null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetAsync()); @@ -54,14 +73,13 @@ public async Task GetAsyncById_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getById: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, getById: serviceMock.Object); // Act await controller.GetAsync(id); // Assert serviceMock.Verify(m => m.GetAsync(id), Times.Once); - } [Fact] @@ -69,8 +87,7 @@ public async Task GetAsyncById_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getById: null); + var controller = new ResourceController(new Mock().Object, null, getById: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetAsync(id)); @@ -85,7 +102,7 @@ public async Task GetRelationshipsAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getRelationships: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, getRelationships: serviceMock.Object); // Act await controller.GetRelationshipsAsync(id, string.Empty); @@ -99,8 +116,7 @@ public async Task GetRelationshipsAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getRelationships: null); + var controller = new ResourceController(new Mock().Object, null, getRelationships: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetRelationshipsAsync(id, string.Empty)); @@ -115,7 +131,7 @@ public async Task GetRelationshipAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getRelationship: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, getRelationship: serviceMock.Object); // Act await controller.GetRelationshipAsync(id, string.Empty); @@ -129,8 +145,7 @@ public async Task GetRelationshipAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, getRelationship: null); + var controller = new ResourceController(new Mock().Object, null, getRelationship: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetRelationshipAsync(id, string.Empty)); @@ -147,7 +162,7 @@ public async Task PatchAsync_Calls_Service() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions(), update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), null, update: serviceMock.Object); // Act await controller.PatchAsync(id, resource); @@ -163,7 +178,7 @@ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateDisbled() const int id = 0; var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions(), update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), null, update: serviceMock.Object); // Act var response = await controller.PatchAsync(id, resource); @@ -181,7 +196,7 @@ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateEnabled() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions { ValidateModelState = true }, update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, null, update: serviceMock.Object); controller.ModelState.AddModelError("TestAttribute", "Failed Validation"); // Act @@ -198,8 +213,7 @@ public async Task PatchAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, update: null); + var controller = new ResourceController(new Mock().Object, null, update: null); // Act var exception = await Assert.ThrowsAsync(() => controller.PatchAsync(id, It.IsAny())); @@ -215,7 +229,7 @@ public async Task PostAsync_Calls_Service() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions(), create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), null, create: serviceMock.Object); serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); controller.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext() }; @@ -232,11 +246,10 @@ public async Task PostAsync_ModelStateInvalid_ValidateModelStateDisabled() // Arrange var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions { ValidateModelState = false }, create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = false }, null, create: serviceMock.Object); controller.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext() }; serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); - // Act var response = await controller.PostAsync(resource); @@ -251,12 +264,11 @@ public async Task PostAsync_ModelStateInvalid_ValidateModelStateEnabled() // Arrange var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new JsonApiOptions { ValidateModelState = true }, create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, null, create: serviceMock.Object); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }; controller.ModelState.AddModelError("TestAttribute", "Failed Validation"); serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); - // Act var response = await controller.PostAsync(resource); @@ -271,9 +283,8 @@ public async Task PatchRelationshipsAsync_Calls_Service() { // Arrange const int id = 0; - var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, updateRelationships: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, updateRelationships: serviceMock.Object); // Act await controller.PatchRelationshipsAsync(id, string.Empty, null); @@ -287,8 +298,7 @@ public async Task PatchRelationshipsAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, updateRelationships: null); + var controller = new ResourceController(new Mock().Object, null, updateRelationships: null); // Act var exception = await Assert.ThrowsAsync(() => controller.PatchRelationshipsAsync(id, string.Empty, null)); @@ -302,9 +312,8 @@ public async Task DeleteAsync_Calls_Service() { // Arrange const int id = 0; - var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, delete: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, null, delete: serviceMock.Object); // Act await controller.DeleteAsync(id); @@ -318,9 +327,7 @@ public async Task DeleteAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var serviceMock = new Mock>(); - var controller = new BaseJsonApiController(new Mock().Object, - + var controller = new ResourceController(new Mock().Object, null, delete: null); // Act @@ -329,7 +336,5 @@ public async Task DeleteAsync_Throws_405_If_No_Service() // Assert Assert.Equal(405, exception.GetStatusCode()); } - - } } diff --git a/test/UnitTests/Data/DefaultEntityRepositoryTest.cs b/test/UnitTests/Data/DefaultEntityRepositoryTest.cs index 1428ab3376..b2487eb628 100644 --- a/test/UnitTests/Data/DefaultEntityRepositoryTest.cs +++ b/test/UnitTests/Data/DefaultEntityRepositoryTest.cs @@ -66,7 +66,7 @@ private DefaultResourceRepository Setup() contextResolverMock.Setup(m => m.GetContext()).Returns(new Mock().Object); var resourceGraph = new Mock(); var targetedFields = new Mock(); - var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph.Object, null); + var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph.Object, null, null); return repository; } diff --git a/test/UnitTests/Services/EntityResourceService_Tests.cs b/test/UnitTests/Services/EntityResourceService_Tests.cs index 4fb8565927..57d90c5198 100644 --- a/test/UnitTests/Services/EntityResourceService_Tests.cs +++ b/test/UnitTests/Services/EntityResourceService_Tests.cs @@ -105,7 +105,7 @@ private DefaultResourceService GetService() _sortService.Object, _sparseFieldsService.Object }; - return new DefaultResourceService(queryParamServices, new JsonApiOptions(), _repositoryMock.Object, _resourceGraph); + return new DefaultResourceService(queryParamServices, new JsonApiOptions(), null, _repositoryMock.Object, _resourceGraph); } } } From de883bd6272395cafb66f0894ecce33388d49f16 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 00:49:36 +0100 Subject: [PATCH 2/6] Fixed broken build after merge --- .../JsonApiDotNetCoreExample/Controllers/TagsController.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs index 5167f317ec..4264e4c29e 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs @@ -10,9 +10,9 @@ public class TagsController : JsonApiController { public TagsController( IJsonApiOptions jsonApiOptions, - IResourceService resourceService, - ILoggerFactory loggerFactory) - : base(jsonApiOptions, resourceService, loggerFactory) + ILoggerFactory loggerFactory, + IResourceService resourceService) + : base(jsonApiOptions, loggerFactory, resourceService) { } } } From 336c5146384d75cc2936348126e4a2f7ae3276e2 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 00:55:19 +0100 Subject: [PATCH 3/6] Fix broken logging configuration After removing all custom log setup, logging started to actually work, in Kestrel Console window and VS Debug Output. Console and Debug writers are added automatically by the runtime. And levels from appsettings.json are also picked up automatically. --- .../Startups/NoDefaultPageSizeStartup.cs | 7 ------- .../JsonApiDotNetCoreExample/Startups/Startup.cs | 15 +++------------ .../JsonApiDotNetCoreExample/appsettings.json | 2 +- src/Examples/NoEntityFrameworkExample/Startup.cs | 7 +------ 4 files changed, 5 insertions(+), 26 deletions(-) diff --git a/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs index 489385f76d..facfd2bb42 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Startups/NoDefaultPageSizeStartup.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using JsonApiDotNetCoreExample.Data; using Microsoft.EntityFrameworkCore; using JsonApiDotNetCore.Extensions; @@ -18,14 +17,8 @@ public NoDefaultPageSizeStartup(IWebHostEnvironment env) : base(env) { } public override void ConfigureServices(IServiceCollection services) { - var loggerFactory = new LoggerFactory(); var mvcBuilder = services.AddMvcCore(); services - .AddSingleton(loggerFactory) - .AddLogging(builder => - { - builder.AddConsole(); - }) .AddDbContext(options => options.UseNpgsql(GetDbConnectionString()), ServiceLifetime.Transient) .AddJsonApi(options => { options.Namespace = "api/v1"; diff --git a/src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs b/src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs index 5f9c4c979c..cc5341026a 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs @@ -2,12 +2,10 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using JsonApiDotNetCoreExample.Data; using Microsoft.EntityFrameworkCore; using JsonApiDotNetCore.Extensions; using System; -using Microsoft.Extensions.Logging.Debug; namespace JsonApiDotNetCoreExample { @@ -27,19 +25,12 @@ public Startup(IWebHostEnvironment env) public virtual void ConfigureServices(IServiceCollection services) { - var loggerFactory = new LoggerFactory(); services - .AddSingleton(loggerFactory) - .AddLogging(builder => - { - builder.AddConsole(); - builder.AddConfiguration(Config.GetSection("Logging")); - }) .AddDbContext(options => { - options.UseLoggerFactory(new LoggerFactory(new[] { new DebugLoggerProvider() })) - .EnableSensitiveDataLogging() - .UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6))); + options + .EnableSensitiveDataLogging() + .UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6))); }, ServiceLifetime.Transient) .AddJsonApi(options => { diff --git a/src/Examples/JsonApiDotNetCoreExample/appsettings.json b/src/Examples/JsonApiDotNetCoreExample/appsettings.json index ef76af7eee..58ef17a7a4 100644 --- a/src/Examples/JsonApiDotNetCoreExample/appsettings.json +++ b/src/Examples/JsonApiDotNetCoreExample/appsettings.json @@ -5,7 +5,7 @@ "Logging": { "IncludeScopes": false, "LogLevel": { - "Default": "Warning", + "Default": "Debug", "System": "Warning", "Microsoft": "Warning", "Microsoft.EntityFrameworkCore": "Debug" diff --git a/src/Examples/NoEntityFrameworkExample/Startup.cs b/src/Examples/NoEntityFrameworkExample/Startup.cs index f21f59c7ba..d9d27f9b00 100644 --- a/src/Examples/NoEntityFrameworkExample/Startup.cs +++ b/src/Examples/NoEntityFrameworkExample/Startup.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using NoEntityFrameworkExample.Services; using Microsoft.EntityFrameworkCore; using NoEntityFrameworkExample.Data; @@ -31,11 +30,7 @@ public virtual void ConfigureServices(IServiceCollection services) { // Add framework services. var mvcBuilder = services.AddMvcCore(); - services.AddLogging(builder => - { - builder.AddConfiguration(Configuration.GetSection("Logging")); - builder.AddConsole(); - }).AddJsonApi( + services.AddJsonApi( options => options.Namespace = "api/v1", resources: resources => resources.AddResource("todoItems"), mvcBuilder: mvcBuilder From 1a0ea3e5f52288b21d838c8be2c98378975c6719 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 00:56:21 +0100 Subject: [PATCH 4/6] Fixed: do not create logger when no factory is available. --- src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs index 6f8db796e8..194da0f68e 100644 --- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs @@ -53,9 +53,7 @@ protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory l _updateRelationships = updateRelationships; _delete = delete; - _logger = loggerFactory != null - ? loggerFactory.CreateLogger>() - : new Logger>(new LoggerFactory()); + _logger = loggerFactory?.CreateLogger>(); } public virtual async Task GetAsync() From 605b08993ec92798e38d5eac3d24db4dc042ff45 Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 16:07:08 +0100 Subject: [PATCH 5/6] Revert style changes --- .../Controllers/ArticlesController.cs | 7 ++- .../Controllers/PeopleController.cs | 7 ++- .../ModelsController.cs | 7 ++- .../Controllers/ArticlesController.cs | 7 ++- .../Controllers/CamelCasedModelsController.cs | 7 ++- .../Controllers/PassportsController.cs | 7 ++- .../Controllers/PeopleController.cs | 7 ++- .../Controllers/PersonRolesController.cs | 7 ++- .../Controllers/TagsController.cs | 2 +- .../Controllers/TodoCollectionsController.cs | 8 ++- .../Controllers/TodoItemsController.cs | 7 ++- .../Controllers/TodoItemsTestController.cs | 14 +++-- .../Controllers/UsersController.cs | 14 +++-- .../Services/CustomArticleService.cs | 13 ++-- .../Controllers/TodoItemsController.cs | 7 ++- .../Controllers/ReportsController.cs | 7 ++- .../Controllers/BaseJsonApiController.cs | 63 ++++++++++++------- .../Controllers/JsonApiCommandController.cs | 14 +++-- .../Controllers/JsonApiController.cs | 46 ++++++++------ .../Controllers/JsonApiQueryController.cs | 13 ++-- .../Data/DefaultResourceRepository.cs | 14 +++-- .../Services/DefaultResourceService.cs | 23 ++++--- .../ServiceDiscoveryFacadeTests.cs | 13 ++-- .../BaseJsonApiController_Tests.cs | 20 +++--- 24 files changed, 199 insertions(+), 135 deletions(-) diff --git a/src/Examples/GettingStarted/Controllers/ArticlesController.cs b/src/Examples/GettingStarted/Controllers/ArticlesController.cs index dd68a1f924..fec9d3e38d 100644 --- a/src/Examples/GettingStarted/Controllers/ArticlesController.cs +++ b/src/Examples/GettingStarted/Controllers/ArticlesController.cs @@ -8,10 +8,11 @@ namespace GettingStarted { public class ArticlesController : JsonApiController
{ - public ArticlesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public ArticlesController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService
resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/GettingStarted/Controllers/PeopleController.cs b/src/Examples/GettingStarted/Controllers/PeopleController.cs index 61712d19f7..68ff5932ea 100644 --- a/src/Examples/GettingStarted/Controllers/PeopleController.cs +++ b/src/Examples/GettingStarted/Controllers/PeopleController.cs @@ -8,10 +8,11 @@ namespace GettingStarted { public class PeopleController : JsonApiController { - public PeopleController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public PeopleController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs b/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs index d5a5002451..52478823f8 100644 --- a/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs +++ b/src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs @@ -7,10 +7,11 @@ namespace GettingStarted.ResourceDefinitionExample { public class ModelsController : JsonApiController { - public ModelsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public ModelsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs index 12f99b9651..ab64bf2c45 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/ArticlesController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class ArticlesController : JsonApiController
{ - public ArticlesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public ArticlesController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService
resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs index 8aa0fca727..588b474c28 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/CamelCasedModelsController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class KebabCasedModelsController : JsonApiController { - public KebabCasedModelsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public KebabCasedModelsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs index 78599939bf..1f38c6aee6 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PassportsController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PassportsController : JsonApiController { - public PassportsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public PassportsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs index b8191708f0..85a00aaa02 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PeopleController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PeopleController : JsonApiController { - public PeopleController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public PeopleController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs index a8aa6348be..f11e2cee5a 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/PersonRolesController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class PersonRolesController : JsonApiController { - public PersonRolesController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public PersonRolesController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs index 4264e4c29e..24a5de6931 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TagsController.cs @@ -11,7 +11,7 @@ public class TagsController : JsonApiController public TagsController( IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IResourceService resourceService) + IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) { } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs index 0af7bc11e5..8bf3a95539 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoCollectionsController.cs @@ -4,7 +4,6 @@ using JsonApiDotNetCore.Configuration; using JsonApiDotNetCore.Controllers; using JsonApiDotNetCore.Data; -using JsonApiDotNetCore.Internal.Contracts; using JsonApiDotNetCore.Services; using JsonApiDotNetCoreExample.Models; using Microsoft.AspNetCore.Mvc; @@ -17,8 +16,11 @@ public class TodoCollectionsController : JsonApiController resourceService) + public TodoCollectionsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IDbContextResolver contextResolver, + IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) { _dbResolver = contextResolver; diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs index bc7cceabf2..ea8dbf8359 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsController.cs @@ -8,10 +8,11 @@ namespace JsonApiDotNetCoreExample.Controllers { public class TodoItemsController : JsonApiController { - public TodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public TodoItemsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs index 884af5d275..379b1dd2ba 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/TodoItemsTestController.cs @@ -11,20 +11,22 @@ namespace JsonApiDotNetCoreExample.Controllers public abstract class AbstractTodoItemsController : JsonApiController where T : class, IIdentifiable { - protected AbstractTodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + protected AbstractTodoItemsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService service) : base(jsonApiOptions, loggerFactory, service) - { - } + { } } [Route("/abstract")] public class TodoItemsTestController : AbstractTodoItemsController { - public TodoItemsTestController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public TodoItemsTestController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService service) : base(jsonApiOptions, loggerFactory, service) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs b/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs index 93a143c6e8..e96bed2517 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Controllers/UsersController.cs @@ -8,19 +8,21 @@ namespace JsonApiDotNetCoreExample.Controllers { public class UsersController : JsonApiController { - public UsersController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public UsersController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } public class SuperUsersController : JsonApiController { - public SuperUsersController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public SuperUsersController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs b/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs index 9ba344a4f5..8b7d07a15b 100644 --- a/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs +++ b/src/Examples/JsonApiDotNetCoreExample/Services/CustomArticleService.cs @@ -14,12 +14,15 @@ namespace JsonApiDotNetCoreExample.Services { public class CustomArticleService : DefaultResourceService
{ - public CustomArticleService(IEnumerable queryParameters, IJsonApiOptions options, - ILoggerFactory loggerFactory, IResourceRepository repository, - IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + public CustomArticleService( + IEnumerable queryParameters, + IJsonApiOptions options, + ILoggerFactory loggerFactory, + IResourceRepository repository, + IResourceContextProvider provider, + IResourceHookExecutor hookExecutor = null) : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) - { - } + { } public override async Task
GetAsync(int id) { diff --git a/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs b/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs index 1d097def78..c21ee5a837 100644 --- a/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs +++ b/src/Examples/NoEntityFrameworkExample/Controllers/TodoItemsController.cs @@ -8,10 +8,11 @@ namespace NoEntityFrameworkExample.Controllers { public class TodoItemsController : JsonApiController { - public TodoItemsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public TodoItemsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } } } diff --git a/src/Examples/ReportsExample/Controllers/ReportsController.cs b/src/Examples/ReportsExample/Controllers/ReportsController.cs index 4fcd903f38..59c985b910 100644 --- a/src/Examples/ReportsExample/Controllers/ReportsController.cs +++ b/src/Examples/ReportsExample/Controllers/ReportsController.cs @@ -11,11 +11,12 @@ namespace ReportsExample.Controllers [Route("api/[controller]")] public class ReportsController : BaseJsonApiController { - public ReportsController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public ReportsController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IGetAllService getAll) : base(jsonApiOptions, loggerFactory, getAll) - { - } + { } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs index fbb320164e..cf0af834ff 100644 --- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs @@ -22,25 +22,33 @@ public abstract class BaseJsonApiController : JsonApiControllerMixin whe private readonly IDeleteService _delete; private readonly ILogger> _logger; - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : this(jsonApiOptions, loggerFactory, resourceService, resourceService, resourceService, resourceService, resourceService, resourceService, resourceService, resourceService) - { - } + { } - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IResourceQueryService queryService = null, IResourceCommandService commandService = null) + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IResourceQueryService queryService = null, + IResourceCommandService commandService = null) : this(jsonApiOptions, loggerFactory, queryService, queryService, queryService, queryService, commandService, commandService, commandService, commandService) - { - } + { } - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IGetAllService getAll = null, IGetByIdService getById = null, + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IGetAllService getAll = null, + IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, ICreateService create = null, - IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IGetRelationshipsService getRelationships = null, + ICreateService create = null, + IUpdateService update = null, + IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) { _jsonApiOptions = jsonApiOptions; @@ -159,27 +167,34 @@ public virtual async Task DeleteAsync(TId id) public abstract class BaseJsonApiController : BaseJsonApiController where T : class, IIdentifiable { - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService, resourceService) - { - } + { } - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IResourceQueryService queryService = null, IResourceCommandService commandService = null) + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IResourceQueryService queryService = null, + IResourceCommandService commandService = null) : base(jsonApiOptions, loggerFactory, queryService, commandService) - { - } + { } - protected BaseJsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IGetAllService getAll = null, IGetByIdService getById = null, + protected BaseJsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IGetAllService getAll = null, + IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, ICreateService create = null, - IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IGetRelationshipsService getRelationships = null, + ICreateService create = null, + IUpdateService update = null, + IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) - { - } + { } } } diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiCommandController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiCommandController.cs index 74f26a5bce..088dc31acc 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiCommandController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiCommandController.cs @@ -9,20 +9,22 @@ namespace JsonApiDotNetCore.Controllers { public class JsonApiCommandController : JsonApiCommandController where T : class, IIdentifiable { - public JsonApiCommandController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public JsonApiCommandController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceCommandService commandService) : base(jsonApiOptions, loggerFactory, commandService) - { - } + { } } public class JsonApiCommandController : BaseJsonApiController where T : class, IIdentifiable { - public JsonApiCommandController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public JsonApiCommandController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceCommandService commandService) : base(jsonApiOptions, loggerFactory, null, commandService) - { - } + { } [HttpPost] public override async Task PostAsync([FromBody] T entity) diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs index f023b76e80..73877ca996 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiController.cs @@ -9,22 +9,27 @@ namespace JsonApiDotNetCore.Controllers { public class JsonApiController : BaseJsonApiController where T : class, IIdentifiable { - public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public JsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } - public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IGetAllService getAll = null, IGetByIdService getById = null, + public JsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IGetAllService getAll = null, + IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, ICreateService create = null, - IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IGetRelationshipsService getRelationships = null, + ICreateService create = null, + IUpdateService update = null, + IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) - { - } + { } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); @@ -61,21 +66,26 @@ public override async Task PatchRelationshipsAsync( public class JsonApiController : JsonApiController where T : class, IIdentifiable { - public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public JsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } - public JsonApiController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IGetAllService getAll = null, IGetByIdService getById = null, + public JsonApiController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IGetAllService getAll = null, + IGetByIdService getById = null, IGetRelationshipService getRelationship = null, - IGetRelationshipsService getRelationships = null, ICreateService create = null, - IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, + IGetRelationshipsService getRelationships = null, + ICreateService create = null, + IUpdateService update = null, + IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) - { - } + { } } } diff --git a/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs b/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs index 96470f9531..d312026804 100644 --- a/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs +++ b/src/JsonApiDotNetCore/Controllers/JsonApiQueryController.cs @@ -9,21 +9,22 @@ namespace JsonApiDotNetCore.Controllers { public class JsonApiQueryController : JsonApiQueryController where T : class, IIdentifiable { - public JsonApiQueryController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public JsonApiQueryController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceQueryService queryService) : base(jsonApiOptions, loggerFactory, queryService) - { - } + { } } public class JsonApiQueryController : BaseJsonApiController where T : class, IIdentifiable { - public JsonApiQueryController(IJsonApiOptions jsonApiContext, + public JsonApiQueryController( + IJsonApiOptions jsonApiContext, ILoggerFactory loggerFactory, IResourceQueryService queryService) : base(jsonApiContext, loggerFactory, queryService) - { - } + { } [HttpGet] public override async Task GetAsync() => await base.GetAsync(); diff --git a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs index 6fff5d711e..61c59c1498 100644 --- a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs +++ b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs @@ -407,11 +407,13 @@ private IIdentifiable AttachOrGetTracked(IIdentifiable relationshipValue) public class DefaultResourceRepository : DefaultResourceRepository, IResourceRepository where TResource : class, IIdentifiable { - public DefaultResourceRepository(ITargetedFields targetedFields, - IDbContextResolver contextResolver, - IResourceGraph resourceGraph, - IGenericServiceFactory genericServiceFactory, - ILoggerFactory loggerFactory) - : base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, loggerFactory) { } + public DefaultResourceRepository( + ITargetedFields targetedFields, + IDbContextResolver contextResolver, + IResourceGraph resourceGraph, + IGenericServiceFactory genericServiceFactory, + ILoggerFactory loggerFactory) + : base(targetedFields, contextResolver, resourceGraph, genericServiceFactory, loggerFactory) + { } } } diff --git a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs index 11c3d753bc..c3aa21e10c 100644 --- a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs +++ b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs @@ -34,9 +34,13 @@ public class DefaultResourceService : private readonly ISparseFieldsService _sparseFieldsService; private readonly ResourceContext _currentRequestResource; - public DefaultResourceService(IEnumerable queryParameters, IJsonApiOptions options, - ILoggerFactory loggerFactory, IResourceRepository repository, - IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + public DefaultResourceService( + IEnumerable queryParameters, + IJsonApiOptions options, + ILoggerFactory loggerFactory, + IResourceRepository repository, + IResourceContextProvider provider, + IResourceHookExecutor hookExecutor = null) { _includeService = queryParameters.FirstOrDefault(); _sparseFieldsService = queryParameters.FirstOrDefault(); @@ -350,11 +354,14 @@ public class DefaultResourceService : DefaultResourceService where TResource : class, IIdentifiable { - public DefaultResourceService(IEnumerable queryParameters, IJsonApiOptions options, - ILoggerFactory loggerFactory, IResourceRepository repository, - IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + public DefaultResourceService( + IEnumerable queryParameters, + IJsonApiOptions options, + ILoggerFactory loggerFactory, + IResourceRepository repository, + IResourceContextProvider provider, + IResourceHookExecutor hookExecutor = null) : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) - { - } + { } } } diff --git a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs index 858ecbf3eb..8af13c7a99 100644 --- a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs +++ b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs @@ -103,12 +103,15 @@ public class TestModelService : DefaultResourceService { private static IResourceRepository _repo = new Mock>().Object; - public TestModelService(IEnumerable queryParameters, IJsonApiOptions options, - ILoggerFactory loggerFactory, IResourceRepository repository, - IResourceContextProvider provider, IResourceHookExecutor hookExecutor = null) + public TestModelService( + IEnumerable queryParameters, + IJsonApiOptions options, + ILoggerFactory loggerFactory, + IResourceRepository repository, + IResourceContextProvider provider, + IResourceHookExecutor hookExecutor = null) : base(queryParameters, options, loggerFactory, repository, provider, hookExecutor) - { - } + { } } public class TestModelRepository : DefaultResourceRepository diff --git a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs index b441e2ee23..8708e8ae7b 100644 --- a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs +++ b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs @@ -21,23 +21,27 @@ public class Resource : Identifiable public class ResourceController : BaseJsonApiController { - public ResourceController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, + public ResourceController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, IResourceService resourceService) : base(jsonApiOptions, loggerFactory, resourceService) - { - } + { } - public ResourceController(IJsonApiOptions jsonApiOptions, ILoggerFactory loggerFactory, - IGetAllService getAll = null, IGetByIdService getById = null, + public ResourceController( + IJsonApiOptions jsonApiOptions, + ILoggerFactory loggerFactory, + IGetAllService getAll = null, + IGetByIdService getById = null, IGetRelationshipService getRelationship = null, IGetRelationshipsService getRelationships = null, - ICreateService create = null, IUpdateService update = null, + ICreateService create = null, + IUpdateService update = null, IUpdateRelationshipService updateRelationships = null, IDeleteService delete = null) : base(jsonApiOptions, loggerFactory, getAll, getById, getRelationship, getRelationships, create, update, updateRelationships, delete) - { - } + { } } [Fact] From fa666418bdca38dd6235e872ec7e90030d7194fc Mon Sep 17 00:00:00 2001 From: Bart Koelman Date: Thu, 13 Feb 2020 16:50:48 +0100 Subject: [PATCH 6/6] - Changed ILoggerFactory into a required parameter. - Added single trace-level log message to controller, service and repository (to make sure we have no crashing tests because they incorrectly pass null). - Removed conditional logging statement --- .../Controllers/BaseJsonApiController.cs | 3 +- .../Data/DefaultResourceRepository.cs | 4 ++ .../Formatters/JsonApiReader.cs | 8 ++-- .../Formatters/JsonApiWriter.cs | 4 +- .../Middleware/DefaultExceptionFilter.cs | 4 +- .../Services/DefaultResourceService.cs | 12 +++--- .../ServiceDiscoveryFacadeTests.cs | 11 ++++-- .../Data/EntityRepositoryTests.cs | 7 ++-- .../BaseJsonApiController_Tests.cs | 39 ++++++++++--------- .../Data/DefaultEntityRepositoryTest.cs | 3 +- .../ResourceHooks/ResourceHooksTestsSetup.cs | 3 +- .../Services/EntityResourceService_Tests.cs | 3 +- 12 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs index cf0af834ff..5b09836440 100644 --- a/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs +++ b/src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs @@ -52,6 +52,7 @@ protected BaseJsonApiController( IDeleteService delete = null) { _jsonApiOptions = jsonApiOptions; + _logger = loggerFactory.CreateLogger>(); _getAll = getAll; _getById = getById; _getRelationship = getRelationship; @@ -61,7 +62,7 @@ protected BaseJsonApiController( _updateRelationships = updateRelationships; _delete = delete; - _logger = loggerFactory?.CreateLogger>(); + _logger.LogTrace("Executing constructor."); } public virtual async Task GetAsync() diff --git a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs index 61c59c1498..f03e3e4f24 100644 --- a/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs +++ b/src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs @@ -27,6 +27,7 @@ public class DefaultResourceRepository : IResourceRepository _dbSet; private readonly IResourceGraph _resourceGraph; private readonly IGenericServiceFactory _genericServiceFactory; + private ILogger> _logger; public DefaultResourceRepository( ITargetedFields targetedFields, @@ -40,6 +41,9 @@ public DefaultResourceRepository( _genericServiceFactory = genericServiceFactory; _context = contextResolver.GetContext(); _dbSet = _context.Set(); + _logger = loggerFactory.CreateLogger>(); + + _logger.LogTrace("Executing constructor."); } /// diff --git a/src/JsonApiDotNetCore/Formatters/JsonApiReader.cs b/src/JsonApiDotNetCore/Formatters/JsonApiReader.cs index 0bc1e79440..784f16fd0b 100644 --- a/src/JsonApiDotNetCore/Formatters/JsonApiReader.cs +++ b/src/JsonApiDotNetCore/Formatters/JsonApiReader.cs @@ -21,6 +21,8 @@ public JsonApiReader(IJsonApiDeserializer deserializer, { _deserializer = deserializer; _logger = loggerFactory.CreateLogger(); + + _logger.LogTrace("Executing constructor."); } public async Task ReadAsync(InputFormatterContext context) @@ -40,7 +42,7 @@ public async Task ReadAsync(InputFormatterContext context object model = _deserializer.Deserialize(body); if (model == null) { - _logger?.LogError("An error occurred while de-serializing the payload"); + _logger.LogError("An error occurred while de-serializing the payload"); } if (context.HttpContext.Request.Method == "PATCH") { @@ -55,7 +57,7 @@ public async Task ReadAsync(InputFormatterContext context } if (idMissing) { - _logger?.LogError("Payload must include id attribute"); + _logger.LogError("Payload must include id attribute"); throw new JsonApiException(400, "Payload must include id attribute"); } } @@ -63,7 +65,7 @@ public async Task ReadAsync(InputFormatterContext context } catch (Exception ex) { - _logger?.LogError(new EventId(), ex, "An error occurred while de-serializing the payload"); + _logger.LogError(new EventId(), ex, "An error occurred while de-serializing the payload"); context.ModelState.AddModelError(context.ModelName, ex, context.Metadata); return await InputFormatterResult.FailureAsync(); } diff --git a/src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs b/src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs index d264f94365..0b2c615581 100644 --- a/src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs +++ b/src/JsonApiDotNetCore/Formatters/JsonApiWriter.cs @@ -25,6 +25,8 @@ public JsonApiWriter(IJsonApiSerializer serializer, { _serializer = serializer; _logger = loggerFactory.CreateLogger(); + + _logger.LogTrace("Executing constructor."); } public async Task WriteAsync(OutputFormatterWriteContext context) @@ -57,7 +59,7 @@ public async Task WriteAsync(OutputFormatterWriteContext context) } catch (Exception e) { - _logger?.LogError(new EventId(), e, "An error occurred while formatting the response"); + _logger.LogError(new EventId(), e, "An error occurred while formatting the response"); var errors = new ErrorCollection(); errors.Add(new Error(500, e.Message, ErrorMeta.FromException(e))); responseContent = _serializer.Serialize(errors); diff --git a/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs b/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs index 1880d70da5..195e38a9e4 100644 --- a/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs +++ b/src/JsonApiDotNetCore/Middleware/DefaultExceptionFilter.cs @@ -14,12 +14,12 @@ public class DefaultExceptionFilter : ActionFilterAttribute, IExceptionFilter public DefaultExceptionFilter(ILoggerFactory loggerFactory) { - _logger = loggerFactory?.CreateLogger(); + _logger = loggerFactory.CreateLogger(); } public void OnException(ExceptionContext context) { - _logger?.LogError(new EventId(), context.Exception, "An unhandled exception occurred during the request"); + _logger.LogError(new EventId(), context.Exception, "An unhandled exception occurred during the request"); var jsonApiException = JsonApiExceptionFactory.GetException(context.Exception); diff --git a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs index c3aa21e10c..d7eaed4001 100644 --- a/src/JsonApiDotNetCore/Services/DefaultResourceService.cs +++ b/src/JsonApiDotNetCore/Services/DefaultResourceService.cs @@ -48,10 +48,12 @@ public DefaultResourceService( _sortService = queryParameters.FirstOrDefault(); _filterService = queryParameters.FirstOrDefault(); _options = options; + _logger = loggerFactory.CreateLogger>(); _repository = repository; _hookExecutor = hookExecutor; - _logger = loggerFactory?.CreateLogger>(); _currentRequestResource = provider.GetResourceContext(); + + _logger.LogTrace("Executing constructor."); } public virtual async Task CreateAsync(TResource entity) @@ -210,11 +212,9 @@ protected virtual async Task> ApplyPageQueryAsync(IQuerya { pageOffset = -pageOffset; } - if (_logger?.IsEnabled(LogLevel.Information) == true) - { - _logger?.LogInformation($"Applying paging query. Fetching page {pageOffset} " + - $"with {_pageService.CurrentPageSize} entities"); - } + + _logger.LogInformation($"Applying paging query. Fetching page {pageOffset} " + + $"with {_pageService.CurrentPageSize} entities"); return await _repository.PageAsync(entities, _pageService.CurrentPageSize, pageOffset); } diff --git a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs index 8af13c7a99..c4a7ba4644 100644 --- a/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs +++ b/test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs @@ -118,10 +118,13 @@ public class TestModelRepository : DefaultResourceRepository { internal static IDbContextResolver _dbContextResolver; - public TestModelRepository(ITargetedFields targetedFields, - IResourceGraph resourceGraph, - IGenericServiceFactory genericServiceFactory) - : base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory, null) { } + public TestModelRepository( + ITargetedFields targetedFields, + IResourceGraph resourceGraph, + IGenericServiceFactory genericServiceFactory, + ILoggerFactory loggerFactory) + : base(targetedFields, _dbContextResolver, resourceGraph, genericServiceFactory, loggerFactory) + { } } } } diff --git a/test/IntegrationTests/Data/EntityRepositoryTests.cs b/test/IntegrationTests/Data/EntityRepositoryTests.cs index c66bbcaeb0..35dbbe878c 100644 --- a/test/IntegrationTests/Data/EntityRepositoryTests.cs +++ b/test/IntegrationTests/Data/EntityRepositoryTests.cs @@ -1,5 +1,5 @@ -using JsonApiDotNetCore.Builders; -using JsonApiDotNetCore.Data; +using JsonApiDotNetCore.Builders; +using JsonApiDotNetCore.Data; using JsonApiDotNetCore.Models; using JsonApiDotNetCore.Serialization; using JsonApiDotNetCoreExample.Data; @@ -10,6 +10,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging.Abstractions; using Xunit; namespace JADNC.IntegrationTests.Data @@ -154,7 +155,7 @@ public async Task Paging_PageNumberIsNegative_GiveBackReverseAmountOfIds(int pag contextResolverMock.Setup(m => m.GetContext()).Returns(context); var resourceGraph = new ResourceGraphBuilder().AddResource().Build(); var targetedFields = new Mock(); - var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph, null, null); + var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph, null, NullLoggerFactory.Instance); return (repository, targetedFields); } diff --git a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs index 8708e8ae7b..2044ca72dd 100644 --- a/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs +++ b/test/UnitTests/Controllers/BaseJsonApiController_Tests.cs @@ -9,6 +9,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; namespace UnitTests { @@ -49,7 +50,7 @@ public async Task GetAsync_Calls_Service() { // Arrange var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, getAll: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getAll: serviceMock.Object); // Act await controller.GetAsync(); @@ -62,7 +63,7 @@ public async Task GetAsync_Calls_Service() public async Task GetAsync_Throws_405_If_No_Service() { // Arrange - var controller = new ResourceController(new Mock().Object, null, null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetAsync()); @@ -77,7 +78,7 @@ public async Task GetAsyncById_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, getById: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getById: serviceMock.Object); // Act await controller.GetAsync(id); @@ -91,7 +92,7 @@ public async Task GetAsyncById_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, getById: null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getById: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetAsync(id)); @@ -106,7 +107,7 @@ public async Task GetRelationshipsAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, getRelationships: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getRelationships: serviceMock.Object); // Act await controller.GetRelationshipsAsync(id, string.Empty); @@ -120,7 +121,7 @@ public async Task GetRelationshipsAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, getRelationships: null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getRelationships: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetRelationshipsAsync(id, string.Empty)); @@ -135,7 +136,7 @@ public async Task GetRelationshipAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, getRelationship: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getRelationship: serviceMock.Object); // Act await controller.GetRelationshipAsync(id, string.Empty); @@ -149,7 +150,7 @@ public async Task GetRelationshipAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, getRelationship: null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, getRelationship: null); // Act var exception = await Assert.ThrowsAsync(() => controller.GetRelationshipAsync(id, string.Empty)); @@ -166,7 +167,7 @@ public async Task PatchAsync_Calls_Service() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions(), null, update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), NullLoggerFactory.Instance, update: serviceMock.Object); // Act await controller.PatchAsync(id, resource); @@ -182,7 +183,7 @@ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateDisabled() const int id = 0; var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions(), null, update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), NullLoggerFactory.Instance, update: serviceMock.Object); // Act var response = await controller.PatchAsync(id, resource); @@ -200,7 +201,7 @@ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateEnabled() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, null, update: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, NullLoggerFactory.Instance, update: serviceMock.Object); controller.ModelState.AddModelError("TestAttribute", "Failed Validation"); // Act @@ -217,7 +218,7 @@ public async Task PatchAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, update: null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, update: null); // Act var exception = await Assert.ThrowsAsync(() => controller.PatchAsync(id, It.IsAny())); @@ -233,7 +234,7 @@ public async Task PostAsync_Calls_Service() var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions(), null, create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions(), NullLoggerFactory.Instance, create: serviceMock.Object); serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); controller.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext() }; @@ -250,7 +251,7 @@ public async Task PostAsync_ModelStateInvalid_ValidateModelStateDisabled() // Arrange var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions { ValidateModelState = false }, null, create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = false }, NullLoggerFactory.Instance, create: serviceMock.Object); controller.ControllerContext = new Microsoft.AspNetCore.Mvc.ControllerContext { HttpContext = new DefaultHttpContext() }; serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); @@ -268,7 +269,7 @@ public async Task PostAsync_ModelStateInvalid_ValidateModelStateEnabled() // Arrange var resource = new Resource(); var serviceMock = new Mock>(); - var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, null, create: serviceMock.Object); + var controller = new ResourceController(new JsonApiOptions { ValidateModelState = true }, NullLoggerFactory.Instance, create: serviceMock.Object); controller.ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() }; controller.ModelState.AddModelError("TestAttribute", "Failed Validation"); serviceMock.Setup(m => m.CreateAsync(It.IsAny())).ReturnsAsync(resource); @@ -288,7 +289,7 @@ public async Task PatchRelationshipsAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, updateRelationships: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, updateRelationships: serviceMock.Object); // Act await controller.PatchRelationshipsAsync(id, string.Empty, null); @@ -302,7 +303,7 @@ public async Task PatchRelationshipsAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, updateRelationships: null); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, updateRelationships: null); // Act var exception = await Assert.ThrowsAsync(() => controller.PatchRelationshipsAsync(id, string.Empty, null)); @@ -317,7 +318,7 @@ public async Task DeleteAsync_Calls_Service() // Arrange const int id = 0; var serviceMock = new Mock>(); - var controller = new ResourceController(new Mock().Object, null, delete: serviceMock.Object); + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, delete: serviceMock.Object); // Act await controller.DeleteAsync(id); @@ -331,7 +332,7 @@ public async Task DeleteAsync_Throws_405_If_No_Service() { // Arrange const int id = 0; - var controller = new ResourceController(new Mock().Object, null, + var controller = new ResourceController(new Mock().Object, NullLoggerFactory.Instance, delete: null); // Act diff --git a/test/UnitTests/Data/DefaultEntityRepositoryTest.cs b/test/UnitTests/Data/DefaultEntityRepositoryTest.cs index b2487eb628..3c9fca2951 100644 --- a/test/UnitTests/Data/DefaultEntityRepositoryTest.cs +++ b/test/UnitTests/Data/DefaultEntityRepositoryTest.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using Microsoft.Extensions.Logging.Abstractions; using Xunit; namespace UnitTests.Data @@ -66,7 +67,7 @@ private DefaultResourceRepository Setup() contextResolverMock.Setup(m => m.GetContext()).Returns(new Mock().Object); var resourceGraph = new Mock(); var targetedFields = new Mock(); - var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph.Object, null, null); + var repository = new DefaultResourceRepository(targetedFields.Object, contextResolverMock.Object, resourceGraph.Object, null, NullLoggerFactory.Instance); return repository; } diff --git a/test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs b/test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs index 8fa2473c5b..28f35f2c83 100644 --- a/test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs +++ b/test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs @@ -18,6 +18,7 @@ using JsonApiDotNetCore.Serialization; using JsonApiDotNetCore.Internal.Query; using JsonApiDotNetCore.Query; +using Microsoft.Extensions.Logging.Abstractions; namespace UnitTests.ResourceHooks { @@ -344,7 +345,7 @@ AppDbContext dbContext ) where TModel : class, IIdentifiable { IDbContextResolver resolver = CreateTestDbResolver(dbContext); - return new DefaultResourceRepository(null, resolver, null, null, null); + return new DefaultResourceRepository(null, resolver, null, null, NullLoggerFactory.Instance); } IDbContextResolver CreateTestDbResolver(AppDbContext dbContext) where TModel : class, IIdentifiable diff --git a/test/UnitTests/Services/EntityResourceService_Tests.cs b/test/UnitTests/Services/EntityResourceService_Tests.cs index 5e8859e0b2..f0a99df980 100644 --- a/test/UnitTests/Services/EntityResourceService_Tests.cs +++ b/test/UnitTests/Services/EntityResourceService_Tests.cs @@ -10,6 +10,7 @@ using JsonApiDotNetCore.Query; using JsonApiDotNetCore.Services; using JsonApiDotNetCoreExample.Models; +using Microsoft.Extensions.Logging.Abstractions; using Moq; using Xunit; @@ -119,7 +120,7 @@ private DefaultResourceService GetService() _sortService.Object, _sparseFieldsService.Object }; - return new DefaultResourceService(queryParamServices, new JsonApiOptions(), null, _repositoryMock.Object, _resourceGraph); + return new DefaultResourceService(queryParamServices, new JsonApiOptions(), NullLoggerFactory.Instance, _repositoryMock.Object, _resourceGraph); } } }