-
-
Save spewu/5502965 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Entity | |
{ | |
public int Id { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Data.Entity.Migrations; | |
using System.Linq; | |
using System.Linq.Expressions; | |
public class EntityFrameworkSession : ISession | |
{ | |
private readonly DbContext context; | |
public EntityFrameworkSession(DbContext context) | |
{ | |
this.context = context; | |
} | |
public T Find<T>(params int[] keyValues) where T : Entity | |
{ | |
return context.Set<T>().Find(keyValues); | |
} | |
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : Entity | |
{ | |
return context.Set<T>().SingleOrDefault(predicate); | |
} | |
public T FirstOrDefault<T>(Func<T, bool> predicate) where T : Entity | |
{ | |
return context.Set<T>().FirstOrDefault(predicate); | |
} | |
public IQueryable<T> All<T>() where T : Entity | |
{ | |
return context.Set<T>().AsQueryable(); | |
} | |
public void Add<T>(T entity) where T : Entity | |
{ | |
context.Set<T>().Add(entity); | |
} | |
public void AddOrUpdate<T>(params T[] entities) where T : Entity | |
{ | |
context.Set<T>().AddOrUpdate(entities); | |
} | |
public void AddOrUpdate<T>(Expression<Func<T, object>> identifierExpression, params T[] entities) where T : Entity | |
{ | |
context.Set<T>().AddOrUpdate(identifierExpression, entities); | |
} | |
public void LoadReference<T, TProperty>(T entity, Expression<Func<T, TProperty>> navigationProperty) | |
where T : Entity | |
where TProperty : class | |
{ | |
context.Entry(entity).Reference(navigationProperty).Load(); | |
} | |
public void LoadCollection<T, TElement>(T entity, Expression<Func<T, ICollection<TElement>>> navigationProperty) | |
where T : Entity | |
where TElement : class | |
{ | |
context.Entry(entity).Collection(navigationProperty).Load(); | |
} | |
public int Commit() | |
{ | |
return context.SaveChanges(); | |
} | |
public void Delete<T>(T entity) where T : Entity | |
{ | |
context.Set<T>().Remove(entity); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
public interface ISession | |
{ | |
T Find<T>(params int[] keyValues) where T : Entity; | |
T SingleOrDefault<T>(Func<T, bool> predicate) where T : Entity; | |
T FirstOrDefault<T>(Func<T, bool> predicate) where T : Entity; | |
IQueryable<T> All<T>() where T : Entity; | |
void Add<T>(T entity) where T : Entity; | |
void AddOrUpdate<T>(params T[] entities) where T : Entity; | |
void AddOrUpdate<T>(Expression<Func<T, object>> identifierExpression, params T[] entities) where T : Entity; | |
void LoadReference<T, TProperty>(T entity, Expression<Func<T, TProperty>> navigationProperty) | |
where T : Entity | |
where TProperty : class; | |
void LoadCollection<T, TElement>(T entity, Expression<Func<T, ICollection<TElement>>> navigationProperty) | |
where T : Entity | |
where TElement : class; | |
int Commit(); | |
void Delete<T>(T entity) where T : Entity; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment