| 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 class Repository<T> : IRepository<T> where T : class, IIdentifiable
|
|---|
| 18 | {
|
|---|
| 19 | // private readonly PlatnosciDataContext _dataContext = new PlatnosciDataContext();
|
|---|
| 20 | //private PlatnosciDataContext _dataContext = new PlatnosciDataContext();
|
|---|
| 21 | protected readonly IDataContext _dataContext;
|
|---|
| 22 |
|
|---|
| 23 | public Repository(IDataContext dataContext)
|
|---|
| 24 | {
|
|---|
| 25 | _dataContext = dataContext;
|
|---|
| 26 | }
|
|---|
| 27 | public int Count()
|
|---|
| 28 | {
|
|---|
| 29 | return _dataContext.GetTable<T>().Count();
|
|---|
| 30 | }
|
|---|
| 31 | public int Count(Expression<Func<T, bool>> expression)
|
|---|
| 32 | {
|
|---|
| 33 | return _dataContext.GetTable<T>().Count(expression);
|
|---|
| 34 | }
|
|---|
| 35 | public T FindOne(int id)
|
|---|
| 36 | {
|
|---|
| 37 | return FindOne(t => t.Id == id);
|
|---|
| 38 | }
|
|---|
| 39 | public T FindOne(Expression<Func<T, bool>> expression)
|
|---|
| 40 | {
|
|---|
| 41 | return _dataContext.GetTable<T>().Where(expression).SingleOrDefault();
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
|
|---|
| 45 | {
|
|---|
| 46 | entity = FindOne(expression);
|
|---|
| 47 |
|
|---|
| 48 | return (entity != null);
|
|---|
| 49 | }
|
|---|
| 50 | public bool Exists(Expression<Func<T, bool>> expression)
|
|---|
| 51 | {
|
|---|
| 52 | var entity = FindOne(expression);
|
|---|
| 53 |
|
|---|
| 54 | return (entity != null);
|
|---|
| 55 | }
|
|---|
| 56 | public IList<T> FindAll()
|
|---|
| 57 | {
|
|---|
| 58 | return _dataContext.GetTable<T>().ToList();
|
|---|
| 59 | }
|
|---|
| 60 | public IList<T> FindAll(Expression<Func<T, bool>> expression)
|
|---|
| 61 | {
|
|---|
| 62 | return _dataContext.GetTable<T>().Where(expression).ToList();
|
|---|
| 63 | }
|
|---|
| 64 | public void Insert(T entity)
|
|---|
| 65 | {
|
|---|
| 66 | _dataContext.Insert(entity);
|
|---|
| 67 | }
|
|---|
| 68 | public void Delete(T entity)
|
|---|
| 69 | {
|
|---|
| 70 | _dataContext.Insert(entity);
|
|---|
| 71 | }
|
|---|
| 72 | public void Update(T entity)
|
|---|
| 73 | {
|
|---|
| 74 | _dataContext.SubmitChanges();
|
|---|
| 75 | }
|
|---|
| 76 | public IQueryable<T> Find(int id)
|
|---|
| 77 | {
|
|---|
| 78 | return _dataContext.GetTable<T>().Where(t => t.Id == id);
|
|---|
| 79 | }
|
|---|
| 80 | public IQueryable<T> Find(Expression<Func<T, bool>> expression)
|
|---|
| 81 | {
|
|---|
| 82 | return _dataContext.GetTable<T>().Where(expression);
|
|---|
| 83 | }
|
|---|
| 84 | public IQueryable<T> Find()
|
|---|
| 85 | {
|
|---|
| 86 | return _dataContext.GetTable<T>();
|
|---|
| 87 | }
|
|---|
| 88 | public IQueryable<vPlatnosciEcard> FindInvoiceByNipNumber(string nip, string numer)
|
|---|
| 89 | {
|
|---|
| 90 | return _dataContext.FindInvoiceByNipNumber(nip, numer);
|
|---|
| 91 | }
|
|---|
| 92 | public IQueryable<vPlatnosciEcard> FindInvoiceById(int id)
|
|---|
| 93 | {
|
|---|
| 94 | return _dataContext.FindInvoiceById(id);
|
|---|
| 95 | }
|
|---|
| 96 | public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
|
|---|
| 97 | {
|
|---|
| 98 | return _dataContext.FindItemsByIdFaktury(idFaktury);
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|