WIP setting up new data project structure and event handlers
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using BrightGlimmer.Data.Domain;
|
||||
using BrightGlimmer.Data.Interfaces;
|
||||
using BrightGlimmer.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -6,9 +8,9 @@ using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data
|
||||
{
|
||||
public class SqliteContext : DbContext
|
||||
public class BgContext : DbContext, IUnitOfWork
|
||||
{
|
||||
public SqliteContext(DbContextOptions<SqliteContext> options)
|
||||
public BgContext(DbContextOptions<BgContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
@@ -11,7 +11,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Interfaces\" />
|
||||
<ProjectReference Include="..\BrightGlimmer.Domain\BrightGlimmer.Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public class Course
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Code { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public class Phone
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public PhoneType Type { get; set; }
|
||||
public int AreaCode { get; set; }
|
||||
public int Number { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public enum PhoneType
|
||||
{
|
||||
HOMEPHONE, CELLPHONE, WORKPHONE
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Domain
|
||||
{
|
||||
public class Student
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string FirstName { get; set; }
|
||||
public string MiddleName { get; set; }
|
||||
public string LastName { get; set; }
|
||||
public string Email { get; set; }
|
||||
public List<Phone> Phones { get; set; }
|
||||
}
|
||||
}
|
||||
22
BrightGlimmer.Data/Interfaces/IRepository.cs
Normal file
22
BrightGlimmer.Data/Interfaces/IRepository.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Interfaces
|
||||
{
|
||||
public interface IRepository<T>
|
||||
{
|
||||
IUnitOfWork UnitOfWork { get; }
|
||||
|
||||
T Create(T entity);
|
||||
|
||||
T Update(T entity);
|
||||
|
||||
void Remove(Guid id);
|
||||
|
||||
IQueryable<T> Get();
|
||||
|
||||
T Get(Guid id);
|
||||
}
|
||||
}
|
||||
13
BrightGlimmer.Data/Interfaces/IUnitOfWork.cs
Normal file
13
BrightGlimmer.Data/Interfaces/IUnitOfWork.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BrightGlimmer.Data.Interfaces
|
||||
{
|
||||
public interface IUnitOfWork
|
||||
{
|
||||
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace BrightGlimmer.Data.Migrations
|
||||
{
|
||||
[DbContext(typeof(SqliteContext))]
|
||||
[DbContext(typeof(BgContext))]
|
||||
partial class SqliteDatabaseContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
|
||||
47
BrightGlimmer.Data/Repositories/Repository.cs
Normal file
47
BrightGlimmer.Data/Repositories/Repository.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using BrightGlimmer.Data.Interfaces;
|
||||
using BrightGlimmer.Domain;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Repositories
|
||||
{
|
||||
public abstract class Repository<T> : IRepository<T> where T : Entity
|
||||
{
|
||||
public IUnitOfWork UnitOfWork => context;
|
||||
|
||||
protected BgContext context;
|
||||
|
||||
public Repository(BgContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public virtual T Create(T entity)
|
||||
{
|
||||
return context.Set<T>().Add(entity).Entity;
|
||||
}
|
||||
|
||||
public virtual T Update(T entity)
|
||||
{
|
||||
return context.Set<T>().Update(entity).Entity;
|
||||
}
|
||||
|
||||
public virtual void Remove(Guid id)
|
||||
{
|
||||
context.Set<T>().Remove(Get(id));
|
||||
}
|
||||
|
||||
public virtual IQueryable<T> Get()
|
||||
{
|
||||
return context.Set<T>();
|
||||
}
|
||||
|
||||
public virtual T Get(Guid id)
|
||||
{
|
||||
return context.Set<T>().Find(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using BrightGlimmer.Data.Domain;
|
||||
using BrightGlimmer.Data.Interfaces;
|
||||
using BrightGlimmer.Domain;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -7,42 +8,8 @@ using System.Text;
|
||||
|
||||
namespace BrightGlimmer.Data.Repositories
|
||||
{
|
||||
public class StudentRepository
|
||||
public class StudentRepository : Repository<Student>
|
||||
{
|
||||
private readonly SqliteContext context;
|
||||
|
||||
|
||||
public StudentRepository(SqliteContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public Student Create(Student student)
|
||||
{
|
||||
EntityEntry<Student> entry = context.Students.Add(student);
|
||||
context.SaveChanges();
|
||||
return entry.Entity;
|
||||
}
|
||||
|
||||
public void Update(Student student)
|
||||
{
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
public void Remove(Guid id)
|
||||
{
|
||||
context.Students.Remove(GetById(id));
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
public IQueryable<Student> GetAll()
|
||||
{
|
||||
return context.Students;
|
||||
}
|
||||
|
||||
public Student GetById(Guid id)
|
||||
{
|
||||
return context.Students.Find(id);
|
||||
}
|
||||
public StudentRepository(BgContext context) : base(context) { }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user