为程序集中的所有类型创建 LinFu 拦截器

发布于 2024-10-15 12:05:24 字数 805 浏览 8 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海夕 2024-10-22 12:05:24

看起来您正在尝试使用 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 中的内部类。尝试一下,让我知道是否有帮助。

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文