| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 | using System.Linq.Expressions;
|
|---|
| 5 | using Platnosci.Core.Interface;
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | namespace Platnosci.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 class Repository<T> : IRepository<T> where T : class, IIdentifiable
|
|---|
| 16 | {
|
|---|
| 17 | protected readonly IDataContext DataContext;
|
|---|
| 18 |
|
|---|
| 19 | public Repository(IDataContext dataContext)
|
|---|
| 20 | {
|
|---|
| 21 | DataContext = dataContext;
|
|---|
| 22 | }
|
|---|
| 23 | public int Count()
|
|---|
| 24 | {
|
|---|
| 25 | return DataContext.GetTable<T>().Count();
|
|---|
| 26 | }
|
|---|
| 27 | public int Count(Expression<Func<T, bool>> expression)
|
|---|
| 28 | {
|
|---|
| 29 | return DataContext.GetTable<T>().Count(expression);
|
|---|
| 30 | }
|
|---|
| 31 | public T FindOne(int id)
|
|---|
| 32 | {
|
|---|
| 33 | return FindOne(t => t.Id == id);
|
|---|
| 34 | }
|
|---|
| 35 | public T FindOne(Expression<Func<T, bool>> expression)
|
|---|
| 36 | {
|
|---|
| 37 | return DataContext.GetTable<T>().Where(expression).SingleOrDefault();
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
|
|---|
| 41 | {
|
|---|
| 42 | entity = FindOne(expression);
|
|---|
| 43 |
|
|---|
| 44 | return (entity != null);
|
|---|
| 45 | }
|
|---|
| 46 | public bool Exists(Expression<Func<T, bool>> expression)
|
|---|
| 47 | {
|
|---|
| 48 | var entity = FindOne(expression);
|
|---|
| 49 |
|
|---|
| 50 | return (entity != null);
|
|---|
| 51 | }
|
|---|
| 52 | public IList<T> FindAll()
|
|---|
| 53 | {
|
|---|
| 54 | return DataContext.GetTable<T>().ToList();
|
|---|
| 55 | }
|
|---|
| 56 | public IList<T> FindAll(Expression<Func<T, bool>> expression)
|
|---|
| 57 | {
|
|---|
| 58 | return DataContext.GetTable<T>().Where(expression).ToList();
|
|---|
| 59 | }
|
|---|
| 60 | public void Insert(T entity)
|
|---|
| 61 | {
|
|---|
| 62 | DataContext.Insert(entity);
|
|---|
| 63 | }
|
|---|
| 64 | public void Delete(T entity)
|
|---|
| 65 | {
|
|---|
| 66 | DataContext.Delete(entity);
|
|---|
| 67 | }
|
|---|
| 68 | public void SubmitChanges()
|
|---|
| 69 | {
|
|---|
| 70 | DataContext.SubmitChanges();
|
|---|
| 71 | }
|
|---|
| 72 | public IQueryable<T> Find(int id)
|
|---|
| 73 | {
|
|---|
| 74 | return DataContext.GetTable<T>().Where(t => t.Id == id);
|
|---|
| 75 | }
|
|---|
| 76 | public IQueryable<T> Find(Expression<Func<T, bool>> expression)
|
|---|
| 77 | {
|
|---|
| 78 | return DataContext.GetTable<T>().Where(expression);
|
|---|
| 79 | }
|
|---|
| 80 | public IQueryable<T> Find()
|
|---|
| 81 | {
|
|---|
| 82 | return DataContext.GetTable<T>();
|
|---|
| 83 | }
|
|---|
| 84 | public IQueryable<vPlatnosciEcard> FindInvoiceByNipNumber(string nip, string numer)
|
|---|
| 85 | {
|
|---|
| 86 | return DataContext.FindInvoiceByNipNumber(nip, numer);
|
|---|
| 87 | }
|
|---|
| 88 | public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
|
|---|
| 89 | {
|
|---|
| 90 | return DataContext.FindItemsByIdFaktury(idFaktury);
|
|---|
| 91 | }
|
|---|
| 92 | public int GetOrdernumber(string description, int? idfaktury, DateTime? data)
|
|---|
| 93 | {
|
|---|
| 94 | return DataContext.GetOrdernumber(description, idfaktury, data);
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|