root/trunk/Wierszowki/Wierszowki.Model/Linq/LinqRepository.cs @ 842

Wersja 823, 5.6 KB (wprowadzona przez alina, 17 years temu)

re #193,#207 dodano usuwanie wierszówek i dynamiczne listy DropDown?

Line 
1using System;
2using System.Collections.Generic;
3using System.Data.Linq;
4using System.Linq;
5using System.Linq.Expressions;
6using Wierszowki.Core.Interfaces;
7
8namespace Wierszowki.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 sealed class LinqRepository<T> : IRepository<T>
16      where T : class, IIdentifiable
17    {
18        /// <summary>
19        /// Gets the active LINQ DataContext which manages access to the persistance store
20        /// </summary>
21        private readonly WierszowkiDataContext _dataContext = new WierszowkiDataContext();
22
23        /// <summary>
24        /// Returns a count of the items in the repository
25        /// </summary>
26        /// <returns></returns>
27        public int Count()
28        {
29            return _dataContext.GetTable<T>().Count();
30        }
31
32        /// <summary>
33        /// Returns a count of the items in the repository which match the supplied lambda
34        /// </summary>
35        /// <param name="expression">A lambda expression which will be evaluated against the repository</param>
36        /// <returns></returns>
37        public int Count(Expression<Func<T, bool>> expression)
38        {
39            return _dataContext.GetTable<T>().Count(expression);
40        }
41
42        /// <summary>
43        /// Finds a single instance of an entity matching a unique identifier.
44        /// </summary>
45        /// <param name="id"></param>
46        /// <returns></returns>
47        public T FindOne(int id)
48        {
49            return FindOne(t => t.Id == id);
50        }
51
52        /// <summary>
53        /// Finds a single instance of an entity matching a lambda expression
54        /// </summary>
55        /// <param name="expression"></param>
56        /// <returns>
57        /// The first entity which matches the expession
58        /// </returns>
59        public T FindOne(Expression<Func<T, bool>> expression)
60        {
61            return _dataContext.GetTable<T>().Where(expression).SingleOrDefault();
62        }
63
64        public bool TryFindOne(Expression<Func<T, bool>> expression, out T entity)
65        {
66            entity = FindOne(expression);
67
68            return (entity != null);
69        }
70
71        /// <summary>
72        /// Checks if an entity exists matching a lambda expression
73        /// </summary>
74        /// <param name="expression"></param>
75        /// <returns>
76        /// True if an entity exists, otherwise False
77        /// </returns>
78        public bool Exists(Expression<Func<T, bool>> expression)
79        {
80            var entity = FindOne(expression);
81
82            return (entity != null);
83        }
84
85
86        /// <summary>
87        /// Finds all entities in the repository
88        /// </summary>
89        /// <returns></returns>
90        public IList<T> FindAll()
91        {
92            return _dataContext.GetTable<T>().ToList();
93        }
94
95        /// <summary>
96        /// Finds all entities matching a lambda expression
97        /// </summary>
98        /// <param name="expression">The expression.</param>
99        /// <returns></returns>
100        public IList<T> FindAll(Expression<Func<T, bool>> expression)
101        {
102            return _dataContext.GetTable<T>().Where(expression).ToList();
103        }
104
105        /// <summary>
106        /// Adds a new entity in to the repository
107        /// </summary>
108        /// <param name="entity"></param>
109        public void Insert(T entity)
110        {
111            //DataContext.GetTable<T>().InsertOnSubmit(entity);
112            //DataContext.SubmitChanges();
113
114            //using (DataContext dc = new WierszowkiDataContext())
115            //{
116                _dataContext.GetTable<T>().InsertOnSubmit(entity);
117                _dataContext.SubmitChanges();
118            //}
119        }
120
121        /// <summary>
122        /// Removes the specified entity from the repository
123        /// </summary>
124        /// <param name="entity"></param>
125        public void Delete(T entity)
126        {
127            if (entity != null)
128            {
129                _dataContext.GetTable<T>().DeleteOnSubmit(entity);
130                _dataContext.SubmitChanges();
131            }
132        }
133
134        /// <summary>
135        /// Saves the specified entity back to the repository
136        /// </summary>
137        /// <param name="entity"></param>
138        public void Update(T entity)
139        {
140            _dataContext.SubmitChanges();
141        }
142
143        /// <summary>
144        /// Returns a requeryable set of all entities in the repository matching a unique identifier
145        /// </summary>
146        /// <param name="id"></param>
147        /// <returns></returns>
148        public IQueryable<T> Find(int id)
149        {
150            return _dataContext.GetTable<T>().Where(t => t.Id == id);
151        }
152
153        /// <summary>
154        /// Returns a requeryable set of all entities in the repository matching a lambda expression
155        /// </summary>
156        /// <param name="expression"></param>
157        /// <returns></returns>
158        public IQueryable<T> Find(Expression<Func<T, bool>> expression)
159        {
160            return _dataContext.GetTable<T>().Where(expression);        }
161
162        /// <summary>
163        /// Returns a requeryable set of all entities in the repository
164        /// </summary>
165        /// <returns></returns>
166        public IQueryable<T> Find()
167        {
168            return _dataContext.GetTable<T>();
169        }
170    }
171}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.