Skip to content

Commit

Permalink
Inject options into proxy generators via ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jul 2, 2020
1 parent fd80bc5 commit b6ebeda
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 53 deletions.
16 changes: 8 additions & 8 deletions src/Castle.Core/DynamicProxy/DefaultProxyBuilder.cs
Expand Up @@ -65,8 +65,8 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT
AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
AssertValidMixins(options, nameof(options));

var generator = new ClassProxyGenerator(scope, classToProxy) { Logger = logger };
return generator.GenerateCode(additionalInterfacesToProxy, options);
var generator = new ClassProxyGenerator(scope, classToProxy, options) { Logger = logger };
return generator.GenerateCode(additionalInterfacesToProxy);
}

public Type CreateClassProxyTypeWithTarget(Type classToProxy, Type[] additionalInterfacesToProxy,
Expand All @@ -89,8 +89,8 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT
AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
AssertValidMixins(options, nameof(options));

var generator = new InterfaceProxyWithTargetGenerator(scope, interfaceToProxy) { Logger = logger };
return generator.GenerateCode(targetType, additionalInterfacesToProxy, options);
var generator = new InterfaceProxyWithTargetGenerator(scope, interfaceToProxy, options) { Logger = logger };
return generator.GenerateCode(targetType, additionalInterfacesToProxy);
}

public Type CreateInterfaceProxyTypeWithTargetInterface(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
Expand All @@ -100,8 +100,8 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT
AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
AssertValidMixins(options, nameof(options));

var generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, interfaceToProxy) { Logger = logger };
return generator.GenerateCode(interfaceToProxy, additionalInterfacesToProxy, options);
var generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, interfaceToProxy, options) { Logger = logger };
return generator.GenerateCode(interfaceToProxy, additionalInterfacesToProxy);
}

