Refactored repositories and fixed service injections

This commit is contained in:
2019-04-23 22:52:58 +00:00
parent a437573af0
commit f9badf98f4
15 changed files with 80 additions and 78 deletions

View File

@@ -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

View File

@@ -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<ActionResult> 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<Data.Domain.Phone>
// {
// new Data.Domain.Phone
// {
// Id = Guid.NewGuid(),
// AreaCode = 305,
// Number = 8888888,
// Type = Data.Domain.PhoneType.HOMEPHONE
// }
// }
// });
// return new ObjectResult(created);
//}
}
}

View File

@@ -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
{

View File

@@ -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<BgContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<BgContext>();
services.AddTransient<BgContext, BgContext>();
}
// 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<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<BgContext>();
context.Database.EnsureCreated();
}
//using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
//{
// var context = serviceScope.ServiceProvider.GetRequiredService<BgContext>();
// context.Database.EnsureCreated();
//}
}
}
}

View File

@@ -5,7 +5,7 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Domain\Interfaces\" />
<Folder Include="Interfaces\" />
<Folder Include="Handlers\CommandHandlers\" />
<Folder Include="Commands\" />
<Folder Include="Handlers\QueryHandlers\" />

View File

@@ -4,5 +4,6 @@ using System.Text;
namespace BrightGlimmer.Cqrs
{
/* USED TO GET ASSEMBLY IN STARTUP.CS */
public class Cqrs { }
}

View File

@@ -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<GetAllStudentsQuery, IEnumerable<Student>>
{
private readonly IRepository<Student> repository;
private readonly IQueryRepository<Student> repository;
public GetAllStudentsQueryHandler(IRepository<Student> repository)
public GetAllStudentsQueryHandler(IQueryRepository<Student> repository)
{
this.repository = repository;
}

View File

@@ -1,8 +1,6 @@
using BrightGlimmer.Domain;
using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
namespace BrightGlimmer.Services.Queries
{

View File

@@ -1,5 +1,4 @@
using BrightGlimmer.Data.Domain;
using BrightGlimmer.Data.Interfaces;
using BrightGlimmer.Data.Interfaces;
using BrightGlimmer.Domain;
using Microsoft.EntityFrameworkCore;
using System;

View File

@@ -5,7 +5,7 @@ using System.Text;
namespace BrightGlimmer.Data.Interfaces
{
public interface IRepository<T>
public interface ICommandRepository<T>
{
IUnitOfWork UnitOfWork { get; }

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BrightGlimmer.Data.Interfaces
{
public interface IQueryRepository<T>
{
IUnitOfWork UnitOfWork { get; }
IQueryable<T> Get();
T Get(Guid id);
}
}

View File

@@ -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
{

View File

@@ -8,13 +8,13 @@ using System.Text;
namespace BrightGlimmer.Data.Repositories
{
public abstract class Repository<T> : IRepository<T> where T : Entity
public class CommandRepository<T> : ICommandRepository<T> 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;
}

View File

@@ -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<T> : IQueryRepository<T> 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<T> Get()
{
return context.Set<T>();
}
public virtual T Get(Guid id)
{
return context.Set<T>().Find(id);
}
}
}

View File

@@ -8,8 +8,8 @@ using System.Text;
namespace BrightGlimmer.Data.Repositories
{
public class StudentRepository : Repository<Student>
public class StudentQueryRepository : QueryRepository<Student>
{
public StudentRepository(BgContext context) : base(context) { }
public StudentQueryRepository(BgContext context) : base(context) { }
}
}