现在,我正在装饰我的 StructureMap4 映射类型,比如,在StructureMap4注册表中用一个带有tryCatchInterceptor的城堡生成的代理来装饰IFormsAuthenticationProvider。例如:
public class AuthenticationRegistry : Registry
{
public AuthenticationRegistry()
{
var proxyGenerator = new ProxyGenerator();
var tryCatchInterceptor = new TryCatchInterceptor();
For<IFormsAuthenticationProvider>().Use<FormsAuthenticationProvider>()
.DecorateWith(x => proxyGenerator.CreateInterfaceProxyWithTarget<IFormsAuthenticationProvider>(x, tryCatchInterceptor));
}
}
public class TryCatchInterceptor : IInterceptor
{..}但正如您所看到的,我必须在装饰方法中指定类型。因此,必须为所有IType->Type定义类似的装饰器,此时代码将变得重复。
问:是否有办法在一个共同的地方,对所有类型,不重复?
发布于 2016-09-19 12:51:36
经过大量的研发之后,我不认为在structuremap4.0版本中存在这样的机制。
然而,我想出了一个我自己的动态解决方案。
创建类模板并动态创建类。编译并运行代码后将类加载到内存中。
classTemplate.txt
using Castle.DynamicProxy;
using StructureMap;
using System.Web;
using Company1.WebApplication.App1.Meta;
using Company1.WebApplication.App1.Meta.Interceptors;
namespace Company1.WebApplication.App1
{
public class DynamicUtils
{
private static StructureMapDependencyResolver _structureMapResolver { get; set; }
private static ProxyGenerator _ProxyGenerator = new ProxyGenerator();
public static void ConfigureCastleInterceptor(Container container)
{
container.Configure(x =>
{
##INTERFACE##
});
}
}
}在我的Global.asax中,编写了加载它的代码。
private static void ConfigureCastleInterceptor(Container container)
{
string classBody = File.ReadAllText(HttpRuntime.AppDomainAppPath + "/RuntimeClasses/RegisterInterceptors.txt");
var classBuilder = new StringBuilder();
string interfaceTemplate = "x.For<##INTERFACE##>()
.DecorateAllWith(y => _ProxyGenerator
.CreateInterfaceProxyWithTarget<##INTERFACE##>(y, new TryCatchLoggingInterceptor())); \n";
foreach (var instance in container.Model.AllInstances)
{
if (instance.PluginType.FullName.Contains("Company1.WebApplication"))
classBuilder.Append(interfaceTemplate.Replace("##INTERFACE##", instance.PluginType.FullName));
}
classBody = classBody.Replace("##INTERFACE##", classBuilder.ToString());
var csharp = new CSharpCodeProvider();
var compiler = new CompilerParameters();
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
compiler.ReferencedAssemblies.Add(asm.Location);
}
compiler.GenerateInMemory = true;
compiler.GenerateExecutable = false;
CompilerResults results = csharp.CompileAssemblyFromSource(compiler, classBody);
if (!results.Errors.HasErrors)
{
Assembly assembly = results.CompiledAssembly;
Type program = assembly.GetType("Company1.WebApplication.App1.DynamicUtils");
MethodInfo configureCastleInterceptor = program.GetMethod("ConfigureCastleInterceptor");
configureCastleInterceptor.Invoke(null, new Object[] { container });
}
else
{
throw new Exception(results.Errors.ToString());
}
}https://stackoverflow.com/questions/39532208
复制相似问题