| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Data.Linq;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Linq.Expressions;
|
|---|
| 6 | using Wierszowki.Core.Interfaces;
|
|---|
| 7 |
|
|---|
| 8 | namespace Wierszowki.Core.Linq
|
|---|
| 9 | {
|
|---|
| 10 | /// <summary>
|
|---|
| 11 | /// An implementation of the Repository which uses LINQ to SQL Server to persist its data
|
|---|
| 12 | /// </summary>
|
|---|
| 13 | /// <typeparam name="T">The generic type representing the entity we are dealing with</typeparam>
|
|---|
| 14 | /// <remarks>We use a 1 to 1 mapping of entity -> SQL table</remarks>
|
|---|
| 15 | public sealed class LinqRepository<T> : IRepository<T>
|
|---|
| 16 | where T : class, IIdentifiable
|
|---|
| 17 | {
|
|---|
| 18 | /// <summary>
|
|---|
| 19 | /// Gets the active LINQ DataContext which manages access to the persistance store
|
|---|
| 20 | /// </summary>
|
|---|
| 21 | private readonly WierszowkiDataContext _dataContext = new WierszowkiDataContext();
|
|---|
| 22 |
|
|---|
| 23 | /// <summary>
|
|---|
| 24 | /// Returns a count of the items in the repository
|
|---|
| 25 | /// </summary>
|
|---|
| 26 | /// <returns></returns>
|
|---|
| 27 | public int Count()
|
|---|
| 28 | {
|
|---|
| 29 | return _dataContext.GetTable<T>().Count();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | /// <summary>
|
|---|
| 33 | /// Returns a count of the items in the repository which match the supplied lambda
|
|---|
| 34 | /// </summary>
|
|---|
| 35 | /// <param name="expression">A lambda expression which will be evaluated against the repository</param>
|
|---|
| 36 | /// <returns></returns>
|
|---|
| 37 | public int Count(Expression<Func<T, bool>> expression)
|
|---|
| 38 | {
|
|---|
| 39 | return _dataContext.GetTable<T>().Count(expression);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | /// <summary>
|
|---|
| 43 | /// Finds a single instance of an entity matching a unique identifier.
|
|---|
| 44 | /// </summary>
|
|---|
| 45 | /// <param name="id"></param>
|
|---|
| 46 | /// <returns></returns>
|
|---|
| 47 | public T FindOne(int id)
|
|---|
| 48 | {
|
|---|
| 49 | return FindOne(t => t.Id == id);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /// <summary>
|
|---|
| 53 | /// Finds a single instance of an entity matching a lambda expression
|
|---|
| 54 | /// </summary>
|
|---|
| 55 | /// <param name="expression"></param>
|
|---|
| 56 | /// <returns>
|
|---|
| 57 | /// The first entity which matches the expession
|
|---|
| 58 | /// </returns>
|
|---|
| 59 | public T FindOne(Expression<Func<T, bool>> expression)
|
|---|
| 60 | {
|
|---|
| 61 | return _dataContext.GetTable<T>().Where(expression).SingleOrDefault();
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
|
|---|
| 65 | {
|
|---|
| 66 | entity = FindOne(expression);
|
|---|
| 67 |
|
|---|
| 68 | return (entity != null);
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /// <summary>
|
|---|
| 72 | /// Checks if an entity exists matching a lambda expression
|
|---|
| 73 | /// </summary>
|
|---|
| 74 | /// <param name="expression"></param>
|
|---|
| 75 | /// <returns>
|
|---|
| 76 | /// True if an entity exists, otherwise False
|
|---|
| 77 | /// </returns>
|
|---|
| 78 | public bool Exists(Expression<Func<T, bool>> expression)
|
|---|
| 79 | {
|
|---|
| 80 | var entity = FindOne(expression);
|
|---|
| 81 |
|
|---|
| 82 | return (entity != null);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | /// <summary>
|
|---|
| 87 | /// Finds all entities in the repository
|
|---|
| 88 | /// </summary>
|
|---|
| 89 | /// <returns></returns>
|
|---|
| 90 | public IList<T> FindAll()
|
|---|
| 91 | {
|
|---|
| 92 | return _dataContext.GetTable<T>().ToList();
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /// <summary>
|
|---|
| 96 | /// Finds all entities matching a lambda expression
|
|---|
| 97 | /// </summary>
|
|---|
| 98 | /// <param name="expression">The expression.</param>
|
|---|
| 99 | /// <returns></returns>
|
|---|
| 100 | public IList<T> FindAll(Expression<Func<T, bool>> expression)
|
|---|
| 101 | {
|
|---|
| 102 | return _dataContext.GetTable<T>().Where(expression).ToList();
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /// <summary>
|
|---|
| 106 | /// Adds a new entity in to the repository
|
|---|
| 107 | /// </summary>
|
|---|
| 108 | /// <param name="entity"></param>
|
|---|
| 109 | public void Insert(T entity)
|
|---|
| 110 | {
|
|---|
| 111 | //DataContext.GetTable<T>().InsertOnSubmit(entity);
|
|---|
| 112 | //DataContext.SubmitChanges();
|
|---|
| 113 |
|
|---|
| 114 | //using (DataContext dc = new WierszowkiDataContext())
|
|---|
| 115 | //{
|
|---|
| 116 | _dataContext.GetTable<T>().InsertOnSubmit(entity);
|
|---|
| 117 | _dataContext.SubmitChanges();
|
|---|
| 118 | //}
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | /// <summary>
|
|---|
| 122 | /// Removes the specified entity from the repository
|
|---|
| 123 | /// </summary>
|
|---|
| 124 | /// <param name="entity"></param>
|
|---|
| 125 | public void Delete(T entity)
|
|---|
| 126 | {
|
|---|
| 127 | using (var dc = new WierszowkiDataContext())
|
|---|
| 128 | {
|
|---|
| 129 | dc.GetTable<T>().Attach(entity);
|
|---|
| 130 | dc.GetTable<T>().DeleteOnSubmit(entity);
|
|---|
| 131 | dc.SubmitChanges();
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | /// <summary>
|
|---|
| 136 | /// Saves the specified entity back to the repository
|
|---|
| 137 | /// </summary>
|
|---|
| 138 | /// <param name="entity"></param>
|
|---|
| 139 | public void Update(T entity)
|
|---|
| 140 | {
|
|---|
| 141 | _dataContext.SubmitChanges();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /// <summary>
|
|---|
| 145 | /// Returns a requeryable set of all entities in the repository matching a unique identifier
|
|---|
| 146 | /// </summary>
|
|---|
| 147 | /// <param name="id"></param>
|
|---|
| 148 | /// <returns></returns>
|
|---|
| 149 | public IQueryable<T> Find(int id)
|
|---|
| 150 | {
|
|---|
| 151 | return _dataContext.GetTable<T>().Where(t => t.Id == id);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /// <summary>
|
|---|
| 155 | /// Returns a requeryable set of all entities in the repository matching a lambda expression
|
|---|
| 156 | /// </summary>
|
|---|
| 157 | /// <param name="expression"></param>
|
|---|
| 158 | /// <returns></returns>
|
|---|
| 159 | public IQueryable<T> Find(Expression<Func<T, bool>> expression)
|
|---|
| 160 | {
|
|---|
| 161 | return _dataContext.GetTable<T>().Where(expression); }
|
|---|
| 162 |
|
|---|
| 163 | /// <summary>
|
|---|
| 164 | /// Returns a requeryable set of all entities in the repository
|
|---|
| 165 | /// </summary>
|
|---|
| 166 | /// <returns></returns>
|
|---|
| 167 | public IQueryable<T> Find()
|
|---|
| 168 | {
|
|---|
| 169 | return _dataContext.GetTable<T>();
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | } |
|---|