root/trunk/eCard/eCardMVC/Platnosci.Core/Linq/Repository.cs @ 872

Wersja 872, 3.1 KB (wprowadzona przez alina, 16 years temu)

re #215

Line 
1using System;
2using System.Collections.Generic;
3using System.Data.Linq;
4using System.Linq;
5using System.Linq.Expressions;
6using System.Text;
7using Platnosci.Core.Interface;
8
9
10namespace 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            _dataContext.Insert(entity);
69        }
70        public void Update(T entity)
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 IQueryable<vPlatnosciEcard> FindInvoiceById(int id)
91        {
92            return _dataContext.FindInvoiceById(id);
93        }
94        public List<PotwierdzeniaEcard> FindItemsByIdFaktury(int idFaktury)
95        {
96            return _dataContext.FindItemsByIdFaktury(idFaktury);
97        }
98    }
99}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.