1818
1919 internal class AzureStorageBlobRepositoryContext : LinqEnumerableRepositoryContextBase , IAzureStorageBlobRepositoryContext
2020 {
21+ #region Fields
22+
23+ private readonly IAzureStorageBlobContainerNameBuilder _containerNameBuilder ;
24+ private readonly bool _createContainerIfNotExists ;
25+
26+ #endregion
27+
2128 #region Constructors
2229
23- public AzureStorageBlobRepositoryContext ( string connectionString , string container = null , bool createIfNotExists = false )
30+ public AzureStorageBlobRepositoryContext ( string connectionString , IAzureStorageBlobContainerNameBuilder containerNameBuilder = null , bool createIfNotExists = false )
2431 {
2532 Guard . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
2633
27- var client = new BlobServiceClient ( connectionString ) ;
28-
29- if ( string . IsNullOrEmpty ( container ) )
30- container = GetType ( ) . Name . ToLower ( ) ;
34+ _containerNameBuilder = containerNameBuilder ?? new DefaultContainerNameBuilder ( ) ;
35+ _createContainerIfNotExists = createIfNotExists ;
3136
32- BlobContainer = client . GetBlobContainerClient ( container ) ;
33-
34- if ( createIfNotExists )
35- BlobContainer . CreateIfNotExists ( ) ;
37+ Client = new BlobServiceClient ( connectionString ) ;
3638 }
3739
3840 #endregion
3941
4042 #region Private Methods
4143
44+ private BlobContainerClient GetBlobContainer < TEntity > ( )
45+ {
46+ var container = _containerNameBuilder . Build < TEntity > ( ) ;
47+ var blobContainer = Client . GetBlobContainerClient ( container ) ;
48+
49+ if ( _createContainerIfNotExists )
50+ blobContainer . CreateIfNotExists ( ) ;
51+
52+ return blobContainer ;
53+ }
54+
4255 private IAsyncEnumerable < TEntity > AsAsyncEnumerable < TEntity > ( ) where TEntity : class
4356 {
4457 return DownloadEntitiesAsync < TEntity > ( ) ;
@@ -48,15 +61,16 @@ private BlobClient GetBlobClient<TEntity>(TEntity entity) where TEntity : class
4861 {
4962 var keyValues = Conventions . GetPrimaryKeyValues ( entity ) ;
5063 var key = string . Join ( ":" , keyValues ) ;
64+ var blobContainer = GetBlobContainer < TEntity > ( ) ;
5165
52- return BlobContainer . GetBlobClient ( key ) ;
66+ return blobContainer . GetBlobClient ( key ) ;
5367 }
5468
55- private TEntity DownloadEntity < TEntity > ( string blobName ) where TEntity : class
69+ private TEntity DownloadEntity < TEntity > ( BlobContainerClient blobContainer , string blobName ) where TEntity : class
5670 {
5771 try
5872 {
59- var blob = BlobContainer . GetBlobClient ( blobName ) ;
73+ var blob = blobContainer . GetBlobClient ( blobName ) ;
6074 var contentResult = blob . DownloadContent ( ) ;
6175 var result = contentResult . Value . Content . ToObjectFromJson < TEntity > ( ) ;
6276
@@ -68,12 +82,41 @@ private TEntity DownloadEntity<TEntity>(string blobName) where TEntity : class
6882 }
6983 }
7084
71- private async Task < TEntity > DownloadEntityAsync < TEntity > ( string blobName , CancellationToken cancellationToken = default ) where TEntity : class
85+ private IEnumerable < TEntity > DownloadEntities < TEntity > ( ) where TEntity : class
86+ {
87+ var blobContainer = GetBlobContainer < TEntity > ( ) ;
88+ foreach ( var blobItem in blobContainer . GetBlobs ( ) )
89+ {
90+ yield return DownloadEntity < TEntity > ( blobContainer , blobItem . Name ) ;
91+ }
92+ }
93+
94+ private async Task < BlobContainerClient > GetBlobContainerAsync < TEntity > ( )
95+ {
96+ var container = _containerNameBuilder . Build < TEntity > ( ) ;
97+ var blobContainer = Client . GetBlobContainerClient ( container ) ;
98+
99+ if ( _createContainerIfNotExists )
100+ await blobContainer . CreateIfNotExistsAsync ( ) ;
101+
102+ return blobContainer ;
103+ }
104+
105+ private async Task < BlobClient > GetBlobClientAsync < TEntity > ( TEntity entity ) where TEntity : class
106+ {
107+ var keyValues = Conventions . GetPrimaryKeyValues ( entity ) ;
108+ var key = string . Join ( ":" , keyValues ) ;
109+ var blobContainer = await GetBlobContainerAsync < TEntity > ( ) ;
110+
111+ return blobContainer . GetBlobClient ( key ) ;
112+ }
113+
114+ private async Task < TEntity > DownloadEntityAsync < TEntity > ( BlobContainerClient blobContainer , string blobName , CancellationToken cancellationToken = default ) where TEntity : class
72115 {
73116 try
74117 {
75- var blob = BlobContainer . GetBlobClient ( blobName ) ;
76- var contentResult = await blob . DownloadContentAsync ( cancellationToken : cancellationToken ) ;
118+ var blob = blobContainer . GetBlobClient ( blobName ) ;
119+ var contentResult = await blob . DownloadContentAsync ( cancellationToken ) ;
77120 var result = contentResult . Value . Content . ToObjectFromJson < TEntity > ( ) ;
78121
79122 return result ;
@@ -84,19 +127,12 @@ private async Task<TEntity> DownloadEntityAsync<TEntity>(string blobName, Cancel
84127 }
85128 }
86129
87- private IEnumerable < TEntity > DownloadEntities < TEntity > ( ) where TEntity : class
88- {
89- foreach ( var blobItem in BlobContainer . GetBlobs ( ) )
90- {
91- yield return DownloadEntity < TEntity > ( blobItem . Name ) ;
92- }
93- }
94-
95130 private async IAsyncEnumerable < TEntity > DownloadEntitiesAsync < TEntity > ( ) where TEntity : class
96131 {
97- await foreach ( var blobItem in BlobContainer . GetBlobsAsync ( ) )
132+ var blobContainer = await GetBlobContainerAsync < TEntity > ( ) ;
133+ await foreach ( var blobItem in blobContainer . GetBlobsAsync ( ) )
98134 {
99- yield return DownloadEntity < TEntity > ( blobItem . Name ) ;
135+ yield return await DownloadEntityAsync < TEntity > ( blobContainer , blobItem . Name ) ;
100136 }
101137 }
102138
@@ -108,12 +144,12 @@ private void UploadEntity<TEntity>(TEntity entity) where TEntity : class
108144 blob . Upload ( binaryData , overwrite : true ) ;
109145 }
110146
111- private Task UploadEntityAsync < TEntity > ( TEntity entity , CancellationToken cancellationToken = new CancellationToken ( ) ) where TEntity : class
147+ private async Task UploadEntityAsync < TEntity > ( TEntity entity , CancellationToken cancellationToken = new CancellationToken ( ) ) where TEntity : class
112148 {
113- var blob = GetBlobClient ( entity ) ;
149+ var blob = await GetBlobClientAsync ( entity ) ;
114150 var binaryData = BinaryData . FromObjectAsJson < TEntity > ( entity ) ;
115151
116- return blob . UploadAsync ( binaryData , overwrite : true , cancellationToken ) ;
152+ await blob . UploadAsync ( binaryData , overwrite : true , cancellationToken ) ;
117153 }
118154
119155 #endregion
@@ -129,7 +165,7 @@ protected override IEnumerable<TEntity> AsEnumerable<TEntity>(IFetchQueryStrateg
129165
130166 #region Implementation of IAzureStorageBlobRepositoryContext
131167
132- public BlobContainerClient BlobContainer { get ; }
168+ public BlobServiceClient Client { get ; }
133169
134170 #endregion
135171
@@ -161,7 +197,8 @@ public override TEntity Find<TEntity>(IFetchQueryStrategy<TEntity> fetchStrategy
161197 Guard . NotEmpty ( keyValues , nameof ( keyValues ) ) ;
162198
163199 var key = string . Join ( ":" , keyValues ) ;
164- var result = DownloadEntity < TEntity > ( key ) ;
200+ var blobContainer = GetBlobContainer < TEntity > ( ) ;
201+ var result = DownloadEntity < TEntity > ( blobContainer , key ) ;
165202
166203 return result ;
167204 }
@@ -201,12 +238,13 @@ public Task<int> ExecuteSqlCommandAsync(string sql, CommandType cmdType, Diction
201238
202239 public Task < int > SaveChangesAsync ( CancellationToken cancellationToken = default ) => Task . FromResult ( - 1 ) ;
203240
204- public Task < TEntity > FindAsync < TEntity > ( CancellationToken cancellationToken , IFetchQueryStrategy < TEntity > fetchStrategy , params object [ ] keyValues ) where TEntity : class
241+ public async Task < TEntity > FindAsync < TEntity > ( CancellationToken cancellationToken , IFetchQueryStrategy < TEntity > fetchStrategy , params object [ ] keyValues ) where TEntity : class
205242 {
206243 Guard . NotEmpty ( keyValues , nameof ( keyValues ) ) ;
207244
208245 var key = string . Join ( ":" , keyValues ) ;
209- var result = DownloadEntityAsync < TEntity > ( key , cancellationToken ) ;
246+ var blobContainer = await GetBlobContainerAsync < TEntity > ( ) ;
247+ var result = await DownloadEntityAsync < TEntity > ( blobContainer , key , cancellationToken ) ;
210248
211249 return result ;
212250 }
@@ -233,7 +271,7 @@ public async Task<PagedQueryResult<IEnumerable<TResult>>> FindAllAsync<TEntity,
233271
234272 var selectorFunc = selector . Compile ( ) ;
235273
236- var query = DownloadEntitiesAsync < TEntity > ( )
274+ var query = AsAsyncEnumerable < TEntity > ( )
237275 . ApplySpecificationOptions ( options )
238276 . ApplySortingOptions ( Conventions , options ) ;
239277
@@ -279,7 +317,7 @@ public async Task<PagedQueryResult<Dictionary<TDictionaryKey, TElement>>> ToDict
279317 var keySelectFunc = keySelector . Compile ( ) ;
280318 var elementSelectorFunc = elementSelector . Compile ( ) ;
281319
282- var query = DownloadEntitiesAsync < TEntity > ( )
320+ var query = AsAsyncEnumerable < TEntity > ( )
283321 . ApplySpecificationOptions ( options )
284322 . ApplySortingOptions ( Conventions , options ) ;
285323
0 commit comments