Skip to content

Commit aff63cb

Browse files
committed
implemented strong name support
1 parent a81f91d commit aff63cb

File tree

12 files changed

+124
-34
lines changed

12 files changed

+124
-34
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Ignore Visual Studio temporary files, build results, and
1+
## Ignore Visual Studio temporary files, build results, and
22
## files generated by popular Visual Studio add-ons.
33

44
# Ignore local web.config as it is built-in automatically
@@ -252,3 +252,4 @@ _Pvt_Extensions
252252
/.vscode/launch.json
253253

254254
# Ignore local nuget sources to force server sources
255+
SubSonic/MyKey.PublicKeyOnly.snk

SubSonic.Extensions.SqlServer/SubSonic.Extensions.SqlServer.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<RepositoryUrl>https://github.com/kccarter76/SubSonic-Core/tree/master/SubSonic.Extensions.Test</RepositoryUrl>
1818
<Version>4.1.0-alpha.1</Version>
1919
<NeutralLanguage>en</NeutralLanguage>
20+
<SignAssembly>true</SignAssembly>
21+
<DelaySign>false</DelaySign>
22+
<AssemblyOriginatorKeyFile>SubSonicStrongName.pfx</AssemblyOriginatorKeyFile>
2023
</PropertyGroup>
2124

2225
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

SubSonic.Extensions.Test/SubSonic.Extensions.Test.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<ApplicationIcon />
1919
<OutputType>Library</OutputType>
2020
<NeutralLanguage>en</NeutralLanguage>
21+
<SignAssembly>true</SignAssembly>
22+
<DelaySign>false</DelaySign>
23+
<AssemblyOriginatorKeyFile>SubSonicStrongName.pfx</AssemblyOriginatorKeyFile>
2124
</PropertyGroup>
2225

2326
<PropertyGroup Condition="'$(Platform)'=='AnyCPU'">

SubSonic.Tests/SubSonic.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<IsPackable>false</IsPackable>
66
<LangVersion>latest</LangVersion>
77
<Platforms>AnyCPU</Platforms>
8+
<SignAssembly>true</SignAssembly>
9+
<DelaySign>false</DelaySign>
10+
<AssemblyOriginatorKeyFile>SubSonicStrongName.pfx</AssemblyOriginatorKeyFile>
811
</PropertyGroup>
912

1013
<ItemGroup>

SubSonic/Data/DynamicProxies/DynamicProxy.cs

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
using SubSonic.Infrastructure;
1+
using Microsoft.Win32.SafeHandles;
2+
using SubSonic.Infrastructure;
23
using SubSonic.Infrastructure.Schema;
34
using System;
45
using System.Collections.Generic;
56
using System.Data;
7+
using System.IO;
68
using System.Reflection;
79
using System.Reflection.Emit;
810
using System.Runtime.CompilerServices;
11+
using System.Security.Cryptography;
12+
using System.Text;
913

10-
[assembly: InternalsVisibleTo("SubSonic.DynamicProxies")]
14+
[assembly: InternalsVisibleTo("SubSonic.DynamicProxies, PublicKey=0024000004800000940000000602000000240000525341310004000001000100290cde84efb341cb2ce91d1e881e9d927bdf6f825f1165ead25ce4881956c0f3c07d6194fb35f09c9aff40946aad571dcdc19a2a040e3a59060aca1dc0a999d081577e3fb1e325115db0794a78082e098da60ab34249388e6cad907ddae1e1b40489b815e9b3cb28e10942ffa651bbf4833611fd201afe5c05c4d27c241be2a9")]
1115

