Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using System.Linq;
11 : using System.Linq.Expressions;
12 : using Cqrs.Azure.DocumentDb.DataStores;
13 : using Cqrs.Azure.DocumentDb.Entities;
14 : using Cqrs.DataStores;
15 : using Cqrs.Repositories;
16 : using Cqrs.Repositories.Queries;
17 :
18 : namespace Cqrs.Azure.DocumentDb.Repositories
19 : {
20 : /// <summary>
21 : /// Provides basic repository methods for operations with an <see cref="IDataStore{TData}"/> using Azure DocumentDB (CosmosDB).
22 : /// </summary>
23 : /// <typeparam name="TQueryStrategy">The <see cref="Type"/> of <see cref="IQueryStrategy"/>.</typeparam>
24 : /// <typeparam name="TQueryBuilder">The <see cref="Type"/> of the <see cref="QueryBuilder{TQueryStrategy, TData}"/> that will be used to build queries.</typeparam>
25 : /// <typeparam name="TData">The <see cref="Type"/> of data held in storage.</typeparam>
26 : public abstract class AzureRepository<TQueryStrategy, TQueryBuilder, TData> : Repository<TQueryStrategy, TQueryBuilder, TData>
27 : where TQueryStrategy : IQueryStrategy
28 : where TQueryBuilder : QueryBuilder<TQueryStrategy, TData>
29 : where TData : AzureDocumentDbEntity
30 1 : {
31 : /// <summary>
32 : /// Instantiates a new instance of <see cref="AzureRepository{TQueryStrategy,TQueryBuilder,TData}"/>
33 : /// </summary>
34 1 : protected AzureRepository(Func<IDataStore<TData>> createDataStoreFunction, TQueryBuilder queryBuilder)
35 : :base(createDataStoreFunction, queryBuilder)
36 : {
37 : }
38 :
39 : /// <summary>
40 : /// Load the <typeparamref name="TData"/> from Azure DocumentDB (CosmosDB) identified by the provided <paramref name="rsn"/>.
41 : /// </summary>
42 : /// <param name="rsn">The identifier if the <typeparamref name="TData"/> to load.</param>
43 : /// <param name="throwExceptionOnMissingEntity">If true will throw an <see cref="Exception"/> if no data is found in storage.</param>
44 1 : public override TData Load(Guid rsn, bool throwExceptionOnMissingEntity = true)
45 : {
46 : using (var dataStore = CreateDataStoreFunction() as AzureDocumentDbDataStore<TData>)
47 : {
48 : if (throwExceptionOnMissingEntity)
49 : return dataStore.Single(entity => entity.Rsn == rsn);
50 : return dataStore.SingleOrDefault(entity => entity.Rsn == rsn);
51 : }
52 : }
53 :
54 : /// <summary>
55 : /// Calls <see cref="Repository{TQueryStrategy,TQueryBuilder,TData}.CreateDataStoreFunction"/> passing the <paramref name="predicate"/>.
56 : /// </summary>
57 : /// <param name="predicate">A function defining a filter if required.</param>
58 1 : protected override IQueryable<TData> CreateQueryable(Expression<Func<TData, bool>> predicate)
59 : {
60 : return CreateDataStoreFunction().AsQueryable().Where(predicate);
61 : }
62 :
63 : }
64 : }
|