ASP.NET MVC IoC 可用性
在实际项目中,您多久将 IoC 用于控制器/DAL?
IoC 允许通过应实现的附加接口层从具体实现中抽象出应用程序。但具体实施的变化频率如何?如果实现几乎不会改变,我们真的应该做两次向接口添加方法然后实现的工作吗?我参与了大约 10 个 ASP.NET 项目,并且 DAL(类似 ORM 或非 ORM)从未被完全重写。
观看大量视频后,我清楚地了解到 IoC“很酷”,并且是一种非常好的编程方式,但它真的需要吗?
后来补充了一点: 是的,IoC 允许准备更好的测试环境,但我们也有很好的方法在没有 IoC 的情况下测试 DAL。我们将对数据库的 DAL 调用包装到未提交的事务中,而不存在导致数据不稳定的风险。
How often do you use IoC for controllers/DAL in real projects?
IoC allows to abstract application from concrete implementation with additional layer of interfaces that should be implemented. But how often concrete implementation changes? Should we really have to do job twice adding method to interface then the implementation if implementation hardly will ever be changed? I took part in about 10 asp.net projects and DAL (ORM-like and not) was never rewritten completely.
Watching lots of videos I clearly understand that IoC "is cool" and the really nice way to program, but does it really needed?
Added a bit later:
Yes, IoC allows prepare better testing environment, but we also have nice way to test DAL without IoC. We wrap DAL calls to database into uncommited transactions without risk to make data unstable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
IoC 不仅仅用于编写模块化程序;它也是一种模式。它还允许更容易的测试,通过能够交换实现与它们所代表的组件相同的接口的模拟对象。
另外,它实际上使代码更容易维护。
IoC isn't a pattern only for writing modular programs; it also allows for easier testing, by being able to swap in mock objects that implement the same interface as the components they stand in for.
Plus, it actually makes code much easier to maintain down the road.
它不是 IOC,它允许您使用附加的接口层从具体实现中抽象应用程序,这就是您应该如何设计应用程序以使其更加模块化和可重用。另一个重要的好处是,一旦您以这种方式设计了应用程序,就可以更轻松地单独测试不同的部分,而无需依赖于具体的数据库访问等。
It's not IOC that allows you to abstract application from concrete implementation with additional layer of interfaces, this is how you should design your application in order to be more modular and reusable. Another important benefit is that once you've designed your application this way it will be much easier to test the different parts in isolation without depending on concrete database access for example.
除了更改实现的能力之外,还有更多关于 IoC 的内容:
,我确信我错过了很多积极的东西。虽然我能想到的唯一“坏”的事情(以及你提到的)是接口的重复,但这对于现代 IDE 对重构的支持来说不是问题。
好吧,如果您的数据接口每天都在变化,并且您有数百个数据接口 - 您可能希望避免 IoC。
但是,您是否仅仅因为遵循良好的设计实践更困难而回避它们?您是否复制并粘贴代码而不是提取方法/类,只是因为这样做需要更多时间和更多代码?您是否将业务逻辑放在视图中只是因为创建视图模型并将其与域模型同步更困难?如果是,那么你可以避免 IoC,没问题。
There's much more about IoC except ability to change implementation:
And so on, I'm sure I missed a lot of positive stuff. While the only "bad" thing that I can think about (and that you mentioned) is duplication of interface, which is non-issue with modern IDEs support for refactoring.
Well, if your data interfaces change every day, and you have hundreds of them - you may want to avoid IoC.
But, do you avoid good design practices just because it's harder to follow them? Do you copy and paste code instead of extracting a method/class, just because it takes more time and more code to do so? Do you place business logic in views just because it's harder to create view models and sync them with domain models? If yes, then you can avoid IoC, no problem.
您认为使用 IOC 比不使用它需要更多代码。我不同意。
以下是我使用 LinqToSql 的项目之一的完整 DAL IOC 配置。 ContextProvider 类只是一个线程安全的 LinqToSql 上下文工厂。
以下是我的一个使用 NHibernate 和存储库模式的项目的完整 DAL 配置:
以下是我在 BLL 中使用 DAL 的方式(使用依赖注入):
You're arguing that using IOC takes MORE code than not using it. I disagree.
Here is the entire DAL IOC configuration for one of my projects using LinqToSql. The ContextProvider class is simply a thread safe LinqToSql context factory.
Here is the entire DAL configuration for one of my projects using NHibernate and the repository pattern:
Here is how I consume the DAL in my BLL (w/ dependency injection):