1216
namespace SubSonic.Data.DynamicProxies
1317
{
1418
public static class DynamicProxy
1519
{
1620
private readonly static Dictionary<string, DynamicProxyWrapper> DynamicProxyCache = new Dictionary<string, DynamicProxyWrapper>();
21+
private static ModuleBuilder ModuleBuilder = GetModuleBuilder();
1722

18-
private readonly static AssemblyName assemblyName = new AssemblyName("SubSonic.DynamicProxies");
19-
private readonly static AssemblyBuilder DynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
20-
private static ModuleBuilder ModuleBuilder = DynamicAssembly.DefineDynamicModule("DynamicProxiesTypeGenerator");
23+
public static Assembly DynamicAssembly => ModuleBuilder.Assembly;
2124

2225
public static TEntity CreateProxyInstanceOf<TEntity>(DbContext dbContext)
2326
{
@@ -90,16 +93,75 @@ public static DynamicProxyWrapper GetProxyWrapper<TEntity>(DbContext dbContext)
9093
return GetProxyWrapper(baseType);
9194
}
9295

96+
private static StrongNameKeyPair GetStrongNameKeyPair()
97+
{
98+
Assembly assembly = Assembly.GetExecutingAssembly();
99+
string
100+
assemblyName = assembly.GetName().Name,
101+
privateKeyFileForDynamicProxy = $"{assemblyName.Substring(0, assemblyName.IndexOf('.'))}.DynamicProxy.pfx";
102+
103+
using (var resource = assembly.GetManifestResourceStream(privateKeyFileForDynamicProxy))
104+
using (var reader = new BinaryReader(resource))
105+
{
106+
var data = new byte[resource.Length];
107+
108+
data = reader.ReadBytes(data.Length);
109+
110+
return new StrongNameKeyPair(data);
111+
}
112+
}
113+
114+
private static byte[] GetPublicKey()
115+
{
116+
Assembly assembly = Assembly.GetExecutingAssembly();
117+
string
118+
assemblyName = assembly.GetName().Name,
119+
publicKeyFileForDynamicProxy = $"{assemblyName.Substring(0, assemblyName.IndexOf('.'))}.DynamicProxy.PublicKey.pfx";
120+
121+
using (var resource = assembly.GetManifestResourceStream(publicKeyFileForDynamicProxy))
122+
using (var reader = new BinaryReader(resource))
123+
{
124+
var data = new byte[resource.Length];
125+
126+
data = reader.ReadBytes(data.Length);
127+
128+
return data;
129+
}
130+
}
131+
132+
internal static ModuleBuilder GetModuleBuilder()
133+
{
134+
if (ModuleBuilder is null)
135+
{
136+
AssemblyName assemblyName = new AssemblyName("SubSonic.DynamicProxies");
137+
138+
StrongNameKeyPair kp = GetStrongNameKeyPair();
139+
140+
assemblyName.KeyPair = kp;
141+
assemblyName.SetPublicKey(GetPublicKey());
142+
143+
144+
AssemblyBuilder DynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(
145+
assemblyName,
146+
AssemblyBuilderAccess.Run);
147+
148+
ModuleBuilder = DynamicAssembly.DefineDynamicModule("DynamicProxiesTypeGenerator");
149+
}
150+
151+
return ModuleBuilder;
152+
}
153+
93154
internal static Type BuildDerivedTypeFrom<TEntity>(DbContext dbContext)
94155
{
95156
Type baseType = typeof(TEntity);
96157

97158
DynamicProxyBuilder<TEntity> proxyBuilder = new DynamicProxyBuilder<TEntity>(ModuleBuilder.DefineType(
98-
$"{assemblyName.FullName}.{baseType.Name}",
159+
$"{DynamicAssembly.FullName}.{baseType.Name}",
99160
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout,
100161
baseType, new[] { typeof(IEntityProxy<>).MakeGenericType(baseType), typeof(IEntityProxy) }), baseType, dbContext);
101162

102163
return proxyBuilder.CreateType();
103164
}
104165
}
105166
}
167+

SubSonic/DbContext/DbContext.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
using System.Reflection;
55
using System.Runtime.CompilerServices;
66

