From dd9f7cc6142950324ed784f5517c6469e7b9c244 Mon Sep 17 00:00:00 2001 From: George Date: Tue, 4 Jun 2019 16:43:31 +0500 Subject: [PATCH] Refactor query context --- .../ContentEntityController.cs | 7 ++++--- .../Controllers/MenuController.cs | 5 ++--- .../Controllers/SectionsController.cs | 10 +++------- .../Controllers/SitesController.cs | 7 +++---- .../Controllers/TagsController.cs | 7 +++---- .../Models/ContentEntityRestModel.cs | 2 +- .../Models/IRequestRestModel.cs | 2 +- .../Models/IResponseRestModel.cs | 2 +- src/BioEngine.Core.API/Models/RestModel.cs | 2 +- .../Models/SectionEntityRestModel.cs | 2 +- .../Models/SectionRestModel.cs | 1 + .../Models/SiteEntityRestModel.cs | 2 +- .../RequestRestController.cs | 20 +++++++++---------- .../ResponseRestController.cs | 16 +++++++-------- src/BioEngine.Core.API/SectionController.cs | 14 ++++++------- src/BioEngine.Core.API/Startup.cs | 2 +- 16 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/BioEngine.Core.API/ContentEntityController.cs b/src/BioEngine.Core.API/ContentEntityController.cs index 1f42947..f152563 100644 --- a/src/BioEngine.Core.API/ContentEntityController.cs +++ b/src/BioEngine.Core.API/ContentEntityController.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using BioEngine.Core.Abstractions; using BioEngine.Core.API.Models; using BioEngine.Core.DB; using BioEngine.Core.Entities; @@ -13,17 +14,17 @@ namespace BioEngine.Core.API { public abstract class ContentEntityController : RequestRestController< - TEntity, ContentEntityQueryContext, TRepository, TResponse, TRequest> + TEntity, TRepository, TResponse, TRequest> where TEntity : class, IContentEntity, IEntity where TResponse : class, IContentResponseRestModel where TRequest : class, IContentRequestRestModel - where TRepository : IContentEntityRepository> + where TRepository : IContentEntityRepository { private readonly ContentBlocksRepository _blocksRepository; protected BioEntitiesManager EntitiesManager { get; } protected ContentEntityController( - BaseControllerContext, TRepository> context, + BaseControllerContext context, BioEntitiesManager entitiesManager, ContentBlocksRepository blocksRepository) : base(context) { _blocksRepository = blocksRepository; diff --git a/src/BioEngine.Core.API/Controllers/MenuController.cs b/src/BioEngine.Core.API/Controllers/MenuController.cs index 9235332..f053111 100644 --- a/src/BioEngine.Core.API/Controllers/MenuController.cs +++ b/src/BioEngine.Core.API/Controllers/MenuController.cs @@ -1,13 +1,12 @@ -using BioEngine.Core.DB; using BioEngine.Core.Entities; using BioEngine.Core.Repository; using BioEngine.Core.Web; namespace BioEngine.Core.API.Controllers { - public class MenuController : ResponseRequestRestController, MenuRepository, Entities.Menu> + public class MenuController : ResponseRequestRestController { - public MenuController(BaseControllerContext, MenuRepository> context) : base(context) + public MenuController(BaseControllerContext context) : base(context) { } } diff --git a/src/BioEngine.Core.API/Controllers/SectionsController.cs b/src/BioEngine.Core.API/Controllers/SectionsController.cs index 7141349..2cf8d8b 100644 --- a/src/BioEngine.Core.API/Controllers/SectionsController.cs +++ b/src/BioEngine.Core.API/Controllers/SectionsController.cs @@ -1,16 +1,12 @@ -using BioEngine.Core.DB; -using BioEngine.Core.Entities; +using BioEngine.Core.Entities; using BioEngine.Core.Repository; using BioEngine.Core.Web; namespace BioEngine.Core.API.Controllers { - public class SectionsController : SectionController, SectionsRepository, - Entities.Section> + public class SectionsController : SectionController { - public SectionsController( - BaseControllerContext, SectionsRepository> context) : - base(context) + public SectionsController(BaseControllerContext context) : base(context) { } } diff --git a/src/BioEngine.Core.API/Controllers/SitesController.cs b/src/BioEngine.Core.API/Controllers/SitesController.cs index f4626b9..bb62e73 100644 --- a/src/BioEngine.Core.API/Controllers/SitesController.cs +++ b/src/BioEngine.Core.API/Controllers/SitesController.cs @@ -1,14 +1,13 @@ -using BioEngine.Core.DB; -using BioEngine.Core.Entities; +using BioEngine.Core.Entities; using BioEngine.Core.Repository; using BioEngine.Core.Web; namespace BioEngine.Core.API.Controllers { public class - SitesController : ResponseRequestRestController, SitesRepository, Entities.Site> + SitesController : ResponseRequestRestController { - public SitesController(BaseControllerContext, SitesRepository> context) : base(context) + public SitesController(BaseControllerContext context) : base(context) { } } diff --git a/src/BioEngine.Core.API/Controllers/TagsController.cs b/src/BioEngine.Core.API/Controllers/TagsController.cs index 7eeef8b..9aa083d 100644 --- a/src/BioEngine.Core.API/Controllers/TagsController.cs +++ b/src/BioEngine.Core.API/Controllers/TagsController.cs @@ -1,13 +1,12 @@ -using BioEngine.Core.DB; -using BioEngine.Core.Entities; +using BioEngine.Core.Entities; using BioEngine.Core.Repository; using BioEngine.Core.Web; namespace BioEngine.Core.API.Controllers { - public class TagsController : ResponseRequestRestController, TagsRepository, Entities.Tag> + public class TagsController : ResponseRequestRestController { - public TagsController(BaseControllerContext, TagsRepository> context) : base(context) + public TagsController(BaseControllerContext context) : base(context) { } } diff --git a/src/BioEngine.Core.API/Models/ContentEntityRestModel.cs b/src/BioEngine.Core.API/Models/ContentEntityRestModel.cs index cd279cf..2686a54 100644 --- a/src/BioEngine.Core.API/Models/ContentEntityRestModel.cs +++ b/src/BioEngine.Core.API/Models/ContentEntityRestModel.cs @@ -1,6 +1,6 @@ using System; using System.Threading.Tasks; -using BioEngine.Core.Entities; +using BioEngine.Core.Abstractions; namespace BioEngine.Core.API.Models { diff --git a/src/BioEngine.Core.API/Models/IRequestRestModel.cs b/src/BioEngine.Core.API/Models/IRequestRestModel.cs index 822e8b6..32014cf 100644 --- a/src/BioEngine.Core.API/Models/IRequestRestModel.cs +++ b/src/BioEngine.Core.API/Models/IRequestRestModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; -using BioEngine.Core.Entities; +using BioEngine.Core.Abstractions; using ContentBlock = BioEngine.Core.API.Entities.ContentBlock; namespace BioEngine.Core.API.Models diff --git a/src/BioEngine.Core.API/Models/IResponseRestModel.cs b/src/BioEngine.Core.API/Models/IResponseRestModel.cs index 684498f..9d858ea 100644 --- a/src/BioEngine.Core.API/Models/IResponseRestModel.cs +++ b/src/BioEngine.Core.API/Models/IResponseRestModel.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; using System.Threading.Tasks; -using BioEngine.Core.Entities; +using BioEngine.Core.Abstractions; using ContentBlock = BioEngine.Core.API.Entities.ContentBlock; namespace BioEngine.Core.API.Models diff --git a/src/BioEngine.Core.API/Models/RestModel.cs b/src/BioEngine.Core.API/Models/RestModel.cs index a2f1054..d6881fc 100644 --- a/src/BioEngine.Core.API/Models/RestModel.cs +++ b/src/BioEngine.Core.API/Models/RestModel.cs @@ -2,8 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using BioEngine.Core.Abstractions; using BioEngine.Core.API.Entities; -using BioEngine.Core.Entities; using BioEngine.Core.Properties; using Newtonsoft.Json; diff --git a/src/BioEngine.Core.API/Models/SectionEntityRestModel.cs b/src/BioEngine.Core.API/Models/SectionEntityRestModel.cs index a046b41..272065a 100644 --- a/src/BioEngine.Core.API/Models/SectionEntityRestModel.cs +++ b/src/BioEngine.Core.API/Models/SectionEntityRestModel.cs @@ -1,6 +1,6 @@ using System; using System.Threading.Tasks; -using BioEngine.Core.Entities; +using BioEngine.Core.Abstractions; namespace BioEngine.Core.API.Models { diff --git a/src/BioEngine.Core.API/Models/SectionRestModel.cs b/src/BioEngine.Core.API/Models/SectionRestModel.cs index 43ac840..e1f284d 100644 --- a/src/BioEngine.Core.API/Models/SectionRestModel.cs +++ b/src/BioEngine.Core.API/Models/SectionRestModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using BioEngine.Core.Abstractions; using BioEngine.Core.Entities; namespace BioEngine.Core.API.Models diff --git a/src/BioEngine.Core.API/Models/SiteEntityRestModel.cs b/src/BioEngine.Core.API/Models/SiteEntityRestModel.cs index 0e8a9f5..e2c4e3c 100644 --- a/src/BioEngine.Core.API/Models/SiteEntityRestModel.cs +++ b/src/BioEngine.Core.API/Models/SiteEntityRestModel.cs @@ -1,6 +1,6 @@ using System; using System.Threading.Tasks; -using BioEngine.Core.Entities; +using BioEngine.Core.Abstractions; namespace BioEngine.Core.API.Models { diff --git a/src/BioEngine.Core.API/RequestRestController.cs b/src/BioEngine.Core.API/RequestRestController.cs index 2ec3f5f..bbca945 100644 --- a/src/BioEngine.Core.API/RequestRestController.cs +++ b/src/BioEngine.Core.API/RequestRestController.cs @@ -1,9 +1,9 @@ using System; using System.Linq; using System.Threading.Tasks; +using BioEngine.Core.Abstractions; using BioEngine.Core.API.Models; using BioEngine.Core.API.Response; -using BioEngine.Core.DB; using BioEngine.Core.Entities; using BioEngine.Core.Repository; using BioEngine.Core.Web; @@ -13,16 +13,15 @@ namespace BioEngine.Core.API { public abstract class - RequestRestController : ResponseRestController : ResponseRestController where TEntity : class, IEntity where TResponse : class, IResponseRestModel where TRequest : class, IRequestRestModel - where TQueryContext : QueryContext, new() - where TRepository : IBioRepository + where TRepository : IBioRepository { - protected RequestRestController(BaseControllerContext context) : + protected RequestRestController(BaseControllerContext context) : base(context) { } @@ -131,14 +130,13 @@ protected virtual Task AfterDeleteAsync(TEntity domainModel) } public abstract class - ResponseRequestRestController : - RequestRestController + ResponseRequestRestController : + RequestRestController where TEntity : class, IEntity where TRequestResponse : class, IResponseRestModel, IRequestRestModel - where TQueryContext : QueryContext, new() - where TRepository : IBioRepository + where TRepository : IBioRepository { - protected ResponseRequestRestController(BaseControllerContext context) : + protected ResponseRequestRestController(BaseControllerContext context) : base(context) { } diff --git a/src/BioEngine.Core.API/ResponseRestController.cs b/src/BioEngine.Core.API/ResponseRestController.cs index c874218..34e9d6f 100644 --- a/src/BioEngine.Core.API/ResponseRestController.cs +++ b/src/BioEngine.Core.API/ResponseRestController.cs @@ -3,12 +3,11 @@ using System.Text; using System.Threading.Tasks; using System.Web; +using BioEngine.Core.Abstractions; using BioEngine.Core.API.Interfaces; using BioEngine.Core.API.Models; using BioEngine.Core.API.Response; -using BioEngine.Core.DB; -using BioEngine.Core.Entities; -using BioEngine.Core.Repository; +using BioEngine.Core.DB.Queries; using BioEngine.Core.Web; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -17,15 +16,14 @@ namespace BioEngine.Core.API { - public abstract class ResponseRestController : ApiController + public abstract class ResponseRestController : ApiController where TResponse : IResponseRestModel where TEntity : class, IEntity - where TQueryContext : QueryContext, new() - where TRepository : IBioRepository + where TRepository : IBioRepository { protected TRepository Repository { get; } - protected ResponseRestController(BaseControllerContext context) : base(context) + protected ResponseRestController(BaseControllerContext context) : base(context) { Repository = context.Repository; } @@ -72,9 +70,9 @@ public virtual async Task> CountAsync([FromQuery] int limit = return Ok(result); } - protected TQueryContext GetQueryContext(int limit, int offset, string order, string filter) + protected IQueryContext GetQueryContext(int limit, int offset, string order, string filter) { - var context = new TQueryContext(); + var context = HttpContext.RequestServices.GetRequiredService>(); if (limit > 0) { context.Limit = limit; diff --git a/src/BioEngine.Core.API/SectionController.cs b/src/BioEngine.Core.API/SectionController.cs index b828fec..5b16f65 100644 --- a/src/BioEngine.Core.API/SectionController.cs +++ b/src/BioEngine.Core.API/SectionController.cs @@ -1,5 +1,6 @@ using System.IO; using System.Threading.Tasks; +using BioEngine.Core.Abstractions; using BioEngine.Core.API.Models; using BioEngine.Core.DB; using BioEngine.Core.Entities; @@ -18,9 +19,9 @@ public abstract class where TData : ITypedData, new() where TResponse : class, IContentResponseRestModel where TRequest : SectionRestModel, IContentRequestRestModel - where TRepository : IContentEntityRepository> + where TRepository : IContentEntityRepository { - protected SectionController(BaseControllerContext, TRepository> context, + protected SectionController(BaseControllerContext context, BioEntitiesManager entitiesManager, ContentBlocksRepository blocksRepository) : base(context, entitiesManager, blocksRepository) { @@ -37,14 +38,13 @@ public override async Task> UploadAsync([FromQuery] st } public abstract class - SectionController : ResponseRestController + SectionController : ResponseRestController where TEntity : Section, IEntity where TResponse : IResponseRestModel - where TRepository : IBioRepository - where TQueryContext : QueryContext, new() + where TRepository : IBioRepository { - protected SectionController(BaseControllerContext context) : base(context) + protected SectionController(BaseControllerContext context) : base(context) { } } diff --git a/src/BioEngine.Core.API/Startup.cs b/src/BioEngine.Core.API/Startup.cs index 9b5a6aa..dd5c8f2 100644 --- a/src/BioEngine.Core.API/Startup.cs +++ b/src/BioEngine.Core.API/Startup.cs @@ -20,7 +20,7 @@ protected BioEngineApiStartup(IConfiguration configuration, IHostEnvironment hos protected override IMvcBuilder ConfigureMvc(IMvcBuilder mvcBuilder) { - return base.ConfigureMvc(mvcBuilder).AddApplicationPart(typeof(ResponseRestController<,,,>).Assembly); + return base.ConfigureMvc(mvcBuilder).AddApplicationPart(typeof(ResponseRestController<,,>).Assembly); } public override void ConfigureServices(IServiceCollection services)