Skip to content

Commit ecd3b20

Browse files
author
Jeff Treuting
committed
Repository as IEnumerable
Now you can use the repository directly in a foreach() statement and it will do a GetAll(). This might be useful for lookup repositories where you usually get all the info in the table for some purpose.
1 parent 6bea3b1 commit ecd3b20

5 files changed

Lines changed: 775 additions & 731 deletions

File tree

Lines changed: 183 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1,183 +1,183 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Linq.Expressions;
5-
using SharpRepository.Repository.Queries;
6-
using SharpRepository.Repository.Specifications;
7-
using SharpRepository.Repository.Transactions;
8-
9-
namespace SharpRepository.Repository
10-
{
11-
/// <summary>
12-
/// Repository that acesses <typeparamref name="T"/> entities and has a primary key of type <typeparamref name="TKey"/>
13-
/// </summary>
14-
/// <typeparam name="T">The entity type that the repository acts on.</typeparam>
15-
/// <typeparam name="TKey">The type of the primary key.</typeparam>
16-
public interface IRepository<T, in TKey> : IDisposable where T : class
17-
{
18-
/// <summary>
19-
/// Gives access to an IQueryable&lt;T&gt; for this repository. You can then use this to join with other IQueryable's for more complicated queries.
20-
/// </summary>
21-
/// <returns></returns>
22-
IQueryable<T> AsQueryable();
23-
24-
25-
/// <summary>
26-
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
27-
/// </summary>
28-
/// <param name="key">The primary key.</param>
29-
/// <returns>The entity that matches on the primary key</returns>
30-
T Get(TKey key);
31-
32-
/// <summary>
33-
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key and maps it to the result of type <typeparamref name="TResult"/>.
34-
/// </summary>
35-
/// <typeparam name="TResult">The type of the result.</typeparam>
36-
/// <param name="key">The primary key.</param>
37-
/// <param name="selector">The mapping selector that returns the result type.</param>
38-
/// <returns>The mapped entity based on the selector that matches on the primary key.</returns>
39-
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);
40-
41-
/// <summary>
42-
/// Gets all entities from the repository.
43-
/// </summary>
44-
/// <returns>All of the entities</returns>
45-
IEnumerable<T> GetAll();
46-
47-
/// <summary>
48-
/// Gets all entities from the repository and applies the query options (e.g. paging or sorting options)
49-
/// </summary>
50-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
51-
/// <returns></returns>
52-
IEnumerable<T> GetAll(IQueryOptions<T> queryOptions);
53-
54-
/// <summary>
55-
/// Gets all entities from the repository, applies the query options (e.g. paging or sorting options) and maps to a new types based on the selector.
56-
/// </summary>
57-
/// <typeparam name="TResult">The type of the result.</typeparam>
58-
/// <param name="selector">The mapping selector that returns the result type.</param>
59-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
60-
/// <returns></returns>
61-
IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
62-
63-
/// <summary>
64-
/// Finds a single entity that matches the predicate supplied using FirstOrDefault with an optional query option, like sorting options.
65-
/// </summary>
66-
/// <param name="predicate">The predicate used to match entities against.</param>
67-
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
68-
/// <returns></returns>
69-
T Find(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null);
70-
71-
/// <summary>
72-
/// Finds a single entity that matches the predicate supplied using FirstOrDefault with an optional query option, like sorting options, and maps it to a new type based on the selector.
73-
/// </summary>
74-
/// <typeparam name="TResult">The type of the result.</typeparam>
75-
/// <param name="predicate">The predicate used to match entities against.</param>
76-
/// <param name="selector">The mapping selector that returns the result type.</param>
77-
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
78-
/// <returns></returns>
79-
TResult Find<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
80-
81-
/// <summary>
82-
/// Finds a single entity that matches the specification criteria using FirstOrDefault with an optional query option, like sorting options.
83-
/// </summary>
84-
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
85-
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
86-
/// <returns></returns>
87-
T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null);
88-
89-
/// <summary>
90-
/// Finds a single entity that matches the specification criteria using FirstOrDefault with an optional query option, like sorting options, and maps to a new type based on the selector.
91-
/// </summary>
92-
/// <typeparam name="TResult">The type of the result.</typeparam>
93-
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
94-
/// <param name="selector">The mapping selector that returns the result type.</param>
95-
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
96-
/// <returns></returns>
97-
TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
98-
99-
/// <summary>
100-
/// Finds all entities that match the predicate with an optional query option applied (like paging or sorting).
101-
/// </summary>
102-
/// <param name="predicate">The predicate used to match entities against.</param>
103-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
104-
/// <returns></returns>
105-
IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null);
106-
107-
/// <summary>
108-
/// Finds all entities that match the predicate with an optional query option applied (like paging or sorting), and maps to a new type based on the selector provided.
109-
/// </summary>
110-
/// <typeparam name="TResult">The type of the result.</typeparam>
111-
/// <param name="predicate">The predicate used to match entities against.</param>
112-
/// <param name="selector">The mapping selector that returns the result type.</param>
113-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
114-
/// <returns></returns>
115-
IEnumerable<TResult> FindAll<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
116-
117-
/// <summary>
118-
/// Finds all entities that match the specification criteria with an optional query option applied (like paging or sorting).
119-
/// </summary>
120-
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
121-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
122-
/// <returns></returns>
123-
IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null);
124-
125-
/// <summary>
126-
/// Finds all entities that match the specification criteria with an optional query option applied (like paging or sorting), and maps to a new type based on the selector provided.
127-
/// </summary>
128-
/// <typeparam name="TResult">The type of the result.</typeparam>
129-
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
130-
/// <param name="selector">The mapping selector that returns the result type.</param>
131-
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
132-
/// <returns></returns>
133-
IEnumerable<TResult> FindAll<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
134-
135-
/// <summary>
136-
/// Adds the specified entity.
137-
/// </summary>
138-
/// <param name="entity">The entity.</param>
139-
void Add(T entity);
140-
141-
/// <summary>
142-
/// Adds the specified entities.
143-
/// </summary>
144-
/// <param name="entities">The entities.</param>
145-
void Add(IEnumerable<T> entities);
146-
147-
/// <summary>
148-
/// Updates the specified entity.
149-
/// </summary>
150-
/// <param name="entity">The entity.</param>
151-
void Update(T entity);
152-
153-
/// <summary>
154-
/// Updates the specified entities.
155-
/// </summary>
156-
/// <param name="entities">The entities.</param>
157-
void Update(IEnumerable<T> entities);
158-
159-
/// <summary>
160-
/// Deletes the specified entity.
161-
/// </summary>
162-
/// <param name="entity">The entity.</param>
163-
void Delete(T entity);
164-
165-
/// <summary>
166-
/// Deletes the specified entities.
167-
/// </summary>
168-
/// <param name="entities">The entities.</param>
169-
void Delete(IEnumerable<T> entities);
170-
171-
/// <summary>
172-
/// Deletes the specified entity by the primary key.
173-
/// </summary>
174-
/// <param name="key">The primary key.</param>
175-
void Delete(TKey key);
176-
177-
/// <summary>
178-
/// Begins a batch mode process. This allows multiple operations against the repository with the ability to commit or rollback.
179-
/// </summary>
180-
/// <returns></returns>
181-
IBatch<T> BeginBatch();
182-
}
183-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using SharpRepository.Repository.Queries;
6+
using SharpRepository.Repository.Specifications;
7+
using SharpRepository.Repository.Transactions;
8+
9+
namespace SharpRepository.Repository
10+
{
11+
/// <summary>
12+
/// Repository that acesses <typeparamref name="T"/> entities and has a primary key of type <typeparamref name="TKey"/>
13+
/// </summary>
14+
/// <typeparam name="T">The entity type that the repository acts on.</typeparam>
15+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
16+
public interface IRepository<T, in TKey> : IEnumerable<T>, IDisposable where T : class
17+
{
18+
/// <summary>
19+
/// Gives access to an IQueryable&lt;T&gt; for this repository. You can then use this to join with other IQueryable's for more complicated queries.
20+
/// </summary>
21+
/// <returns></returns>
22+
IQueryable<T> AsQueryable();
23+
24+
25+
/// <summary>
26+
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
27+
/// </summary>
28+
/// <param name="key">The primary key.</param>
29+
/// <returns>The entity that matches on the primary key</returns>
30+
T Get(TKey key);
31+
32+
/// <summary>
33+
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key and maps it to the result of type <typeparamref name="TResult"/>.
34+
/// </summary>
35+
/// <typeparam name="TResult">The type of the result.</typeparam>
36+
/// <param name="key">The primary key.</param>
37+
/// <param name="selector">The mapping selector that returns the result type.</param>
38+
/// <returns>The mapped entity based on the selector that matches on the primary key.</returns>
39+
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);
40+
41+
/// <summary>
42+
/// Gets all entities from the repository.
43+
/// </summary>
44+
/// <returns>All of the entities</returns>
45+
IEnumerable<T> GetAll();
46+
47+
/// <summary>
48+
/// Gets all entities from the repository and applies the query options (e.g. paging or sorting options)
49+
/// </summary>
50+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
51+
/// <returns></returns>
52+
IEnumerable<T> GetAll(IQueryOptions<T> queryOptions);
53+
54+
/// <summary>
55+
/// Gets all entities from the repository, applies the query options (e.g. paging or sorting options) and maps to a new types based on the selector.
56+
/// </summary>
57+
/// <typeparam name="TResult">The type of the result.</typeparam>
58+
/// <param name="selector">The mapping selector that returns the result type.</param>
59+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
60+
/// <returns></returns>
61+
IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
62+
63+
/// <summary>
64+
/// Finds a single entity that matches the predicate supplied using FirstOrDefault with an optional query option, like sorting options.
65+
/// </summary>
66+
/// <param name="predicate">The predicate used to match entities against.</param>
67+
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
68+
/// <returns></returns>
69+
T Find(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null);
70+
71+
/// <summary>
72+
/// Finds a single entity that matches the predicate supplied using FirstOrDefault with an optional query option, like sorting options, and maps it to a new type based on the selector.
73+
/// </summary>
74+
/// <typeparam name="TResult">The type of the result.</typeparam>
75+
/// <param name="predicate">The predicate used to match entities against.</param>
76+
/// <param name="selector">The mapping selector that returns the result type.</param>
77+
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
78+
/// <returns></returns>
79+
TResult Find<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
80+
81+
/// <summary>
82+
/// Finds a single entity that matches the specification criteria using FirstOrDefault with an optional query option, like sorting options.
83+
/// </summary>
84+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
85+
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
86+
/// <returns></returns>
87+
T Find(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null);
88+
89+
/// <summary>
90+
/// Finds a single entity that matches the specification criteria using FirstOrDefault with an optional query option, like sorting options, and maps to a new type based on the selector.
91+
/// </summary>
92+
/// <typeparam name="TResult">The type of the result.</typeparam>
93+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
94+
/// <param name="selector">The mapping selector that returns the result type.</param>
95+
/// <param name="queryOptions">The query options to use (usually sorting) since this uses FirstOrDefault.</param>
96+
/// <returns></returns>
97+
TResult Find<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
98+
99+
/// <summary>
100+
/// Finds all entities that match the predicate with an optional query option applied (like paging or sorting).
101+
/// </summary>
102+
/// <param name="predicate">The predicate used to match entities against.</param>
103+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
104+
/// <returns></returns>
105+
IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null);
106+
107+
/// <summary>
108+
/// Finds all entities that match the predicate with an optional query option applied (like paging or sorting), and maps to a new type based on the selector provided.
109+
/// </summary>
110+
/// <typeparam name="TResult">The type of the result.</typeparam>
111+
/// <param name="predicate">The predicate used to match entities against.</param>
112+
/// <param name="selector">The mapping selector that returns the result type.</param>
113+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
114+
/// <returns></returns>
115+
IEnumerable<TResult> FindAll<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
116+
117+
/// <summary>
118+
/// Finds all entities that match the specification criteria with an optional query option applied (like paging or sorting).
119+
/// </summary>
120+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
121+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
122+
/// <returns></returns>
123+
IEnumerable<T> FindAll(ISpecification<T> criteria, IQueryOptions<T> queryOptions = null);
124+
125+
/// <summary>
126+
/// Finds all entities that match the specification criteria with an optional query option applied (like paging or sorting), and maps to a new type based on the selector provided.
127+
/// </summary>
128+
/// <typeparam name="TResult">The type of the result.</typeparam>
129+
/// <param name="criteria">The specification criteria that is used for matching entities against.</param>
130+
/// <param name="selector">The mapping selector that returns the result type.</param>
131+
/// <param name="queryOptions">The query options to apply like paging or sorting.</param>
132+
/// <returns></returns>
133+
IEnumerable<TResult> FindAll<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector, IQueryOptions<T> queryOptions = null);
134+
135+
/// <summary>
136+
/// Adds the specified entity.
137+
/// </summary>
138+
/// <param name="entity">The entity.</param>
139+
void Add(T entity);
140+
141+
/// <summary>
142+
/// Adds the specified entities.
143+
/// </summary>
144+
/// <param name="entities">The entities.</param>
145+
void Add(IEnumerable<T> entities);
146+
147+
/// <summary>
148+
/// Updates the specified entity.
149+
/// </summary>
150+
/// <param name="entity">The entity.</param>
151+
void Update(T entity);
152+
153+
/// <summary>
154+
/// Updates the specified entities.
155+
/// </summary>
156+
/// <param name="entities">The entities.</param>
157+
void Update(IEnumerable<T> entities);
158+
159+
/// <summary>
160+
/// Deletes the specified entity.
161+
/// </summary>
162+
/// <param name="entity">The entity.</param>
163+
void Delete(T entity);
164+
165+
/// <summary>
166+
/// Deletes the specified entities.
167+
/// </summary>
168+
/// <param name="entities">The entities.</param>
169+
void Delete(IEnumerable<T> entities);
170+
171+
/// <summary>
172+
/// Deletes the specified entity by the primary key.
173+
/// </summary>
174+
/// <param name="key">The primary key.</param>
175+
void Delete(TKey key);
176+
177+
/// <summary>
178+
/// Begins a batch mode process. This allows multiple operations against the repository with the ability to commit or rollback.
179+
/// </summary>
180+
/// <returns></returns>
181+
IBatch<T> BeginBatch();
182+
}
183+
}

0 commit comments

Comments
 (0)