root/trunk/eCard/eCardMVC/adMoto.Payments.Core/Repository.cs

Wersja 970, 3.2 KB (wprowadzona przez marek, 16 years temu)

re #215 - dodanie nowego projektu z lepsza nazwa i typem projektu

Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Linq.Expressions;
5using adMoto.Payments.Core.Data;
6using adMoto.Payments.Core.Interfaces;
7
8
9namespace adMoto.Payments.Core
10{
11    /// <summary>
12    /// An implementation of the Repository which uses LINQ to SQL Server to persist its data
13    /// </summary>
14    /// <typeparam name="T">The generic type representing the entity we are dealing with</typeparam>
15    /// <remarks>We use a 1 to 1 mapping of entity -> SQL table</remarks>
16    public class Repository<T> : IRepository<T> where T : class, IIdentifiable
17    {
18        protected readonly IDataContext DataContext;
19
20        public Repository(IDataContext dataContext)
21        {
22            DataContext = dataContext;
23        } 
24        public int Count()
25        {
26            return DataContext.GetTable<T>().Count();
27        }
28        public int Count(Expression<Func<T, bool>> expression)
29        {
30            return DataContext.GetTable<T>().Count(expression);
31        }
32        public T FindOne(int id)
33        {
34            return FindOne(t => t.Id == id);           
35        }       
36        public T FindOne(Expression<Func<T, bool>> expression)
37        {
38            return DataContext.GetTable<T>().Where(expression).SingleOrDefault();
39        }
40
41        public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
42        {
43            entity = FindOne(expression);
44
45            return (entity != null);
46        }
47        public bool Exists(Expression<Func<T, bool>> expression)
48        {
49            var entity = FindOne(expression);
50
51            return (entity != null);
52        }
53        public IList<T> FindAll()
54        {
55            return DataContext.GetTable<T>().ToList();
56        }
57        public IList<T> FindAll(Expression<Func<T, bool>> expression)
58        {
59            return DataContext.GetTable<T>().Where(expression).ToList();
60        }
61        public void Insert(T entity)
62        {
63            DataContext.Insert(entity);           
64        }
65        public void Delete(T entity)
66        {
67            DataContext.Delete(entity);
68        }
69        public void SubmitChanges()
70        {
71            DataContext.SubmitChanges();
72        }
73        public IQueryable<T> Find(int id)
74        {
75            return DataContext.GetTable<T>().Where(t => t.Id == id);
76        }
77        public IQueryable<T> Find(Expression<Func<T, bool>> expression)
78        {
79            return DataContext.GetTable<T>().Where(expression);       
80        }
81        public IQueryable<T> Find()
82        {
83            return DataContext.GetTable<T>();
84        }
85        public IQueryable<Invoice> FindInvoiceByNipNumber(string nip, string numer)
86        {
87            return DataContext.FindInvoiceByNipNumber(nip, numer);
88        }
89        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
90        {
91            return DataContext.FindItemsByIdFaktury(idFaktury);
92        }
93        public int GetOrdernumber(string description, int? idfaktury, DateTime? data)
94        {
95            return DataContext.GetOrdernumber(description, idfaktury, data);
96        }
97    }
98}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.