added Data, Models, EntityFramework

This commit is contained in:
chsakell
2016-09-28 13:21:28 +03:00
parent 1ef3f04731
commit e6aed79fa9
17 changed files with 378 additions and 28 deletions

View File

@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Linq.Expressions;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data.Abstract
{
public interface IEntityBaseRepository<T> where T : class, IEntityBase, new()
{
IEnumerable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties);
IEnumerable<T> GetAll();
int Count();
T GetSingle(int id);
T GetSingle(Expression<Func<T, bool>> predicate);
T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties);
IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate);
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void DeleteWhere(Expression<Func<T, bool>> predicate);
void Commit();
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data.Abstract
{
public interface IMatchRepository : IEntityBaseRepository<Match> { }
public interface IFeedRepository : IEntityBaseRepository<Feed> { }
}

36
Data/LiveGameContext.cs Normal file
View File

@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LiveGameFeed.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace LiveGameFeed.Data
{
public class LiveGameContext : DbContext
{
public DbSet<Match> Matches { get; set; }
public DbSet<Feed> Feeds { get; set; }
public LiveGameContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys()))
{
relationship.DeleteBehavior = DeleteBehavior.Restrict;
}
modelBuilder.Entity<Match>()
.ToTable("Match");
modelBuilder.Entity<Feed>()
.ToTable("Feed");
modelBuilder.Entity<Feed>()
.Property(f => f.MatchId)
.IsRequired();
}
}
}

View File

@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data
{
public class LiveGameDbInitializer
{
private static LiveGameContext context;
public static void Initialize(IServiceProvider serviceProvider)
{
context = (LiveGameContext)serviceProvider.GetService(typeof(LiveGameContext));
InitializeSchedules();
}
private static void InitializeSchedules()
{
if (!context.Matches.Any())
{
Match match_01 = new Match
{
Host = "Panathinaikos",
Guest = "Olimpiakos",
HostScore = 3,
GuestScore = 1,
MatchDate = DateTime.Now,
League = "FootballLeauge",
Feeds = new List<Feed>
{
new Feed()
{
Description = "Match started",
MatchId = 1
},
new Feed()
{
Description = "Goal for Panathinaikos",
MatchId = 1
},
}
};
Match match_02 = new Match
{
Host = "Real Madrit FC",
Guest = "Barchelona",
HostScore = 5,
GuestScore = 3,
MatchDate = DateTime.Now,
League = "Spanish League",
Feeds = new List<Feed>
{
new Feed()
{
Description = "Match started",
MatchId = 2
},
new Feed()
{
Description = "Goal for Real Madrid",
MatchId = 2
},
}
};
context.Matches.Add(match_01); context.Matches.Add(match_02);
context.SaveChanges();
}
}
}
}

View File

@@ -0,0 +1,102 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using LiveGameFeed.Data.Abstract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data.Repositories
{
public class EntityBaseRepository<T> : IEntityBaseRepository<T>
where T : class, IEntityBase, new()
{
private LiveGameContext _context;
#region Properties
public EntityBaseRepository(LiveGameContext context)
{
_context = context;
}
#endregion
public virtual IEnumerable<T> GetAll()
{
return _context.Set<T>().AsEnumerable();
}
public virtual int Count()
{
return _context.Set<T>().Count();
}
public virtual IEnumerable<T> AllIncluding(params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.AsEnumerable();
}
public T GetSingle(int id)
{
return _context.Set<T>().FirstOrDefault(x => x.Id == id);
}
public T GetSingle(Expression<Func<T, bool>> predicate)
{
return _context.Set<T>().FirstOrDefault(predicate);
}
public T GetSingle(Expression<Func<T, bool>> predicate, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = _context.Set<T>();
foreach (var includeProperty in includeProperties)
{
query = query.Include(includeProperty);
}
return query.Where(predicate).FirstOrDefault();
}
public virtual IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate)
{
return _context.Set<T>().Where(predicate);
}
public virtual void Add(T entity)
{
EntityEntry dbEntityEntry = _context.Entry<T>(entity);
_context.Set<T>().Add(entity);
}
public virtual void Update(T entity)
{
EntityEntry dbEntityEntry = _context.Entry<T>(entity);
dbEntityEntry.State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
EntityEntry dbEntityEntry = _context.Entry<T>(entity);
dbEntityEntry.State = EntityState.Deleted;
}
public virtual void DeleteWhere(Expression<Func<T, bool>> predicate)
{
IEnumerable<T> entities = _context.Set<T>().Where(predicate);
foreach(var entity in entities)
{
_context.Entry<T>(entity).State = EntityState.Deleted;
}
}
public virtual void Commit()
{
_context.SaveChanges();
}
}
}

View File

@@ -0,0 +1,12 @@
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data.Repositories
{
public class FeedRepository : EntityBaseRepository<Feed>, IFeedRepository
{
public FeedRepository(LiveGameContext context)
: base(context)
{ }
}
}

View File

@@ -0,0 +1,12 @@
using LiveGameFeed.Data.Abstract;
using LiveGameFeed.Models;
namespace LiveGameFeed.Data.Repositories
{
public class MatchRepository : EntityBaseRepository<Match>, IMatchRepository
{
public MatchRepository(LiveGameContext context)
: base(context)
{ }
}
}