From f9badf98f4be5591f99d97b299b8559f8933e8bf Mon Sep 17 00:00:00 2001 From: Giovani Date: Tue, 23 Apr 2019 22:52:58 +0000 Subject: [PATCH] Refactored repositories and fixed service injections --- .../Controllers/CourseController.cs | 6 +-- .../Controllers/StudentController.cs | 38 +++---------------- BrightGlimmer.Api/Program.cs | 9 +---- BrightGlimmer.Api/Startup.cs | 29 +++++++------- .../BrightGlimmer.Services.csproj | 2 +- BrightGlimmer.CQRS/Cqrs.cs | 1 + .../GetAllStudentsQueryHandler.cs | 7 +--- .../Queries/GetAllStudentsQuery.cs | 2 - BrightGlimmer.Data/BgContext.cs | 3 +- .../{IRepository.cs => ICommandRepository.cs} | 2 +- .../Interfaces/IQueryRepository.cs | 16 ++++++++ .../20190420001348_CreateDatabase.Designer.cs | 2 +- .../{Repository.cs => CommandRepository.cs} | 6 +-- .../Repositories/QueryRepository.cs | 31 +++++++++++++++ ...epository.cs => StudentQueryRepository.cs} | 4 +- 15 files changed, 80 insertions(+), 78 deletions(-) rename BrightGlimmer.Data/Interfaces/{IRepository.cs => ICommandRepository.cs} (88%) create mode 100644 BrightGlimmer.Data/Interfaces/IQueryRepository.cs rename BrightGlimmer.Data/Repositories/{Repository.cs => CommandRepository.cs} (80%) create mode 100644 BrightGlimmer.Data/Repositories/QueryRepository.cs rename BrightGlimmer.Data/Repositories/{StudentRepository.cs => StudentQueryRepository.cs} (64%) diff --git a/BrightGlimmer.Api/Controllers/CourseController.cs b/BrightGlimmer.Api/Controllers/CourseController.cs index c010e33..6bc8634 100644 --- a/BrightGlimmer.Api/Controllers/CourseController.cs +++ b/BrightGlimmer.Api/Controllers/CourseController.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using MediatR; +using MediatR; using Microsoft.AspNetCore.Mvc; namespace BrightGlimmer.Api.Controllers diff --git a/BrightGlimmer.Api/Controllers/StudentController.cs b/BrightGlimmer.Api/Controllers/StudentController.cs index ab882b3..0a9cc9d 100644 --- a/BrightGlimmer.Api/Controllers/StudentController.cs +++ b/BrightGlimmer.Api/Controllers/StudentController.cs @@ -1,7 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; +using BrightGlimmer.Services.Queries; using MediatR; using Microsoft.AspNetCore.Mvc; @@ -19,39 +17,15 @@ namespace BrightGlimmer.Api.Controllers } [HttpGet] - public IActionResult GetAll() + public async Task GetAll() { - var customers = mediator.GetAll(); - if (customers == null) + var students = await mediator.Send(new GetAllStudentsQuery()); + if (students == null) { return NotFound(); } - return new ObjectResult(customers); + return new JsonResult(students); } - - //[HttpGet("createstudent")] - //public IActionResult CreateStudent() - //{ - // var created = studentRepository.Create(new Data.Domain.Student - // { - // Id = Guid.NewGuid(), - // FirstName = "Giovani", - // LastName = "Rodriguez", - // Email = "giovaniluisrodriguez@gmail.com", - // Phones = new List - // { - // new Data.Domain.Phone - // { - // Id = Guid.NewGuid(), - // AreaCode = 305, - // Number = 8888888, - // Type = Data.Domain.PhoneType.HOMEPHONE - // } - // } - // }); - - // return new ObjectResult(created); - //} } } \ No newline at end of file diff --git a/BrightGlimmer.Api/Program.cs b/BrightGlimmer.Api/Program.cs index 023bf73..b41bb9b 100644 --- a/BrightGlimmer.Api/Program.cs +++ b/BrightGlimmer.Api/Program.cs @@ -1,12 +1,5 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore; +using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Logging; namespace BrightGlimmer.Api { diff --git a/BrightGlimmer.Api/Startup.cs b/BrightGlimmer.Api/Startup.cs index e051c98..a339566 100644 --- a/BrightGlimmer.Api/Startup.cs +++ b/BrightGlimmer.Api/Startup.cs @@ -1,20 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using BrightGlimmer.Data; +using BrightGlimmer.Data; using BrightGlimmer.Data.Interfaces; -using BrightGlimmer.Data.Repositories; /* REMOVE LATER */ +using BrightGlimmer.Data.Repositories; using MediatR; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; namespace BrightGlimmer.Api { @@ -31,11 +24,15 @@ namespace BrightGlimmer.Api public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); + services.AddMediatR(); services.AddMediatR(typeof(Cqrs.Cqrs).Assembly); // Registers handlers in services project - services.AddScoped(typeof(IRepository<>)); /* FIGURE OUT LATER */ + + services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>)); + services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>)); + services.AddDbContext(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"))); - services.AddTransient(); + services.AddTransient(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -54,11 +51,11 @@ namespace BrightGlimmer.Api app.UseMvc(); // Makes sure that the database is in fact created - using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) - { - var context = serviceScope.ServiceProvider.GetRequiredService(); - context.Database.EnsureCreated(); - } + //using (var serviceScope = app.ApplicationServices.GetService().CreateScope()) + //{ + // var context = serviceScope.ServiceProvider.GetRequiredService(); + // context.Database.EnsureCreated(); + //} } } } \ No newline at end of file diff --git a/BrightGlimmer.CQRS/BrightGlimmer.Services.csproj b/BrightGlimmer.CQRS/BrightGlimmer.Services.csproj index 90d0190..ef92186 100644 --- a/BrightGlimmer.CQRS/BrightGlimmer.Services.csproj +++ b/BrightGlimmer.CQRS/BrightGlimmer.Services.csproj @@ -5,7 +5,7 @@ - + diff --git a/BrightGlimmer.CQRS/Cqrs.cs b/BrightGlimmer.CQRS/Cqrs.cs index 1fa640b..be8c3b1 100644 --- a/BrightGlimmer.CQRS/Cqrs.cs +++ b/BrightGlimmer.CQRS/Cqrs.cs @@ -4,5 +4,6 @@ using System.Text; namespace BrightGlimmer.Cqrs { + /* USED TO GET ASSEMBLY IN STARTUP.CS */ public class Cqrs { } } diff --git a/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs b/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs index 0f50fc2..f551612 100644 --- a/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs +++ b/BrightGlimmer.CQRS/Handlers/QueryHandlers/GetAllStudentsQueryHandler.cs @@ -3,10 +3,7 @@ using BrightGlimmer.Domain; using BrightGlimmer.Services.Queries; using MediatR; using Microsoft.EntityFrameworkCore; /* TODO */ -using System; using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -14,9 +11,9 @@ namespace BrightGlimmer.Services.Handlers.QueryHandlers { public class GetAllStudentsQueryHandler : IRequestHandler> { - private readonly IRepository repository; + private readonly IQueryRepository repository; - public GetAllStudentsQueryHandler(IRepository repository) + public GetAllStudentsQueryHandler(IQueryRepository repository) { this.repository = repository; } diff --git a/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs b/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs index fe3c1e5..8f341f8 100644 --- a/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs +++ b/BrightGlimmer.CQRS/Queries/GetAllStudentsQuery.cs @@ -1,8 +1,6 @@ using BrightGlimmer.Domain; using MediatR; -using System; using System.Collections.Generic; -using System.Text; namespace BrightGlimmer.Services.Queries { diff --git a/BrightGlimmer.Data/BgContext.cs b/BrightGlimmer.Data/BgContext.cs index 1a02aac..d560f73 100644 --- a/BrightGlimmer.Data/BgContext.cs +++ b/BrightGlimmer.Data/BgContext.cs @@ -1,5 +1,4 @@ -using BrightGlimmer.Data.Domain; -using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Domain; using Microsoft.EntityFrameworkCore; using System; diff --git a/BrightGlimmer.Data/Interfaces/IRepository.cs b/BrightGlimmer.Data/Interfaces/ICommandRepository.cs similarity index 88% rename from BrightGlimmer.Data/Interfaces/IRepository.cs rename to BrightGlimmer.Data/Interfaces/ICommandRepository.cs index 446319a..4877cc9 100644 --- a/BrightGlimmer.Data/Interfaces/IRepository.cs +++ b/BrightGlimmer.Data/Interfaces/ICommandRepository.cs @@ -5,7 +5,7 @@ using System.Text; namespace BrightGlimmer.Data.Interfaces { - public interface IRepository + public interface ICommandRepository { IUnitOfWork UnitOfWork { get; } diff --git a/BrightGlimmer.Data/Interfaces/IQueryRepository.cs b/BrightGlimmer.Data/Interfaces/IQueryRepository.cs new file mode 100644 index 0000000..58bb819 --- /dev/null +++ b/BrightGlimmer.Data/Interfaces/IQueryRepository.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BrightGlimmer.Data.Interfaces +{ + public interface IQueryRepository + { + IUnitOfWork UnitOfWork { get; } + + IQueryable Get(); + + T Get(Guid id); + } +} diff --git a/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs index 403b2f5..d4256a1 100644 --- a/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs +++ b/BrightGlimmer.Data/Migrations/20190420001348_CreateDatabase.Designer.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace BrightGlimmer.Data.Migrations { - [DbContext(typeof(SqliteContext))] + [DbContext(typeof(BgContext))] [Migration("20190420001348_CreateDatabase")] partial class CreateDatabase { diff --git a/BrightGlimmer.Data/Repositories/Repository.cs b/BrightGlimmer.Data/Repositories/CommandRepository.cs similarity index 80% rename from BrightGlimmer.Data/Repositories/Repository.cs rename to BrightGlimmer.Data/Repositories/CommandRepository.cs index 9674115..94c6d57 100644 --- a/BrightGlimmer.Data/Repositories/Repository.cs +++ b/BrightGlimmer.Data/Repositories/CommandRepository.cs @@ -8,13 +8,13 @@ using System.Text; namespace BrightGlimmer.Data.Repositories { - public abstract class Repository : IRepository where T : Entity + public class CommandRepository : ICommandRepository where T : Entity { public IUnitOfWork UnitOfWork => context; - protected BgContext context; + protected readonly BgContext context; - public Repository(BgContext context) + public CommandRepository(BgContext context) /* TODO: Change way we inject context later */ { this.context = context; } diff --git a/BrightGlimmer.Data/Repositories/QueryRepository.cs b/BrightGlimmer.Data/Repositories/QueryRepository.cs new file mode 100644 index 0000000..512ca09 --- /dev/null +++ b/BrightGlimmer.Data/Repositories/QueryRepository.cs @@ -0,0 +1,31 @@ +using BrightGlimmer.Data.Interfaces; +using BrightGlimmer.Domain; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BrightGlimmer.Data.Repositories +{ + public class QueryRepository : IQueryRepository where T : Entity + { + public IUnitOfWork UnitOfWork => context; + + protected readonly BgContext context; + + public QueryRepository(BgContext context) /* TODO: Change way we inject context later */ + { + this.context = context; + } + + public virtual IQueryable Get() + { + return context.Set(); + } + + public virtual T Get(Guid id) + { + return context.Set().Find(id); + } + } +} diff --git a/BrightGlimmer.Data/Repositories/StudentRepository.cs b/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs similarity index 64% rename from BrightGlimmer.Data/Repositories/StudentRepository.cs rename to BrightGlimmer.Data/Repositories/StudentQueryRepository.cs index 0e25f81..8fd2dea 100644 --- a/BrightGlimmer.Data/Repositories/StudentRepository.cs +++ b/BrightGlimmer.Data/Repositories/StudentQueryRepository.cs @@ -8,8 +8,8 @@ using System.Text; namespace BrightGlimmer.Data.Repositories { - public class StudentRepository : Repository + public class StudentQueryRepository : QueryRepository { - public StudentRepository(BgContext context) : base(context) { } + public StudentQueryRepository(BgContext context) : base(context) { } } }