简体   繁体   English

温莎城堡有什么缺点吗?

[英]Castle Windsor are there any downsides?

I have been looking into the Castle project and specifically Windsor. 我一直在研究Castle项目,尤其是Windsor。 I have been so impressed with what is possible with this technology and the benefits of having a such a loosely coupled system are definitely apparent. 这项技术可能给我留下深刻的印象,拥有这样一个松散耦合的系统的好处是显而易见的。 The only thing I am unsure of is if using this method has any downsides, specifically in asp.net? 我唯一不确定的是使用此方法是否有任何缺点,特别是在asp.net中? for example performance hits, etc. 例如效果命中率等

I am trying to make the benefits of this approach visible to my fellow developers here and am being hit with the following comebacks: 我试图在这里让我的开发人员看到这种方法的好处,并受到以下卷土重来的打击:

  1. That is using reflection and each time that an object is called from the container, reflection must used so performance will be terrible. 那就是使用反射,每次从容器中调用一个对象时,都必须使用反射,这样性能会很糟糕。 (Is this the case? does it use reflection on every call?) (是这种情况吗?是否在每个呼叫中​​都使用反射?)

  2. If I am relying on Interfaces; 如果我依赖接口; how do I deal with objects that have extra methods and properties which have been tacked onto the class? 如何处理具有附加到类上的额外方法和属性的对象? (through inheritance) (通过继承)

To answer your questions: 要回答您的问题:

  1. That is using reflection and each time that an object is called from the container, reflection must used so performance will be terrible. 那就是使用反射,每次从容器中调用一个对象时,都必须使用反射,这样性能会很糟糕。 (Is this the case? does it use reflection on every call?) (是这种情况吗?是否在每个呼叫中​​都使用反射?)
  • No, it does not. 不,不是的。 Most of the time it uses little reflection when you register the component. 在大多数情况下,注册组件时它很少反射。 It may also use reflection while generating proxy type, first time you request a component from the container. 第一次从容器中请求组件时,它在生成代理类型时也可能会使用反射。
  1. If I am relying on Interfaces; 如果我依赖接口; how do I deal with objects that have extra methods and properties which have been tacked onto the class? 如何处理具有附加到类上的额外方法和属性的对象? (through inheritance) (通过继承)
  • It's all a matter of design. 这都是设计问题。 You don't want to have each and every object created by the container. 您不想让容器创建每个对象。 You use it primarily for service dependencies. 您主要将其用于服务依赖性。 In this case, you don't care about what type is actually hiding behind the interface (that's the whole point of it, isn't it?). 在这种情况下,您不必担心接口后面实际上隐藏了什么类型(这就是问题的全部,不是吗?)。

