IoC 和依赖注入的实用性

发布于 2024-08-24 12:46:56 字数 333 浏览 6 评论 0原文

某些情况中,单元测试不不为该项目工作。

我正在研究控制反转和依赖注入实用程序,我想知道是否有充分的理由使用它而不是使单元测试更容易。

--update

好吧,让我们分析一下所提到的优点之一:更少的耦合。 您从子类型中取出耦合,并将耦合添加到需要创建要注入的对象的处理程序类型。

如果没有单元测试,这种耦合转移(而不是消除耦合)有什么好处。

There are some cases in which unit test don't work for the project.

I'm studing the Inversion of Control and Dependency Injection utilities and I would like to know if there are good reasons for use it than for make unit tests easier.

--update

Ok, let's analysis 1 of the cited advanteges: less coupling.
You take out the coupling from the child type, and you add coupling to the handler type who need to create the objects to inject.

Without unit testing, what's the advantage in this coupling transfer (not coupling eliminate).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

苏别ゝ 2024-08-31 12:46:56

IOC/DI 为您的应用程序带来了一些非常重要的功能

  • 可插拔性:使用 DI,您可以将依赖项注入到代码中,而无需明确了解功能实际如何工作。
    例如:您的类可能会注入 ILog 接口,以便它可以写入日志。由于该类使用 ILog 接口,因此可以实现 FileLog、MemoryLog 或 DatabaseLog 等。将其注入到您的班级中。只要实现 ILog 接口,任何这些实现​​都可以正常工作
  • 可测试性:通过类中的 DI,您可以注入模拟对象来测试类的行为,而无需实际需要具体实现。
    例如:考虑一个需要存储库来执行数据操作的控制器类。在这种情况下,存储库可以是控制器的 DI。如果您需要在 Controller 类上编写测试,您可以传递存储库的 DI 模拟版本,而无需使用实际的存储库类
  • 可配置性:一些常见的 DI 框架,如 Castle Windor 、Unity、Spring 等,允许对创建的对象进行 DI 和生命周期管理。这是一个非常强大的功能&允许您管理依赖关系和通过配置它们的生命周期。例如,考虑您的应用程序需要 ICache 依赖项。通过终身配置和对象管理,您将能够将缓存配置为每个应用程序、每个会话或每个请求等,而无需在代码中显式地烘焙实现。

华泰

IOC/DI bring some very important features to your application

  • Plugability: with DI you can inject dependency into the code without explicitly knowing how the functionality is actually working.
    For example: your class might get a ILog interface injected so that it can write logs. Since the class works with the ILog interface, it would be possible to implement a FileLog, MemoryLog or a DatabaseLog & inject this into your class. Any of these implementation will work fine as long as they implement the ILog interface
  • Testability: With DI in your class, you can inject mock objects to test the behaviour of your class without actually needing the concrete implementation.
    For example: consider a Controller class which needs a Repository to perform data operations. In this case, the repository can be DI for the controller. If you need to write tests on the Controller class, you can pass a DI'd mock version of the repository without having to work with the actual repository class
  • Configurability: Some of the common DI frameworks like Castle Windor, Unity, Spring etc., allow doing DI along with lifetime management of the object created. This is a very powerful feature & allow you to manage the dependencies & their lifetime via configuration. For example consider your application needs a ICache dependency. Via the configuration for lifetime & object management, you will be able to configure the cache to be a Per-application or per-session or per-request etc. without having to explicitly bake the implementation in your code.

HTH

过去的过去 2024-08-31 12:46:56

IoC reduces coupling, which has a correlation with defect rates in some studies. (If that really long link doesn't work, it's to Software Engineering Quality Practices by Ronald Kirk Kandt.)

恋你朝朝暮暮 2024-08-31 12:46:56

当然,这里有几个原因:

  1. 动态生成用于远程处理和事务的代理
  2. 面向方面的编程
  3. 使用接口分层和实现分离

足够了吗?

Sure, here are a few reasons:

  1. Dynamic generation of proxies for remoting and transactons
  2. Aspect oriented programming
  3. Layering using interfaces and separation of implementation

Enough?

剩一世无双 2024-08-31 12:46:56

来自 IoC wikipedia 文章

  • 某个任务的执行与实现是解耦的。
  • 每个系统都可以专注于其设计目的。
  • 每个系统都不会对其他系统做什么或应该做什么做出假设。
  • 更换系统不会对其他系统产生副作用。

虽然我认为上面的功能列表有点模糊,但即使没有测试,您也会看到上面的大部分好处。

如果我必须简而言之,我会说 IoC 显着改善了关注点分离,这是软件开发中的一个有价值的目标。

From the IoC wikipedia article:

  • There is a decoupling of the execution of a certain task from implementation.
  • Every system can focus on what it is designed for.
  • Every system does not make assumptions about what other systems do or should do.
  • Replacing systems will have no side effect on other systems.

While I would call the above feature list a bit vague, you see most of the above benefits, even without testing.

If I had to say it in a nutshell, I would say that IoC significantly improves separation of concerns which is a valuable goal in software development.

同展鸳鸯锦 2024-08-31 12:46:56

是的,依赖注入可以帮助您使您的类更加集中、更清晰*并且更容易更改,因为它可以更轻松地遵守 单一责任原则

它还使您可以更轻松地相互独立地改变应用程序的各个部分。

特别是当您使用构造函数注入时,可以更轻松地了解代码需要执行的操作它的工作。如果 WeatherUpdater 类在其构造函数中需要一个 IWeatherRepository ,那么没有人会对它使用数据库感到惊讶。

* 再次强调,仅限构造函数注入。

Yes, dependency injection helps you make your classes more focused, clearer*, and easier to change, because it makes it easier to adhere to the single-responsibility principle.

It also makes it easier to vary parts of your application independently of one another.

When you use constructor injection in particular, it's easier to tell what your code needs to do its job. If the WeatherUpdater class requires an IWeatherRepository in its constructor no one is surprised that it uses a database.

* Again, constructor injection only.

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