Skip to content

Commit f906c67

Browse files
author
dudchenko610
committed
finished first working vesrion of library binding
1 parent 2c5a175 commit f906c67

File tree

7 files changed

+62
-39
lines changed

7 files changed

+62
-39
lines changed

ManagedCode.Storage.Azure/AzureBlobStorage.cs

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

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

ManagedCode.Storage.Azure/Extensions/ProviderExtensions.cs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using System;
22
using Microsoft.Extensions.DependencyInjection;
3-
using Azure.Storage.Blobs;
43
using ManagedCode.Storage.Core.Builders;
5-
using System.Reflection.Emit;
6-
using System.Reflection;
4+
using ManagedCode.Storage.Core.Helpers;
5+
using ManagedCode.Storage.Core;
76

87
namespace ManagedCode.Storage.Azure.Extensions
98
{
@@ -12,32 +11,17 @@ public static class ProviderExtensions
1211
public static ProviderBuilder AddAzureBlobStorage<TAzureStorage>(
1312
this ProviderBuilder providerBuilder,
1413
Action<AzureBlobStorageConnectionOptions> action)
15-
where TAzureStorage : IAzureBlobStorage
14+
where TAzureStorage : IBlobStorage
1615
{
1716
var connectionOptions = new AzureBlobStorageConnectionOptions();
1817
action.Invoke(connectionOptions);
1918

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);
19+
var implementationType = TypeHelpers.GetImplementationType<TAzureStorage, AzureBlobStorage, AzureBlobStorageConnectionOptions>();
20+
providerBuilder.ServiceCollection.AddScoped(typeof(TAzureStorage), x => Activator.CreateInstance(implementationType, connectionOptions));
3221

33-
tb.SetParent(typeof(AzureBlobStorage));
34-
tb.AddInterfaceImplementation(typeof(TAzureStorage));
22+
// Because of AzureBlobStorage does not inherits TAzureStorage, DI complains on unability of casting
23+
// providerBuilder.ServiceCollection.AddScoped(typeof(TAzureStorage), x => new AzureBlobStorage(connectionOptions));
3524

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-
4125
return providerBuilder;
4226
}
4327
}

ManagedCode.Storage.Azure/IAzureBlobStorage.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.Reflection;
3+
using System.Reflection.Emit;
4+
5+
namespace ManagedCode.Storage.Core.Helpers
6+
{
7+
public static class TypeHelpers
8+
{
9+
public static Type GetImplementationType<TAbstraction, TImplementation, TOptions>()
10+
where TAbstraction : IBlobStorage
11+
{
12+
var typeSignature = typeof(TAbstraction).Name;
13+
var an = new AssemblyName(typeSignature);
14+
AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(an, AssemblyBuilderAccess.Run);
15+
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("ManagedCodeCModule");
16+
TypeBuilder tb = moduleBuilder.DefineType(typeSignature,
17+
TypeAttributes.Public |
18+
TypeAttributes.Class |
19+
TypeAttributes.AutoClass |
20+
TypeAttributes.AnsiClass |
21+
TypeAttributes.BeforeFieldInit |
22+
TypeAttributes.AutoLayout,
23+
null);
24+
25+
tb.SetParent(typeof(TImplementation));
26+
tb.AddInterfaceImplementation(typeof(TAbstraction));
27+
28+
var newConstructor = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard,
29+
new Type[] { typeof(TOptions) });
30+
31+
var baseConstructors = typeof(TImplementation).GetConstructors(
32+
BindingFlags.Public |
33+
BindingFlags.NonPublic |
34+
BindingFlags.Instance);
35+
36+
var emitter = newConstructor.GetILGenerator();
37+
emitter.Emit(OpCodes.Nop);
38+
39+
// Load `this` and call base constructor with arguments
40+
emitter.Emit(OpCodes.Ldarg_0);
41+
emitter.Emit(OpCodes.Ldarg, 1);
42+
emitter.Emit(OpCodes.Call, baseConstructors[0]);
43+
44+
emitter.Emit(OpCodes.Ret);
45+
46+
return tb.CreateType();
47+
}
48+
}
49+
}

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

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

33-
<ItemGroup>
34-
<Folder Include="Helpers\" />
35-
</ItemGroup>
36-
3733
</Project>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using ManagedCode.Storage.Azure;
2+
using ManagedCode.Storage.Core;
23

34
namespace ManagedCode.Storage.Tests.Azure
45
{
5-
public interface IDocumentStorage : IAzureBlobStorage
6+
public interface IDocumentStorage : IBlobStorage
67
{
78
}
89
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using ManagedCode.Storage.Azure;
2+
using ManagedCode.Storage.Core;
23

34
namespace ManagedCode.Storage.Tests.Azure
45
{
5-
public interface IPhotoStorage : IAzureBlobStorage
6+
public interface IPhotoStorage : IBlobStorage
67
{
78
}
89
}

0 commit comments

Comments
 (0)