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.Collections.Generic;
11 : using System.Linq;
12 : using cdmdotnet.Logging;
13 : using Cqrs.DataStores;
14 : using Cqrs.Entities;
15 :
16 : namespace Cqrs.Azure.BlobStorage.DataStores
17 : {
18 : /// <summary>
19 : /// A <see cref="IDataStore{TData}"/> that uses Azure Blob Storage for storage.
20 : /// </summary>
21 : /// <typeparam name="TData">The <see cref="Type"/> of <see cref="IEntity"/> the <see cref="IDataStore{TData}"/> will contain.</typeparam>
22 : public class BlobStorageDataStore<TData>
23 : : BlobStorageStore<TData>
24 : , IDataStore<TData>
25 : where TData : Entity
26 1 : {
27 : internal Func<string> GenerateFolderName { get; set; }
28 :
29 : /// <summary>
30 : /// Initializes a new instance of the <see cref="BlobStorage"/> class using the specified container.
31 : /// </summary>
32 1 : public BlobStorageDataStore(ILogger logger, IBlobStorageDataStoreConnectionStringFactory blobStorageDataStoreConnectionStringFactory)
33 : : base(logger)
34 : {
35 : GetContainerName = blobStorageDataStoreConnectionStringFactory.GetBaseContainerName;
36 : IsContainerPublic = blobStorageDataStoreConnectionStringFactory.IsContainerPublic<TData>;
37 : GenerateFolderName = blobStorageDataStoreConnectionStringFactory.GetEntityName<TData>;
38 : GenerateFileName = data => string.Format("{0}\\{1}", GenerateFolderName(), data.Rsn.ToString("N"));
39 :
40 : // ReSharper disable DoNotCallOverridableMethodsInConstructor
41 : Initialise(blobStorageDataStoreConnectionStringFactory);
42 : // ReSharper restore DoNotCallOverridableMethodsInConstructor
43 : }
44 :
45 : #region Implementation of IDataStore<TData>
46 :
47 : /// <summary>
48 : /// Will mark the <paramref name="data"/> as logically (or soft).
49 : /// </summary>
50 1 : public void Remove(TData data)
51 : {
52 : data.IsLogicallyDeleted = true;
53 : Update(data);
54 : }
55 :
56 : #endregion
57 :
58 : /// <summary>
59 : /// Get all <typeparamref name="TData"/> items in the folder.
60 : /// </summary>
61 1 : public virtual IEnumerable<TData> GetByFolder()
62 : {
63 : string folderName = GenerateFolderName();
64 : return OpenStreamsForReading(folderName: folderName)
65 : .Select(Deserialise);
66 : }
67 : }
68 : }
|