|
| 1 | +namespace DotNetToolkit.Repository.AzureStorageBlob.Internal |
| 2 | +{ |
| 3 | + using Configuration; |
| 4 | + using Configuration.Conventions; |
| 5 | + using Extensions; |
| 6 | + using Microsoft.Azure.Storage; |
| 7 | + using Microsoft.Azure.Storage.Blob; |
| 8 | + using Newtonsoft.Json; |
| 9 | + using Queries; |
| 10 | + using Queries.Strategies; |
| 11 | + using System.Collections.Generic; |
| 12 | + using System.Configuration; |
| 13 | + using System.Linq; |
| 14 | + using Utility; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// An implementation of <see cref="IAzureStorageBlobRepositoryContext" />. |
| 18 | + /// </summary> |
| 19 | + /// <seealso cref="IAzureStorageBlobRepositoryContext" /> |
| 20 | + internal class AzureStorageBlobRepositoryContext : LinqRepositoryContextBase, IAzureStorageBlobRepositoryContext |
| 21 | + { |
| 22 | + #region Properties |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gest the cloud blob container. |
| 26 | + /// </summary> |
| 27 | + public CloudBlobContainer BlobContainer { get; } |
| 28 | + |
| 29 | + #endregion |
| 30 | + |
| 31 | + #region Constructors |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Initializes a new instance of the <see cref="AzureStorageBlobRepositoryContext" /> class. |
| 35 | + /// </summary> |
| 36 | + /// <param name="nameOrConnectionString">Either the database name or a connection string.</param> |
| 37 | + /// <param name="container">The name of the container.</param> |
| 38 | + /// <param name="createIfNotExists">Creates the container if it does not exist.</param> |
| 39 | + public AzureStorageBlobRepositoryContext(string nameOrConnectionString, string container = null, bool createIfNotExists = false) |
| 40 | + { |
| 41 | + Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString)); |
| 42 | + |
| 43 | + Conventions = RepositoryConventions.Default<AzureStorageBlobRepositoryContext>(); |
| 44 | + |
| 45 | + var css = GetConnectionStringSettings(nameOrConnectionString); |
| 46 | + |
| 47 | + var connectionString = css != null |
| 48 | + ? css.ConnectionString |
| 49 | + : nameOrConnectionString; |
| 50 | + |
| 51 | + var account = CloudStorageAccount.Parse(connectionString); |
| 52 | + var client = account.CreateCloudBlobClient(); |
| 53 | + |
| 54 | + if (string.IsNullOrEmpty(container)) |
| 55 | + container = GetType().Name.ToLower(); |
| 56 | + |
| 57 | + BlobContainer = client.GetContainerReference(container); |
| 58 | + |
| 59 | + if (createIfNotExists) |
| 60 | + BlobContainer.CreateIfNotExists(); |
| 61 | + } |
| 62 | + |
| 63 | + #endregion |
| 64 | + |
| 65 | + #region Private Methods |
| 66 | + |
| 67 | + private static ConnectionStringSettings GetConnectionStringSettings(string nameOrConnectionString) |
| 68 | + { |
| 69 | + var css = ConfigurationManager.ConnectionStrings[nameOrConnectionString]; |
| 70 | + |
| 71 | + if (css != null) |
| 72 | + return css; |
| 73 | + |
| 74 | + for (var i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++) |
| 75 | + { |
| 76 | + css = ConfigurationManager.ConnectionStrings[i]; |
| 77 | + |
| 78 | + if (css.ConnectionString.Equals(nameOrConnectionString)) |
| 79 | + return css; |
| 80 | + } |
| 81 | + |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private CloudBlockBlob GetBlobkBlobReference(params object[] keyValues) |
| 86 | + { |
| 87 | + var key = string.Join(":", keyValues); |
| 88 | + var blob = BlobContainer.GetBlockBlobReference(key); |
| 89 | + |
| 90 | + return blob; |
| 91 | + } |
| 92 | + |
| 93 | + private CloudBlockBlob GetBlobkBlobReference<TEntity>(TEntity entity) where TEntity : class |
| 94 | + { |
| 95 | + return GetBlobkBlobReference(Conventions.GetPrimaryKeyValues(entity)); |
| 96 | + } |
| 97 | + |
| 98 | + private IEnumerable<CloudBlockBlob> GetBlobs() |
| 99 | + { |
| 100 | + BlobContinuationToken continuationToken = null; |
| 101 | + |
| 102 | + do |
| 103 | + { |
| 104 | + var response = BlobContainer.ListBlobsSegmented(continuationToken); |
| 105 | + |
| 106 | + continuationToken = response.ContinuationToken; |
| 107 | + |
| 108 | + foreach (var blob in response.Results.OfType<CloudBlockBlob>()) |
| 109 | + { |
| 110 | + yield return blob; |
| 111 | + } |
| 112 | + |
| 113 | + } while (continuationToken != null); |
| 114 | + } |
| 115 | + |
| 116 | + private static string Serialize(object o) |
| 117 | + { |
| 118 | + if (o == null) |
| 119 | + return null; |
| 120 | + |
| 121 | + return JsonConvert.SerializeObject(o, new JsonSerializerSettings() |
| 122 | + { |
| 123 | + ReferenceLoopHandling = ReferenceLoopHandling.Ignore |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + private static T Deserialize<T>(string v) |
| 128 | + { |
| 129 | + if (string.IsNullOrEmpty(v)) |
| 130 | + return default(T); |
| 131 | + |
| 132 | + return JsonConvert.DeserializeObject<T>(v); |
| 133 | + } |
| 134 | + |
| 135 | + #endregion |
| 136 | + |
| 137 | + #region Overrides of LinqRepositoryContextBase |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// Returns the entity's query. |
| 141 | + /// </summary> |
| 142 | + /// <typeparam name="TEntity">The type of the of the entity.</typeparam> |
| 143 | + /// <returns>The entity's query.</returns> |
| 144 | + protected override IQueryable<TEntity> AsQueryable<TEntity>() |
| 145 | + { |
| 146 | + return GetBlobs() |
| 147 | + .Select(x => |
| 148 | + { |
| 149 | + try |
| 150 | + { |
| 151 | + return Deserialize<TEntity>(x.DownloadText()); |
| 152 | + } |
| 153 | + catch (StorageException storageException) |
| 154 | + { |
| 155 | + if (storageException.RequestInformation.HttpStatusCode == 404) |
| 156 | + return default(TEntity); |
| 157 | + |
| 158 | + throw; |
| 159 | + } |
| 160 | + }) |
| 161 | + .AsQueryable(); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Apply a fetching options to the specified entity's query. |
| 166 | + /// </summary> |
| 167 | + /// <returns>The entity's query with the applied options.</returns> |
| 168 | + protected override IQueryable<TEntity> ApplyFetchingOptions<TEntity>(IQueryable<TEntity> query, IQueryOptions<TEntity> options) |
| 169 | + { |
| 170 | + if (options?.FetchStrategy?.PropertyPaths?.Any() == true) |
| 171 | + Logger.Debug("The azure storage blob context does not support fetching strategy."); |
| 172 | + |
| 173 | + return query; |
| 174 | + } |
| 175 | + |
| 176 | + /// <summary> |
| 177 | + /// Tracks the specified entity in memory and will be inserted into the database when <see cref="SaveChanges" /> is called. |
| 178 | + /// </summary> |
| 179 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 180 | + /// <param name="entity">The entity.</param> |
| 181 | + public override void Add<TEntity>(TEntity entity) |
| 182 | + { |
| 183 | + Guard.NotNull(entity, nameof(entity)); |
| 184 | + |
| 185 | + var blob = GetBlobkBlobReference(entity); |
| 186 | + |
| 187 | + blob.UploadText(Serialize(entity)); |
| 188 | + } |
| 189 | + |
| 190 | + /// <summary> |
| 191 | + /// Tracks the specified entity in memory and will be updated in the database when <see cref="SaveChanges" /> is called. |
| 192 | + /// </summary> |
| 193 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 194 | + /// <param name="entity">The entity.</param> |
| 195 | + public override void Update<TEntity>(TEntity entity) |
| 196 | + { |
| 197 | + Guard.NotNull(entity, nameof(entity)); |
| 198 | + |
| 199 | + var blob = GetBlobkBlobReference(entity); |
| 200 | + |
| 201 | + blob.UploadText(Serialize(entity)); |
| 202 | + } |
| 203 | + |
| 204 | + /// <summary> |
| 205 | + /// Tracks the specified entity in memory and will be removed from the database when <see cref="SaveChanges" /> is called. |
| 206 | + /// </summary> |
| 207 | + /// <typeparam name="TEntity">The type of the entity.</typeparam> |
| 208 | + /// <param name="entity">The entity.</param> |
| 209 | + public override void Remove<TEntity>(TEntity entity) |
| 210 | + { |
| 211 | + Guard.NotNull(entity, nameof(entity)); |
| 212 | + |
| 213 | + var blob = GetBlobkBlobReference(entity); |
| 214 | + |
| 215 | + blob.DeleteIfExists(); |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// Saves all changes made in this context to the database. |
| 220 | + /// </summary> |
| 221 | + /// <returns>The number of state entries written to the database.</returns> |
| 222 | + public override int SaveChanges() |
| 223 | + { |
| 224 | + return -1; |
| 225 | + } |
| 226 | + |
| 227 | + /// <summary> |
| 228 | + /// Finds an entity with the given primary key values in the repository. |
| 229 | + /// </summary> |
| 230 | + /// <typeparam name="TEntity">The type of the of the entity.</typeparam> |
| 231 | + /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param> |
| 232 | + /// <param name="keyValues">The values of the primary key for the entity to be found.</param> |
| 233 | + /// <returns>The entity found in the repository.</returns> |
| 234 | + public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy, params object[] keyValues) |
| 235 | + { |
| 236 | + Guard.NotEmpty(keyValues, nameof(keyValues)); |
| 237 | + |
| 238 | + try |
| 239 | + { |
| 240 | + var blob = GetBlobkBlobReference(keyValues); |
| 241 | + |
| 242 | + return Deserialize<TEntity>(blob.DownloadText()); |
| 243 | + } |
| 244 | + catch (StorageException storageException) |
| 245 | + { |
| 246 | + if (storageException.RequestInformation.HttpStatusCode == 404) |
| 247 | + return default(TEntity); |
| 248 | + |
| 249 | + throw; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + #endregion |
| 254 | + } |
| 255 | +} |
0 commit comments