我正在尝试为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);
};拦截器的设置与以前一样。我现在遇到的问题是,代理对象不包括原始对象的构造函数和方法(我猜是因为我在通用的create方法中使用object )。
我只是将它转换回必需的类型,还是我做了一些根本错误的事情?
谢谢。
发布于 2011-02-03 13:26:44
看起来您正在尝试使用LinFu的IOC容器来拦截由该容器实例化的各种服务。事实证明,LinFu有一个名为ProxyInjector的内部类,它允许您决定应该拦截哪些服务以及如何为每个服务实例创建代理。以下是示例代码:
Func<IServiceRequestResult, bool> shouldInterceptServiceInstance = request=>request.ServiceType.Name.EndsWith("Repository");
Func<IServiceRequestResult, object> createProxy = request =>
{
// TODO: create your proxy instance here
return yourProxy;
};
// Create the injector and attach it to the container so that you can selectively
// decide which instances should be proxied
var container = new ServiceContainer();
var injector = new ProxyInjector(shouldInterceptServiceInstance, createProxy);
container.PostProcessors.Add(injector);
// ...Do something with the container here编辑:我刚刚修改了ProxyInjector类,以便它现在是公共类,而不是LinFu中的内部类。试一试,如果有帮助,请告诉我。
https://stackoverflow.com/questions/4876839
复制相似问题