| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Data.Linq;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Linq.Expressions;
|
|---|
| 6 | using System.Text;
|
|---|
| 7 | using Platnosci.Core.Interface;
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 | namespace Platnosci.Core.Linq
|
|---|
| 11 | {
|
|---|
| 12 | /// <summary>
|
|---|
| 13 | /// An implementation of the Repository which uses LINQ to SQL Server to persist its data
|
|---|
| 14 | /// </summary>
|
|---|
| 15 | /// <typeparam name="T">The generic type representing the entity we are dealing with</typeparam>
|
|---|
| 16 | /// <remarks>We use a 1 to 1 mapping of entity -> SQL table</remarks>
|
|---|
| 17 | public sealed class Repository<T> : IRepository<T>
|
|---|
| 18 | where T : class, IIdentifiable
|
|---|
| 19 | {
|
|---|
| 20 | private readonly PlatnosciDataContext _dataContext = new PlatnosciDataContext();
|
|---|
| 21 |
|
|---|
| 22 | public int Count()
|
|---|
| 23 | {
|
|---|
| 24 | return _dataContext.GetTable<T>().Count();
|
|---|
| 25 | }
|
|---|
| 26 | public int Count(Expression<Func<T, bool>> expression)
|
|---|
| 27 | {
|
|---|
| 28 | return _dataContext.GetTable<T>().Count(expression);
|
|---|
| 29 | }
|
|---|
| 30 | public T FindOne(int id)
|
|---|
| 31 | {
|
|---|
| 32 | return FindOne(t => t.ID_faktury == id);
|
|---|
| 33 | }
|
|---|
| 34 | public T FindOne(Expression<Func<T, bool>> expression)
|
|---|
| 35 | {
|
|---|
| 36 | return _dataContext.GetTable<T>().Where(expression).SingleOrDefault();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
|
|---|
| 40 | {
|
|---|
| 41 | entity = FindOne(expression);
|
|---|
| 42 |
|
|---|
| 43 | return (entity != null);
|
|---|
| 44 | }
|
|---|
| 45 | public bool Exists(Expression<Func<T, bool>> expression)
|
|---|
| 46 | {
|
|---|
| 47 | var entity = FindOne(expression);
|
|---|
| 48 |
|
|---|
| 49 | return (entity != null);
|
|---|
| 50 | }
|
|---|
| 51 | public IList<T> FindAll()
|
|---|
| 52 | {
|
|---|
| 53 | return _dataContext.GetTable<T>().ToList();
|
|---|
| 54 | }
|
|---|
| 55 | public IList<T> FindAll(Expression<Func<T, bool>> expression)
|
|---|
| 56 | {
|
|---|
| 57 | return _dataContext.GetTable<T>().Where(expression).ToList();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public void Insert(T entity)
|
|---|
| 61 | {
|
|---|
| 62 | //DataContext.GetTable<T>().InsertOnSubmit(entity);
|
|---|
| 63 | //DataContext.SubmitChanges();
|
|---|
| 64 |
|
|---|
| 65 | //using (DataContext dc = new WierszowkiDataContext())
|
|---|
| 66 | //{
|
|---|
| 67 | _dataContext.GetTable<T>().InsertOnSubmit(entity);
|
|---|
| 68 | _dataContext.SubmitChanges();
|
|---|
| 69 | //}
|
|---|
| 70 | }
|
|---|
| 71 | public void Delete(T entity)
|
|---|
| 72 | {
|
|---|
| 73 | if (entity != null)
|
|---|
| 74 | {
|
|---|
| 75 | _dataContext.GetTable<T>().DeleteOnSubmit(entity);
|
|---|
| 76 | _dataContext.SubmitChanges();
|
|---|
| 77 | }
|
|---|
| 78 | }
|
|---|
| 79 | public void Update(T entity)
|
|---|
| 80 | {
|
|---|
| 81 | _dataContext.SubmitChanges();
|
|---|
| 82 | }
|
|---|
| 83 | public IQueryable<T> Find(int id)
|
|---|
| 84 | {
|
|---|
| 85 | return _dataContext.GetTable<T>().Where(t => t.ID_faktury == id);
|
|---|
| 86 | }
|
|---|
| 87 | public IQueryable<T> Find(Expression<Func<T, bool>> expression)
|
|---|
| 88 | {
|
|---|
| 89 | return _dataContext.GetTable<T>().Where(expression);
|
|---|
| 90 | }
|
|---|
| 91 | public IQueryable<T> Find()
|
|---|
| 92 | {
|
|---|
| 93 | return _dataContext.GetTable<T>();
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|