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