C# 和 Visual Studio 2005 中的程序集之间的循环引用(再次...)

发布于 2024-07-21 22:27:04 字数 412 浏览 3 评论 0原文

请首先阅读以下线程:

程序集之间的循环引用C# 和 Visual Studio 2005

实现接口解决了我的问题,但没有实现我的目标。

我的目标是仅使用 UI 层/组件中的 BO 层/组件。 这样我就可以保持清晰的层与层参考。

因为我不想在 UI 层/程序集中添加 BO 层/程序集和 ORMapper 层/程序集的引用。

我只想在 UI 层/程序集中使用 BO 层/程序集。

同时有人建议我,这只能通过反射来实现,而不是 DI。 真的吗?

Please first read the following thread:

Circular reference between Assemblies in C# and Visual Studio 2005

Implementing interfaces is solving my problem but not fulfilling my target.

My target is to work only with BO-layer/assembly from the UI layer/Assembly. So that I can maintain a clean layer-to-layer reference.

Coz I don't want a reference to be added both for BO-layer/assembly and ORMapper-layer/assembly in the UI-layer/assembly.

I only want to work with BO-layer/assembly from within UI-layer/assembly.

Meanwhile someone suggested me that, it can only be possible with using Reflection, not DI. Is that true?

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

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

发布评论

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

评论(1

尐偏执 2024-07-28 22:27:05

如果您确实坚持将 ORM 层隐藏在 BO 层后面,那么依赖注入仍然可以帮助您。 请注意,您将无法摆脱从 UI 线程引用 DI 库的情况,因为那时您的业务对象将来自那里。

方法:

  • 创建一个接口,其中包含加载数据以填充 User 对象的方法
  • 在您的 ORM 项目中实现此接口
  • 在启动时,使用某种 DI 容器针对该接口注册 ORM 实现(阅读您使用的任何 DI 库的详细信息,例如:www.ninject.org 以获得轻量级的内容)
  • User 对象提供一个构造函数,该构造函数采用人口接口的实例来加载数据 然后

当您需要一个 User 对象,您要求 DI 库创建它,DI 库将构造 User 并为其提供对 ORM 实现的引用。 您还必须将一些属性注入到 DI 调用中,以便“用户名”和“密码”能够用值完全填充它。

请注意,这并不是 DI 的发明目的……应用程序代码实际上应该直接处理 ORM 来创建/读取/更新/删除业务对象。

If you really insist on hiding the ORM layer behind your BO layer, then Dependency Injection should still be able to help you out. Note that you will not be able to get away from referencing the DI library from your UI thread though, because that'll be where your business objects will come from then.

Approach:

  • Create an interface that contains methods to load the data to populate User objects
  • Implement this interface in your ORM project
  • At start-up, register the ORM implementation against the interface using a DI container of some sort (read up the details on whatever DI library you use, for example: www.ninject.org for something lightweight)
  • Give the User object a constructor that takes an instance of the population interface to load data from

Then when you need a User object, you ask the DI library to create it, and the DI library will construct the User and give it a reference to the ORM implementation. You'll have to inject some properties into the DI call as well for the 'username' and 'password' to be able to fully populate it with values.

Note that this is not exactly what DI was invented for... application code is really supposed to deal with ORM directly to create/read/update/delete business objects.

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