public Type CreateInterfaceProxyTypeWithoutTarget(Type interfaceToProxy, Type[] additionalInterfacesToProxy,
Expand All @@ -111,8 +111,8 @@ public Type CreateClassProxyType(Type classToProxy, Type[] additionalInterfacesT
AssertValidTypes(additionalInterfacesToProxy, nameof(additionalInterfacesToProxy));
AssertValidMixins(options, nameof(options));

var generator = new InterfaceProxyWithoutTargetGenerator(scope, interfaceToProxy) { Logger = logger };
return generator.GenerateCode(typeof(object), additionalInterfacesToProxy, options);
var generator = new InterfaceProxyWithoutTargetGenerator(scope, interfaceToProxy, options) { Logger = logger };
return generator.GenerateCode(typeof(object), additionalInterfacesToProxy);
}

private void AssertValidMixins(ProxyGenerationOptions options, string paramName)
Expand Down
21 changes: 4 additions & 17 deletions src/Castle.Core/DynamicProxy/Generators/BaseProxyGenerator.cs
Expand Up @@ -42,10 +42,12 @@ internal abstract class BaseProxyGenerator
private ILogger logger = NullLogger.Instance;
private ProxyGenerationOptions proxyGenerationOptions;

protected BaseProxyGenerator(ModuleScope scope, Type targetType)
protected BaseProxyGenerator(ModuleScope scope, Type targetType, ProxyGenerationOptions proxyGenerationOptions)
{
this.scope = scope;
this.targetType = targetType;
this.proxyGenerationOptions = proxyGenerationOptions;
this.proxyGenerationOptions.Initialize();
}

public ILogger Logger
Expand All @@ -56,22 +58,7 @@ public ILogger Logger

protected ProxyGenerationOptions ProxyGenerationOptions
{
get
{
if (proxyGenerationOptions == null)
{
throw new InvalidOperationException("ProxyGenerationOptions must be set before being retrieved.");
}
return proxyGenerationOptions;
}
set
{
if (proxyGenerationOptions != null)
{
throw new InvalidOperationException("ProxyGenerationOptions can only be set once.");
}
proxyGenerationOptions = value;
}
get { return proxyGenerationOptions; }
}

protected ModuleScope Scope
Expand Down
11 changes: 4 additions & 7 deletions src/Castle.Core/DynamicProxy/Generators/ClassProxyGenerator.cs
Expand Up @@ -27,21 +27,18 @@ namespace Castle.DynamicProxy.Generators

internal class ClassProxyGenerator : BaseProxyGenerator
{
public ClassProxyGenerator(ModuleScope scope, Type targetType) : base(scope, targetType)
public ClassProxyGenerator(ModuleScope scope, Type targetType, ProxyGenerationOptions options)
: base(scope, targetType, options)
{
CheckNotGenericTypeDefinition(targetType, "targetType");
EnsureDoesNotImplementIProxyTargetAccessor(targetType, "targetType");
}

public Type GenerateCode(Type[] interfaces, ProxyGenerationOptions options)
public Type GenerateCode(Type[] interfaces)
{
// make sure ProxyGenerationOptions is initialized
options.Initialize();

interfaces = TypeUtil.GetAllInterfaces(interfaces);
CheckNotGenericTypeDefinitions(interfaces, "interfaces");
ProxyGenerationOptions = options;
var cacheKey = new CacheKey(targetType, interfaces, options);
var cacheKey = new CacheKey(targetType, interfaces, ProxyGenerationOptions);
return ObtainProxyType(cacheKey, (n, s) => GenerateType(n, interfaces, s));
}

Expand Down
Expand Up @@ -34,14 +34,12 @@ internal class ClassProxyWithTargetGenerator : BaseProxyGenerator

public ClassProxyWithTargetGenerator(ModuleScope scope, Type classToProxy, Type[] additionalInterfacesToProxy,
ProxyGenerationOptions options)
: base(scope, classToProxy)
: base(scope, classToProxy, options)
{
CheckNotGenericTypeDefinition(targetType, "targetType");
EnsureDoesNotImplementIProxyTargetAccessor(targetType, "targetType");
CheckNotGenericTypeDefinitions(additionalInterfacesToProxy, "additionalInterfacesToProxy");

options.Initialize();
ProxyGenerationOptions = options;
this.additionalInterfacesToProxy = TypeUtil.GetAllInterfaces(additionalInterfacesToProxy);
}

Expand Down
Expand Up @@ -32,8 +32,8 @@ internal class InterfaceProxyWithTargetGenerator : BaseProxyGenerator
{
protected FieldReference targetField;

public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type @interface)
: base(scope, @interface)
public InterfaceProxyWithTargetGenerator(ModuleScope scope, Type @interface, ProxyGenerationOptions options)
: base(scope, @interface, options)
{
CheckNotGenericTypeDefinition(@interface, "@interface");
}
Expand All @@ -48,18 +48,14 @@ protected virtual string GeneratorType
get { return ProxyTypeConstants.InterfaceWithTarget; }
}

public Type GenerateCode(Type proxyTargetType, Type[] interfaces, ProxyGenerationOptions options)
public Type GenerateCode(Type proxyTargetType, Type[] interfaces)
{
// make sure ProxyGenerationOptions is initialized
options.Initialize();

CheckNotGenericTypeDefinition(proxyTargetType, "proxyTargetType");
CheckNotGenericTypeDefinitions(interfaces, "interfaces");
EnsureValidBaseType(options.BaseTypeForInterfaceProxy);
ProxyGenerationOptions = options;
EnsureValidBaseType(ProxyGenerationOptions.BaseTypeForInterfaceProxy);

interfaces = TypeUtil.GetAllInterfaces(interfaces);
var cacheKey = new CacheKey(proxyTargetType, targetType, interfaces, options);
var cacheKey = new CacheKey(proxyTargetType, targetType, interfaces, ProxyGenerationOptions);

return ObtainProxyType(cacheKey, (n, s) => GenerateType(n, proxyTargetType, interfaces, s));
}
Expand Down
Expand Up @@ -26,8 +26,8 @@ namespace Castle.DynamicProxy.Generators

internal class InterfaceProxyWithTargetInterfaceGenerator : InterfaceProxyWithTargetGenerator
{
public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, Type @interface)
: base(scope, @interface)
public InterfaceProxyWithTargetInterfaceGenerator(ModuleScope scope, Type @interface, ProxyGenerationOptions options)
: base(scope, @interface, options)
{
}

Expand Down
Expand Up @@ -25,7 +25,8 @@ namespace Castle.DynamicProxy.Generators

internal class InterfaceProxyWithoutTargetGenerator : InterfaceProxyWithTargetGenerator
{
public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type @interface) : base(scope, @interface)
public InterfaceProxyWithoutTargetGenerator(ModuleScope scope, Type @interface, ProxyGenerationOptions options)
: base(scope, @interface, options)
{
}

Expand Down
Expand Up @@ -144,15 +144,15 @@ public object RecreateInterfaceProxy(string generatorType)
InterfaceProxyWithTargetGenerator generator;
if (generatorType == ProxyTypeConstants.InterfaceWithTarget)
{
generator = new InterfaceProxyWithTargetGenerator(scope, @interface);
generator = new InterfaceProxyWithTargetGenerator(scope, @interface, proxyGenerationOptions);
}
else if (generatorType == ProxyTypeConstants.InterfaceWithoutTarget)
{
generator = new InterfaceProxyWithoutTargetGenerator(scope, @interface);
generator = new InterfaceProxyWithoutTargetGenerator(scope, @interface, proxyGenerationOptions);
}
else if (generatorType == ProxyTypeConstants.InterfaceWithTargetInterface)
{
generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, @interface);
generator = new InterfaceProxyWithTargetInterfaceGenerator(scope, @interface, proxyGenerationOptions);
}
else
{
Expand All @@ -162,14 +162,14 @@ public object RecreateInterfaceProxy(string generatorType)
generatorType));
}

var proxyType = generator.GenerateCode(targetType, interfaces, proxyGenerationOptions);
var proxyType = generator.GenerateCode(targetType, interfaces);
return FormatterServices.GetSafeUninitializedObject(proxyType);
}

public object RecreateClassProxy()
{
var generator = new ClassProxyGenerator(scope, baseType);
var proxyType = generator.GenerateCode(interfaces, proxyGenerationOptions);
var generator = new ClassProxyGenerator(scope, baseType, proxyGenerationOptions);
var proxyType = generator.GenerateCode(interfaces);
return InstantiateClassProxy(proxyType);
}

Expand Down

0 comments on commit b6ebeda

Please sign in to comment.