2014-01-12 87 views
0

如何注册一个没有使用Autofac实现的界面?如何注册一个没有autofac实现的接口?

我想Autofac使用DynamicProxy生成界面你我!

builder.RegisterType(typeof(IUserDao)) 
    .AsImplementedInterfaces() 
    .EnableInterfaceInterceptors() 
    .InterceptedBy(typeof(SqlMapperInterceptor)); 


public class SqlMapperInterceptor : IInterceptor 
{ 
    public void Intercept(IInvocation invocation) 
    { 
     //todo: mapper sql file and return data 
    } 
} 

回答

0

必须有东西要拦截,否则就不能拦截。解决方案是创建一个虚拟IUserDao实现a.k.a.空对象模式。这个虚拟cwn在Autofac中被注册为实现。

+0

哈瓦任何其他方式我不t想要impl空对象类,我可以使用Castle.DynamicProxy的CreateInterfaceProxyWithoutTarget,但我不知道如何使用Autofac.Extras.DynamicProxy做到这一点。 'ProxyGenerator proxyGen = new ProxyGenerator(); var userDaoProxy = proxyGen.CreateInterfaceProxyWithoutTarget (new SqlMapperInterceptor());' – user3187103

0

目前没有机制通过Autofac提供的DynamicProxy2整合,实现这一点,但它仍然可以通过注册与Autofac直接生成的代理接口来完成:

builder.Register(c => 
{ 
    var proxyGen = new ProxyGenerator(); 
    return proxyGen.CreateInterfaceProxyWithoutTarget<IUserDao>(new SqlMapperInterceptor()); 
}) 
.As<IUserDao>(); 
相关问题