Skip to content

Commit 2c5a175

Browse files
author
dudchenko610
committed
defined base approach to connect blob services
1 parent 9d0da60 commit 2c5a175

9 files changed

Lines changed: 96 additions & 7 deletions

File tree

ManagedCode.Storage.Azure/AzureBlobStorage.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
namespace ManagedCode.Storage.Azure
1010
{
11-
public class AzureBlobStorage : IBlobStorage
11+
public class AzureBlobStorage : IAzureBlobStorage
1212
{
13-
public AzureBlobStorage()
13+
public AzureBlobStorage(/*AzureBlobStorageConnectionOptions connectionOptions*/)
1414
{
15-
15+
// var blobServiceClient = new BlobServiceClient(connectionOptions.ConnectionString);
1616
}
1717

1818
public Task DeleteAsync(string blob, CancellationToken cancellationToken = default)

ManagedCode.Storage.Azure/AzureBlobStorageConnectionOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public class AzureBlobStorageConnectionOptions
44
{
55
public string ConnectionString { get; set; }
6+
public string Container { get; set; }
67
}
78
}

ManagedCode.Storage.Azure/Extensions/ProviderExtensions.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,42 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Azure.Storage.Blobs;
44
using ManagedCode.Storage.Core.Builders;
5+
using System.Reflection.Emit;
6+
using System.Reflection;
57

68
namespace ManagedCode.Storage.Azure.Extensions
79
{
810
public static class ProviderExtensions
911
{
10-
public static ProviderBuilder AddAzureBlobStorage(this ProviderBuilder providerBuilder, Action<AzureBlobStorageConnectionOptions> action)
12+
public static ProviderBuilder AddAzureBlobStorage<TAzureStorage>(
13+
this ProviderBuilder providerBuilder,
14+
Action<AzureBlobStorageConnectionOptions> action)
15+
where TAzureStorage : IAzureBlobStorage
1116
{
1217
var connectionOptions = new AzureBlobStorageConnectionOptions();
1318
action.Invoke(connectionOptions);
1419

15-
var blobServiceClient = new BlobServiceClient(connectionOptions.ConnectionString);
16-
providerBuilder.ServiceCollection.AddSingleton(blobServiceClient);
20+
var typeSignature = typeof(TAzureStorage).Name;
21+
var an = new AssemblyName(typeSignature);
22+
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
23+
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("MainModule");
24+
TypeBuilder tb = moduleBuilder.DefineType(typeSignature,
25+
TypeAttributes.Public |
26+
TypeAttributes.Class |
27+
TypeAttributes.AutoClass |
28+
TypeAttributes.AnsiClass |
29+
TypeAttributes.BeforeFieldInit |
30+
TypeAttributes.AutoLayout,
31+
null);
1732

33+
tb.SetParent(typeof(AzureBlobStorage));
34+
tb.AddInterfaceImplementation(typeof(TAzureStorage));
35+
36+
ConstructorBuilder constructor = tb.DefineDefaultConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName);
37+
Type implType = tb.CreateType();
38+
39+
providerBuilder.ServiceCollection.AddScoped(typeof(TAzureStorage), implType);
40+
1841
return providerBuilder;
1942
}
2043
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using ManagedCode.Storage.Core;
2+
3+
namespace ManagedCode.Storage.Azure
4+
{
5+
public interface IAzureBlobStorage : IBlobStorage
6+
{
7+
}
8+
}

ManagedCode.Storage.Core/ManagedCode.Storage.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,8 @@
3030
<None Include="..\logo.png" Pack="true" Visible="false" PackagePath="" />
3131
</ItemGroup>
3232

33+
<ItemGroup>
34+
<Folder Include="Helpers\" />
35+
</ItemGroup>
36+
3337
</Project>
Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,44 @@
1-

1+
using ManagedCode.Storage.Core.Extensions;
2+
using ManagedCode.Storage.Azure.Extensions;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
using FluentAssertions;
7+
28
namespace ManagedCode.Storage.Tests.Azure
39
{
410
public class DependencyInjectionTests
511
{
12+
private IPhotoStorage _photoStorage;
13+
private IDocumentStorage _documentStorage;
14+
15+
public DependencyInjectionTests()
16+
{
17+
18+
}
19+
20+
[Fact]
21+
public void WhenDIInitialized()
22+
{
23+
var services = new ServiceCollection();
24+
25+
services.AddManagedCodeStorage()
26+
.AddAzureBlobStorage<IPhotoStorage>(opt => {
27+
opt.ConnectionString = "";
28+
opt.Container = "photos";
29+
})
30+
.AddAzureBlobStorage<IDocumentStorage>(opt => {
31+
opt.ConnectionString = "";
32+
opt.Container = "documents";
33+
});
34+
35+
var provider = services.BuildServiceProvider();
36+
37+
_photoStorage = provider.GetService<IPhotoStorage>();
38+
_documentStorage = provider.GetService<IDocumentStorage>();
639

40+
_photoStorage.Should().NotBeNull();
41+
_documentStorage.Should().NotBeNull();
42+
}
743
}
844
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using ManagedCode.Storage.Azure;
2+
3+
namespace ManagedCode.Storage.Tests.Azure
4+
{
5+
public interface IDocumentStorage : IAzureBlobStorage
6+
{
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using ManagedCode.Storage.Azure;
2+
3+
namespace ManagedCode.Storage.Tests.Azure
4+
{
5+
public interface IPhotoStorage : IAzureBlobStorage
6+
{
7+
}
8+
}

ManagedCode.Storage.Tests/ManagedCode.Storage.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="FluentAssertions" Version="5.10.3" />
12+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
1314
<PackageReference Include="System.Linq.Async" Version="5.0.0" />
1415
<PackageReference Include="xunit" Version="2.4.1" />

0 commit comments

Comments
 (0)