Entity Framework 6 + Generic – Business Logic Design Pattern
Root Generic Business Logic |
using System; using System.Collections.Generic; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using System.Linq.Dynamic; using SoccerDAO; public class GenericService { internal DbContext context; public GenericService(string name) { this.context = new Soccer(name); } public IQueryable<TEntity> GetAll<TEntity>() where TEntity : class { … … } public IQueryable<TEntity> GetItemByKey<TEntity>(int id) where TEntity : class { … … } public IQueryable<TEntity> GetItemByFieldValue<TEntity>(string fieldname, int value) where TEntity : class { … … } public int RemoveItem<TEntity>(int id) where TEntity : class { … … } public bool CreateItem<TEntity>(TEntity item) where TEntity : class { … … } public List<string> GetPrimaryKey<TEntity>() where TEntity : class { … … } } |
Child Business Logic |
using System; using System.Collections.Generic; using System.Linq; using SoccerDAO; namespace SoccerBusinessLogic.Services { public class TeamService : GenericService { public TeamService(string name) : base(name) { } public List<TEAM> getAllTeam() { this.context.Configuration.LazyLoadingEnabled = false; List<TEAM> list = this.GetAll<TEAM>().ToList(); this.context.Configuration.LazyLoadingEnabled = true; return list; } public List<TEAM> getTeamByLeague(int LeagueID) { this.context.Configuration.LazyLoadingEnabled = false; List<TEAM> list = this.GetItemByFieldValue<TEAM>(“TEAM_LEAGUE", LeagueID).ToList(); this.context.Configuration.LazyLoadingEnabled = true; return list; } } } |