You also can have class components, but they have limitations, and you must be aware of those (for example you can't intercept calls to non-virtual methods). 您也可以具有类组件,但是它们具有局限性,并且您必须意识到这些局限性(例如,您不能拦截对非虚拟方法的调用)。 I have found Windsor to be the most mature, and best suited to my style of development container of all. 我发现温莎最成熟,最适合我所有的开发容器风格。

Other than that, Performance, I haven't heard of a project that had to discard dependency container because of unacceptable performance. 除此之外,Performance,我还没有听说过由于性能不可接受而不得不放弃依赖容器的项目。 Windsor is really smart about it, and it caches the results of lengthy operations so that you don't have to pay the price twice. 温莎真的很聪明,它缓存了冗长的操作结果,因此您不必付出两次代价。 You may find charts on the Internet, comparing speed of many IoC containers. 您可以在Internet上找到图表,比较许多IoC容器的速度。 Two things to note about those: All containers are really fast. 需要注意的两件事:所有容器都非常快。 Don't think that the fact that other containers are faster on these charts than Windsor, means that they are better. 不要以为其他容器在这些图表上比Windsor快,这意味着它们更好。 Windsor does a lot of stuff for you that other containers don't. 温莎为您做了很多其他容器无法做到的事情。

I've used IoC containers (Spring.NET and StructureMap) in several production apps under high load (not Facebook/MySpace high, but enough to stress out a few servers). 我已经在高负载下使用了IoC容器(Spring.NET和StructureMap),这些应用程序在高负载下(不是Facebook / MySpace高负载,但是足以减轻一些服务器的负担)。

In my experience, even before I started using IoC, the biggest perf concern is the database and the interaction with the database -- optimizing queries, indexes, using 2nd-level caching, etc. 以我的经验,甚至在我开始使用IoC之前,最大的性能问题就是数据库以及与数据库的交互-优化查询,索引,使用第二级缓存等。

If you have a database involved with your app, then whatever performance hit Windsor or any other container may cause will be so infintessimally small compared to a DB round trip. 如果您的应用程序涉及数据库,那么与数据库往返相比,温莎或任何其他容器可能遭受的性能下降都将非常小。

This is like people who compare the performance hit of the new() operator vs. Activator.CreateInstance() at 1-10ms when a single DB roundtrip is usually an order of magnitude more expensive. 这就像人们在1-10毫秒之间比较new()运算符与Activator.CreateInstance()的性能影响时,单个DB往返通常要贵一个数量级。

I would advise you not to worry about the small stuff and concentrate on the big stuff. 我建议您不要担心小东西,而应该专注于大东西。

Also, I'd like to advise you to look at StructureMap, as I believe it has a lot more functionality than Windsor and doesn't have a lot of Windsor's shortcomings (ie holding on to references and requiring you to release them, etc). 另外,我还建议您查看StructureMap,因为我认为它比Windsor具有更多的功能,并且没有很多Windsor的缺点(例如,保留引用并要求您释放它们,等等) 。

One problem with Castle Windsor I came across is that it is not able to run in Medium Trust (without recompilation which I was not able to do). 我遇到的Castle Windsor的一个问题是,它无法在Medium Trust中运行(没有重新编译,而我不能这样做)。 So I needed to switch from Windsor to Unity. 所以我需要从Windsor切换到Unity。

According to DI/IoC performance - I believe that the performance hit is not large especially when you keep in mind the power it has. 根据DI / IoC的性能-我认为对性能的影响并不大,尤其是当您牢记它具有的功能时。

BTW: If you are beginning with DI/IoC, you should read this MSDN article . 顺便说一句:如果您开始使用DI / IoC,则应阅读此MSDN文章

Significant start-up costs, almost no performance hit during operation (no reflection for calls, of course - everything's compiled during instantiation). 巨大的启动成本,在运行过程中几乎没有性能下降(当然,没有反射到调用-实例化过程中所有内容都已编译)。 Windsor is a bit on a heavy side, but with proper lifetime management shouldn't cause any problems. 温莎的立场有点沉重,但如果进行适当的生命周期管理,就不会造成任何问题。

A more important downside is that integration problems are not caught during build, and some not even at launch. 一个更重要的缺点是在构建过程中不会发现集成问题,甚至在发布时也不会发现。 Without carefull tracking of all modules' versions and continuous testing of whole system it's easy to get in trouble. 如果不仔细跟踪所有模块的版本以及对整个系统进行连续测试,就很容易遇到麻烦。 Reflection helps here, so Windsor is better in that aspect than many other DI frameworks. 反射在这里有帮助,因此温莎在这方面比许多其他DI框架要好。

The one exception where performance of an IoC container is unacceptable in my experience is when trying to integrate/wrap the IoC container as an IServiceProvider for use in the ComponentModel - a once common example of this was when creating your own hosted winforms designer (normally to build some kind of custom designer, ie workflow/flowchart etc.) 根据我的经验,IoC容器的性能无法接受的一个例外是,当尝试将IoC容器作为IServiceProvider集成/包装以便在ComponentModel中使用时-一个常见的例子是在创建自己的托管Winforms设计器时(通常是建立某种自定义设计器,例如工作流/流程图等。

Due to the way a number of winforms components behave, especially at design time, the cost of resolving a component will physically cause the mouse to "stutter" when dragging because the framework can make upwards of 30,000 service resolution calls a second - this is more a reflection on poor coding practices in the components themselves though I think, or at least due to assumptions being made about the service providers implementation being fast/simple. 由于许多winforms组件的行为方式,尤其是在设计时,解决该组件的成本实际上将导致鼠标在拖动时“结结巴巴”,因为该框架可以每秒调用30,000以上的服务分辨率-这更多尽管我认为,或者至少由于对服务提供者实现的快速/简单的假设,这反映了组件本身的不良编码实践。

In practice I've never found component resolution times to be a problem in even heavily loaded commercial applications. 在实践中,我什至没有发现即使在重载的商业应用中,组件的解析时间也不成问题。

  1. That is using reflection and each time that an object is called from the container, reflection must used so performance will be terrible. 那就是使用反射,每次从容器中调用一个对象时,都必须使用反射,这样性能会很糟糕。 (Is this the case? does it use reflection on every call?) (是这种情况吗?是否在每个呼叫中​​都使用反射?)

As far as know all the good containers (Castle Windsor included here) do use reflection to create new instances. 据了解,所有好的容器(此处包括城堡温莎)都使用反射来创建新实例。 However this is improve performance. 但是,这可以提高性能。 This is to compare with Activator.CreateInstance which is much slower. 这是与Activator.CreateInstance相比,后者要慢得多。 Some other Containers are quite fast though and can compete with the new operator. 其他一些容器虽然速度很快,但可以与新运营商竞争。 See comparison bench mark here . 在这里查看比较基准。 Castle Windsor is not from the high performant ones however the speed is not really important. 温莎城堡不是来自高绩效的城堡,但是速度并不是很重要。 When it comes to IoC use in application. 在应用程序中使用IoC时。 90% of your classes should be instated as singletones and this means work at the startup of your application only. 90%的类应恢复为单调,这意味着只能在应用程序启动时工作。

  1. If I am relying on Interfaces; 如果我依赖接口; how do I deal with objects that have extra methods and properties which have been tacked onto the class? 如何处理具有附加到类上的额外方法和属性的对象? (through inheritance) (通过继承)

Try this tutorial and this tutorials. 尝试本教程教程。 It will reveal answer to your question. 它将显示您问题的答案。 If you want to avoid problems of design and easy to maintain software I would highly recommend going for SOLID practices 如果您想避免设计问题和易于维护的软件,我强烈建议您采用SOLID实践

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

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