|
2 | 2 | using Microsoft.Extensions.DependencyInjection; |
3 | 3 | using Azure.Storage.Blobs; |
4 | 4 | using ManagedCode.Storage.Core.Builders; |
| 5 | +using System.Reflection.Emit; |
| 6 | +using System.Reflection; |
5 | 7 |
|
6 | 8 | namespace ManagedCode.Storage.Azure.Extensions |
7 | 9 | { |
8 | 10 | public static class ProviderExtensions |
9 | 11 | { |
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 |
11 | 16 | { |
12 | 17 | var connectionOptions = new AzureBlobStorageConnectionOptions(); |
13 | 18 | action.Invoke(connectionOptions); |
14 | 19 |
|
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); |
17 | 32 |
|
| 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 | + |
18 | 41 | return providerBuilder; |
19 | 42 | } |
20 | 43 | } |
|
0 commit comments