using System; using System.Collections.Generic; using System.Data.Linq; using System.Linq; using System.Linq.Expressions; using Wierszowki.Core.Interfaces; namespace Wierszowki.Core.Linq { /// /// An implementation of the Repository which uses LINQ to SQL Server to persist its data /// /// The generic type representing the entity we are dealing with /// We use a 1 to 1 mapping of entity -> SQL table public sealed class LinqRepository : IRepository where T : class, IIdentifiable { /// /// Gets the active LINQ DataContext which manages access to the persistance store /// private readonly WierszowkiDataContext _dataContext = new WierszowkiDataContext(); /// /// Returns a count of the items in the repository /// /// public int Count() { return _dataContext.GetTable().Count(); } /// /// Returns a count of the items in the repository which match the supplied lambda /// /// A lambda expression which will be evaluated against the repository /// public int Count(Expression> expression) { return _dataContext.GetTable().Count(expression); } /// /// Finds a single instance of an entity matching a unique identifier. /// /// /// public T FindOne(int id) { return FindOne(t => t.Id == id); } /// /// Finds a single instance of an entity matching a lambda expression /// /// /// /// The first entity which matches the expession /// public T FindOne(Expression> expression) { return _dataContext.GetTable().Where(expression).SingleOrDefault(); } public bool TryFindOne(Expression> expression, out T entity) { entity = FindOne(expression); return (entity != null); } /// /// Checks if an entity exists matching a lambda expression /// /// /// /// True if an entity exists, otherwise False /// public bool Exists(Expression> expression) { var entity = FindOne(expression); return (entity != null); } /// /// Finds all entities in the repository /// /// public IList FindAll() { return _dataContext.GetTable().ToList(); } /// /// Finds all entities matching a lambda expression /// /// The expression. /// public IList FindAll(Expression> expression) { return _dataContext.GetTable().Where(expression).ToList(); } /// /// Adds a new entity in to the repository /// /// public void Insert(T entity) { //DataContext.GetTable().InsertOnSubmit(entity); //DataContext.SubmitChanges(); //using (DataContext dc = new WierszowkiDataContext()) //{ _dataContext.GetTable().InsertOnSubmit(entity); _dataContext.SubmitChanges(); //} } /// /// Removes the specified entity from the repository /// /// public void Delete(T entity) { if (entity != null) { _dataContext.GetTable().DeleteOnSubmit(entity); _dataContext.SubmitChanges(); } } /// /// Saves the specified entity back to the repository /// /// public void Update(T entity) { _dataContext.SubmitChanges(); } /// /// Returns a requeryable set of all entities in the repository matching a unique identifier /// /// /// public IQueryable Find(int id) { return _dataContext.GetTable().Where(t => t.Id == id); } /// /// Returns a requeryable set of all entities in the repository matching a lambda expression /// /// /// public IQueryable Find(Expression> expression) { return _dataContext.GetTable().Where(expression); } /// /// Returns a requeryable set of all entities in the repository /// /// public IQueryable Find() { return _dataContext.GetTable(); } } }