Added lazy loading and fixed private properties not deserializing

This commit is contained in:
Giovani
2019-05-01 10:13:05 -05:00
parent 783e251c97
commit 63fb52de6b
10 changed files with 28 additions and 27 deletions

View File

@@ -9,15 +9,13 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JsonNet.PrivateSettersContractResolvers.Source" Version="0.1.0"> <PackageReference Include="JsonNet.PrivateSettersContractResolvers" Version="1.0.0" />
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MediatR" Version="6.0.0" /> <PackageReference Include="MediatR" Version="6.0.0" />
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="6.0.1" /> <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" /> <PackageReference Include="Microsoft.AspNetCore.All" />
<PackageReference Include="Microsoft.AspNetCore.App" /> <PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -2,6 +2,7 @@
using BrightGlimmer.Data.Interfaces; using BrightGlimmer.Data.Interfaces;
using BrightGlimmer.Data.Repositories; using BrightGlimmer.Data.Repositories;
using BrightGlimmer.Domain; using BrightGlimmer.Domain;
using JsonNet.PrivateSettersContractResolvers;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
@@ -28,7 +29,7 @@ namespace BrightGlimmer.Api
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(options => .AddJsonOptions(options =>
{ {
options.SerializerSettings.ContractResolver = new PrivateSetterContractResolver() options.SerializerSettings.ContractResolver = new PrivateSetterContractResolver();
}); });
services.AddMediatR(); services.AddMediatR();
@@ -37,9 +38,8 @@ namespace BrightGlimmer.Api
services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>)); services.AddTransient(typeof(IQueryRepository<>), typeof(QueryRepository<>));
services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>)); services.AddTransient(typeof(ICommandRepository<>), typeof(CommandRepository<>));
services.AddTransient(typeof(IQueryRepository<Student>), typeof(StudentQueryRepository)); services.AddDbContext<BgContext>(options => options.UseLazyLoadingProxies()
.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddDbContext<BgContext>(options => options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<BgContext, BgContext>(); services.AddTransient<BgContext, BgContext>();
} }
@@ -59,11 +59,11 @@ namespace BrightGlimmer.Api
app.UseMvc(); app.UseMvc();
// Makes sure that the database is in fact created // Makes sure that the database is in fact created
//using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope()) using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
//{ {
// var context = serviceScope.ServiceProvider.GetRequiredService<BgContext>(); var context = serviceScope.ServiceProvider.GetRequiredService<BgContext>();
// context.Database.EnsureCreated(); context.Database.EnsureCreated();
//} }
} }
} }
} }

View File

@@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Proxies" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
</ItemGroup> </ItemGroup>

View File

@@ -10,6 +10,7 @@ using System.Threading.Tasks;
namespace BrightGlimmer.Data.Repositories namespace BrightGlimmer.Data.Repositories
{ {
/* TODO: REMOVE MAYBE? */
public class StudentQueryRepository : QueryRepository<Student> public class StudentQueryRepository : QueryRepository<Student>
{ {
public StudentQueryRepository(BgContext context) : base(context) { } public StudentQueryRepository(BgContext context) : base(context) { }
@@ -34,9 +35,9 @@ namespace BrightGlimmer.Data.Repositories
public override async Task<Student> GetAsync(Guid id) public override async Task<Student> GetAsync(Guid id)
{ {
return await context.Students return await context.Students
.Include(x => x.Phones) //.Include(x => x.Phones)
.Include(x => x.Address) //.Include(x => x.Address)
.Include(x => x.AssignedCourses) //.Include(x => x.AssignedCourses)
.SingleAsync(x => x.Id == id); .SingleAsync(x => x.Id == id);
} }
} }

Binary file not shown.

View File

@@ -20,7 +20,7 @@ namespace BrightGlimmer.Domain
[JsonIgnore] [JsonIgnore]
public decimal Longitude { get; private set; } public decimal Longitude { get; private set; }
private Address() { } protected Address() { }
public Address(string streetAddress1, public Address(string streetAddress1,
string streetAddress2, string streetAddress2,

View File

@@ -12,10 +12,10 @@ namespace BrightGlimmer.Domain
public bool IsActive { get; set; } public bool IsActive { get; set; }
public string Term { get; set; } public string Term { get; set; }
public Student Student { get; private set; } public virtual Student Student { get; private set; }
public Course Course { get; private set; } public virtual Course Course { get; private set; }
private AssignedCourse() { } protected AssignedCourse() { }
public AssignedCourse(bool isActive, string term) public AssignedCourse(bool isActive, string term)
{ {

View File

@@ -12,7 +12,7 @@ namespace BrightGlimmer.Domain
public string Description { get; set; } public string Description { get; set; }
public string Code { get; set; } public string Code { get; set; }
private Course() { } protected Course() { }
public Course(string name, string description, string code) public Course(string name, string description, string code)
{ {

View File

@@ -16,7 +16,7 @@ namespace BrightGlimmer.Domain
[JsonProperty] [JsonProperty]
public int Number { get; private set; } public int Number { get; private set; }
private Phone() { } protected Phone() { }
public Phone(PhoneType type, int areaCode, int number) public Phone(PhoneType type, int areaCode, int number)
{ {

View File

@@ -1,4 +1,5 @@
using System; using Newtonsoft.Json;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Linq; using System.Linq;
@@ -17,16 +18,16 @@ namespace BrightGlimmer.Domain
public string MiddleName { get; set; } public string MiddleName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
public string Email { get; set; } public string Email { get; set; }
public Address Address { get; private set; } public virtual Address Address { get; private set; }
public string ProfilePictureUrl { get; set; } public string ProfilePictureUrl { get; set; }
private readonly List<Phone> phones = new List<Phone>(); private readonly List<Phone> phones = new List<Phone>();
public IReadOnlyList<Phone> Phones => phones; public virtual IReadOnlyList<Phone> Phones => phones;
private readonly List<AssignedCourse> assignedCourses = new List<AssignedCourse>(); private readonly List<AssignedCourse> assignedCourses = new List<AssignedCourse>();
public IReadOnlyList<AssignedCourse> AssignedCourses => assignedCourses; public virtual IReadOnlyList<AssignedCourse> AssignedCourses => assignedCourses;
private Student() { } protected Student() { }
public Student(string firstName, public Student(string firstName,
string lastName, string lastName,