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 Cqrs.DataStores;
11 : using Cqrs.Repositories;
12 : using Cqrs.Entities;
13 : using Cqrs.Repositories.Queries;
14 :
15 : namespace Cqrs.Azure.BlobStorage.Repositories
16 : {
17 : /// <summary>
18 : /// Provides basic repository methods for operations with an <see cref="IDataStore{TData}"/> using Azure Blob Storage.
19 : /// </summary>
20 : /// <typeparam name="TQueryStrategy">The <see cref="Type"/> of <see cref="IQueryStrategy"/>.</typeparam>
21 : /// <typeparam name="TQueryBuilder">The <see cref="Type"/> of the <see cref="QueryBuilder{TQueryStrategy, TData}"/> that will be used to build queries.</typeparam>
22 : /// <typeparam name="TData">The <see cref="Type"/> of data held in storage.</typeparam>
23 : public class BlobStorageRepository<TQueryStrategy, TQueryBuilder, TData> : Repository<TQueryStrategy, TQueryBuilder, TData>
24 : where TQueryStrategy : IQueryStrategy
25 : where TQueryBuilder : QueryBuilder<TQueryStrategy, TData>
26 : where TData : Entity, new()
27 1 : {
28 : /// <summary>
29 : /// Instantiates a new instance of <see cref="BlobStorageRepository{TQueryStrategy,TQueryBuilder,TData}"/>
30 : /// </summary>
31 1 : public BlobStorageRepository(Func<IDataStore<TData>> createDataStoreFunction, TQueryBuilder queryBuilder)
32 : : base(createDataStoreFunction, queryBuilder)
33 : {
34 : }
35 :
36 : #region Overrides of Repository<TQueryStrategy,TQueryBuilder,TData>
37 :
38 : /// <summary>
39 : /// Load the <typeparamref name="TData"/> from Azure Blob Storage identified by the provided <paramref name="rsn"/>.
40 : /// </summary>
41 : /// <param name="rsn">The identifier if the <typeparamref name="TData"/> to load.</param>
42 : /// <param name="throwExceptionOnMissingEntity">If true will throw an <see cref="Exception"/> if no data is found in storage.</param>
43 1 : public override TData Load(Guid rsn, bool throwExceptionOnMissingEntity = true)
44 : {
45 : using (IDataStore<TData> dataStore = CreateDataStoreFunction())
46 : {
47 : try
48 : {
49 : return dataStore.GetByName(rsn);
50 : }
51 : catch (InvalidOperationException exception)
52 : {
53 : if (throwExceptionOnMissingEntity && exception.Message == "Sequence contains no elements")
54 : throw;
55 : }
56 :
57 : return null;
58 : }
59 : }
60 :
61 : #endregion
62 : }
63 : }
|