7-
[assembly: InternalsVisibleTo("SubSonic.Extensions.Test", AllInternalsVisible = true)]
8-
[assembly: InternalsVisibleTo("SubSonic.Extensions.SqlServer", AllInternalsVisible = true)]
9-
[assembly: InternalsVisibleTo("SubSonic.Tests", AllInternalsVisible = true)]
7+
[assembly: InternalsVisibleTo(@"SubSonic.Extensions.Test, PublicKey=
8+
00240000048000009400000006020000002400005253413100040000010001007950a48c4a8b1354157c9e20fc196b4cdd111ec87039072b24322794f1c2b96d779f62a5edcf2b4e52dd487707fa34ea1fab4dad57a33f2cd89a6c70acc1b74b2f52c9bb79dd32fda12a610fd7e1facf966f6b057670ff8288a40cd7a8804be65c2049e589ca0de760e201f2d95baf4e6b6b6567a45090e65865ea47aca39ea5", AllInternalsVisible = true)]
9+
[assembly: InternalsVisibleTo(@"SubSonic.Extensions.SqlServer, PublicKey=
10+
00240000048000009400000006020000002400005253413100040000010001007950a48c4a8b1354157c9e20fc196b4cdd111ec87039072b24322794f1c2b96d779f62a5edcf2b4e52dd487707fa34ea1fab4dad57a33f2cd89a6c70acc1b74b2f52c9bb79dd32fda12a610fd7e1facf966f6b057670ff8288a40cd7a8804be65c2049e589ca0de760e201f2d95baf4e6b6b6567a45090e65865ea47aca39ea5", AllInternalsVisible = true)]
11+
[assembly: InternalsVisibleTo(@"SubSonic.Tests, PublicKey=
12+
00240000048000009400000006020000002400005253413100040000010001007950a48c4a8b1354157c9e20fc196b4cdd111ec87039072b24322794f1c2b96d779f62a5edcf2b4e52dd487707fa34ea1fab4dad57a33f2cd89a6c70acc1b74b2f52c9bb79dd32fda12a610fd7e1facf966f6b057670ff8288a40cd7a8804be65c2049e589ca0de760e201f2d95baf4e6b6b6567a45090e65865ea47aca39ea5", AllInternalsVisible = true)]
1013

1114
namespace SubSonic
1215
{
160 Bytes
Binary file not shown.

SubSonic/DynamicProxy.pfx

1.72 KB
Binary file not shown.

SubSonic/Infrastructure/Builders/DbExpressionBuilder.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ namespace SubSonic.Infrastructure
99
{
1010
using Linq;
1111

12-
internal class DbExpressionBuilder
12+
public class DbExpressionBuilder
1313
{
1414
private readonly ParameterExpression parameter;
1515
private readonly ConstantExpression root;
16-
private MethodCallExpression call;
16+
//private MethodCallExpression call;
1717
private Expression body;
1818

1919
public DbExpressionBuilder(
@@ -24,7 +24,7 @@ public DbExpressionBuilder(
2424
this.root = root ?? throw new ArgumentNullException(nameof(root));
2525
}
2626

27-
public Expression ToMethodCallExpression() => call;
27+
//public Expression ToMethodCallExpression() => call;
2828

2929
public DbExpressionBuilder BuildComparisonExpression(string property, object value, DbComparisonOperator @operator, DbGroupOperator @group)
3030
{
@@ -48,30 +48,30 @@ public DbExpressionBuilder BuildComparisonExpression(string property, object val
4848
return this;
4949
}
5050

51-
public DbExpressionBuilder ForEachProperty(string[] properties, Action<string> action)
52-
{
53-
foreach(string property in properties)
54-
{
55-
action(property);
56-
}
51+
//public DbExpressionBuilder ForEachProperty(string[] properties, Action<string> action)
52+
//{
53+
// foreach(string property in properties)
54+
// {
55+
// action(property);
56+
// }
5757

58-
return this;
59-
}
58+
// return this;
59+
//}
6060

6161

62-
public DbExpressionBuilder CallExpression<TEntity>(LambdaType @enum, params string[] properties)
63-
{
64-
Expression lambda = GetExpressionArgument<TEntity>(@enum, properties);
62+
//public DbExpressionBuilder CallExpression<TEntity>(LambdaType @enum, params string[] properties)
63+
//{
64+
// Expression lambda = GetExpressionArgument<TEntity>(@enum, properties);
6565

66-
this.call = Expression.Call(
67-
typeof(System.Linq.Queryable),
68-
@enum.ToString(),
69-
GetTypeArguments(@enum, lambda),
70-
(Expression)call ?? root,
71-
lambda);
66+
// this.call = Expression.Call(
67+
// typeof(System.Linq.Queryable),
68+
// @enum.ToString(),
69+
// GetTypeArguments(@enum, lambda),
70+
// (Expression)call ?? root,
71+
// lambda);
7272

73-
return this;
74-
}
73+
// return this;
74+
//}
7575

7676
private static Type[] GetTypeArguments(LambdaType @enum, Expression expression)
7777
{

SubSonic/Infrastructure/Logging/SubSonicLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SubSonic.Infrastructure.Logging
55
{
66
using Linq;
77

8-
class SubSonicLogger<TCategoryName>
8+
public class SubSonicLogger<TCategoryName>
99
: ISubSonicLogger<TCategoryName>
1010
{
1111
private readonly ILogger logger;

0 commit comments

Comments
 (0)