为程序集中的所有类型创建 LinFu 拦截器
我正在尝试为 DAL 程序集中的所有方法创建 LinFu 拦截器。虽然我可以做这样的事情:
[Intercepts(typeof(IFirstRepository))]
[Intercepts(typeof(ISecondaryRepository))]
[Intercepts(typeof(IIAnotherRepository))]
public class DalInterceptor : IInterceptor, IInitialize
{
...
}
这会变得非常混乱,并且每次将新存储库添加到程序集中时都需要手动更新。
有没有办法为程序集中的每种类型自动创建代理类?
更新:
我已经使用作者本人(Laureano 先生)的建议更新了我的代理构建器,所以我现在有了:
Func<IServiceRequestResult, object> createProxy = request =>
{
var proxyFactory = new ProxyFactory();
DalInterceptor dalInterceptor = new DalLiteInterceptor();
return proxyFactory.CreateProxy<object>(dalInterceptor);
};
拦截器的设置与以前一样。我现在遇到的问题是代理对象不包括原始对象的构造函数和方法(我猜测是因为我在通用创建方法中使用对象)。
我是否只是将其转换回所需的类型,或者我是否做了一些根本错误的事情?
谢谢。
I'm trying to create LinFu interceptors for all methods in my DAL assembly. While I can do something like this:
[Intercepts(typeof(IFirstRepository))]
[Intercepts(typeof(ISecondaryRepository))]
[Intercepts(typeof(IIAnotherRepository))]
public class DalInterceptor : IInterceptor, IInitialize
{
...
}
that's getting quite messy and needs manual updating every time a new repository is added to the assembly.
Is there a way to automatically create a proxy class for each type in the assembly?
UPDATE:
I've updated my proxy builder using the suggestion from the author himself (Mr Laureano) so I now have this:
Func<IServiceRequestResult, object> createProxy = request =>
{
var proxyFactory = new ProxyFactory();
DalInterceptor dalInterceptor = new DalLiteInterceptor();
return proxyFactory.CreateProxy<object>(dalInterceptor);
};
The interceptor is set up as before. The issue I'm having now is that the proxy object doesn't include the constructors and methods of the original object (I'm guessing as I'm using object in the generic create method).
Do I just cast this back to the required type or am I doing something fundamentally wrong?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您正在尝试使用 LinFu 的 IOC 容器来拦截容器实例化的各种服务。事实证明,LinFu 有一个名为 ProxyInjector 的内部类,可让您决定应拦截哪些服务以及应如何为每个服务实例创建代理。这是示例代码:
编辑:我刚刚修改了 ProxyInjector 类,使其现在是一个公共类,而不是 LinFu 中的内部类。尝试一下,让我知道是否有帮助。
It looks like you're trying to use LinFu's IOC container to intercept various services that are instantiated by the container. It turns out that LinFu has an internal class called ProxyInjector that lets you decide which services should be intercepted and how the proxy for each service instance should be created. Here's the sample code:
EDIT: I just modified the ProxyInjector class so that it is now a public class instead of an internal class in LinFu. Try it out and let me know if that helps.