我正在尝试使用结构映射进行一些基于属性的拦截,但我正在努力锁定最后的松散结束。使用结构映射执行拦截
我有一个自定义注册表,它扫描我的程序集,并在此注册表中定义了以下ITypeInterceptor,其目的是匹配使用给定属性修饰的类型,然后在匹配的情况下应用拦截器。该类被定义为这样的:
public class AttributeMatchTypeInterceptor<TAttribute, TInterceptor>
: TypeInterceptor
where TAttribute : Attribute
where TInterceptor : IInterceptor
{
private readonly ProxyGenerator m_proxyGeneration = new ProxyGenerator();
public object Process(object target, IContext context)
{
return m_proxyGeneration.CreateInterfaceProxyWithTarget(target, ObjectFactory.GetInstance<TInterceptor>());
}
public bool MatchesType(Type type)
{
return type.GetCustomAttributes(typeof (TAttribute), true).Length > 0;
}
}
//Usage
[Transactional]
public class OrderProcessor : IOrderProcessor{
}
...
public class MyRegistry : Registry{
public MyRegistry()
{
RegisterInterceptor(
new AttributeMatchTypeInterceptor<TransactionalAttribute, TransactionInterceptor>());
...
}
}
我使用DynamicProxy从Castle.Core创建拦截器,但我的问题是,对象从CreateInterfaceProxyWithTarget(...)退还调用不实现触发在结构图中创建目标实例的接口(即上例中的IOrderProcessor)。我希望IContext参数能够揭示这个接口,但我似乎只能看到具体类型(例如上例中的OrderProcessor)。
我在寻找关于如何让这个场景工作的指导,要么通过调用ProxyGenerator来返回一个实现所有接口作为目标实例的实例,通过从结构映射或通过其他机制获取请求的接口。