简体   繁体   English

实现拦截器模式的问题

[英]Problem implementing Interceptor pattern

I'm attempting to develop an Interceptor framework (in C#) where I can simply implement some interfaces, and through the use of some static initialization, register all my Interceptors with a common Dispatcher to be invoked at a later time. 我正在尝试开发一个Interceptor框架(在C#中),我可以简单地实现一些接口,并通过一些静态初始化,使用一个公共Dispatcher注册我的Interceptor,以便稍后调用。

The problem lies in the fact that my Interceptor implementations are never actually referenced by my application so the static constructors never get called, and as a result, the Interceptors are never registered. 问题在于我的拦截器实现从未被我的应用程序实际引用,因此静态构造函数永远不会被调用,因此拦截器永远不会被注册。

If possible, I would like to keep all references to my Interceptor libraries out of my application, as this is my way of (hopefully) enforcing loose coupling across different modules. 如果可能的话,我想保留对我的Interceptor库的所有引用都不在我的应用程序中,因为这是我(希望)在不同模块之间强制松散耦合的方式。

Hopefully this makes some sense. 希望这有一定道理。 Let me know if there's anything I can clarify... 如果有任何我可以澄清的话,请告诉我......

Does anyone have any ideas, or perhaps a better way to go about implementing my Interceptor pattern? 有没有人有任何想法,或者更好的方法来实现我的拦截器模式?

Update: I came across Spring.NET. 更新:我遇到了Spring.NET。 I've heard of it before, but never really looked into it. 我之前听说过,但从未真正研究过它。 It sounds like it has a lot of great features that would be very useful for what I'm trying to do. 听起来它有很多很棒的功能,对我正在尝试做的事情非常有用。 Does anyone have any experience with Spring.NET? 有没有人有过使用Spring.NET的经验?

TIA, TIA,
Jeremy 杰里米

Spring.NET, Unity Framework and StructureMap are probably your best bet. Spring.NET,Unity Framework和StructureMap可能是您最好的选择。 What I have done in the past is used MVC and created a custom controller factory to generate my controllers. 我过去所做的是使用MVC并创建了一个自定义控制器工厂来生成我的控制器。 However using dependency injection became difficult with frameworks like NinJect because of it's lack of an XML configuration (Ninject uses a fluent API). 然而,使用NinJect等框架使用依赖注入变得困难,因为它缺少XML配置(Ninject使用流畅的API)。 The reason it's difficult ofcourse is that you are not sure at runtime what modules you will need dependency injection for. 之所以难以接受的原因是你在运行时不确定你需要依赖注入的模块。

The three IOC containers above all have some form of XML configuration which can be modified outside of your compilation and therefore help to make your application more modular, so there would be (hopefully) no need for static initialization. 上面的三个IOC容器都有某种形式的XML配置,可以在编译之外进行修改,因此有助于使您的应用程序更加模块化,因此(希望)不需要静态初始化。 The major plus being that you don't have to worry as much about thread safety. 主要的好处是你不必担心线程安全问题。

I strongly recommend looking at some of the existing Aspect Oriented Programming (AOP) frameworks for .NET since they will likely accomplish what you're trying to achieve in a more generalized and maintainable fashion. 我强烈建议您查看一些现有的面向.NET的面向方面编程(AOP)框架,因为它们可能会以更通用和可维护的方式实现您想要实现的目标。 Most of them are open source, so where they don't meet your needs you may be able to extend and contribute to the project(s). 它们中的大多数都是开源的,因此如果它们不能满足您的需求,您可以扩展并为项目做出贡献。

More details: http://www.sharpcrafters.com/aop.net 更多细节: http//www.sharpcrafters.com/aop.net

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM