MODEL 和 BLL 之间的循环依赖
假设这样的架构。
MODEL > BLL > DLL
尝试在我的 MODEL 中实现延迟加载,我遇到了 MODEL 和 BLL 之间的循环依赖关系。
基本上想象一下我想要实现的模型中的一个属性,如下所示
Public Readonly Property ProjectCategory As ProjectCategory
Get
If Me._ProjectCategory Is Nothing Then
Me._ProjectCategory = ProjectCategoryBLL.GetProjectCategoryByID(Me._ProjectCategoryID)
End If
Return Me._ProjectCategory
End Get
End Property
我的 MODEL、BLL 和 DLL 位于不同的项目中,因为事实上,我的 BLL 引用了我的模型,因此我无法从模型中引用我的 BLL,因为这会创建循环依赖关系。
这个问题的典型解决方案是什么?
Assuming an architecture as such.
MODEL > BLL > DLL
Trying to implement lazy loading in my MODEL I have run into a circular dependency between my MODEL and BLL..
Basically imagine a property in my model that I want to implement as follows
Public Readonly Property ProjectCategory As ProjectCategory
Get
If Me._ProjectCategory Is Nothing Then
Me._ProjectCategory = ProjectCategoryBLL.GetProjectCategoryByID(Me._ProjectCategoryID)
End If
Return Me._ProjectCategory
End Get
End Property
I have my MODEL, BLL and DLL in separate projects and because of the fact that my BLL references my model I can not reference my BLL from my model as this would create a circular dependency.
What is the typical solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来你把事情搞得很混乱,很可能是因为对层次和模式的理解混乱。
为什么你的 BLL 引用你的模型?应该没有必要。在经典的 n 层应用程序中,模型和 BLL 是一回事。如果您随后为您的 UI 实现一个模式(如 MVVM),那么该模型可能仍然是 BLL,或者它可能是调用 BLL 的单独代码位(并且 BLL 不直接了解该模型) 。在 MVC 中,模型处理数据,因此它再次与 BLL 对话(或者甚至可能集成为 BLL 的一部分)。
我的建议是模型参考 BLL,但反之则不然。或者,您可以将模型集成到 BLL 中,具体取决于您正在构建的内容的复杂性。
It looks like you have got things horribly mixed up, most likely due to a mixed up understanding of tiers and patterns.
Why does your BLL reference your model? It should have no need to. In a classical n-tier application, the model and the BLL are one and the same thing. If you then go and implement a pattern for your UI (like MVVM), then the model may still be the BLL, or it may be a separate bit of code that calls the BLL (and the BLL has no direct knowledge of the model). In MVC, the model handles the data, so once again it talks to the BLL (or may even be integrated and part of the BLL).
My suggestion is for the model to reference the BLL, but not the other way round. Or you could integrate the model into the BLL, depending on the complexity of what you are building.
我不同意你现在的架构。当然,将应用程序分层为逻辑层或物理层取决于您的项目需求和复杂性。但最好摆脱当前的实现并应用新的架构,如下所示:
View Models
、Messages
、Application Services
等组成。这将是用户进入整个系统的入口点。MVP
或MVC
模式的演示模式。这将是分层应用程序的通用架构。尝试针对接口而不是具体实现进行编程。此外,您对业务组件(应用程序层)的抽象程度越高,并且您越能利用设计模式和最佳实践的优势,您就可以确保您的代码库更具可扩展性、松散耦合性,并且具有更好的可维护性。
如果您正在开发一个产品而不仅仅是您自己的示例应用程序,我建议您更深入地研究
领域驱动设计
、企业应用程序架构
和设计模式
能够更好地对您的业务需求进行建模并实现更好、更健壮的架构。如果您需要更多信息或具体问题,我很乐意更多地讨论这一点[:
I don't approve your current architecture. of course layering your application into logical or physical tiers depend on your project needs and complexity. but it would be a better idea to get rid of your current implementation and apply a new architecture as such :
View Models
,Messages
,Application Services
and so on. this would be the entry point for users into the whole system.MVP
orMVC
pattern.This would be a common architecture for layered applications. try to program against interfaces rather than concrete implementations. also, the more you abstract your business components (application tiers) and the more you leverage the advantages of using
Design Patterns
and best practices, you make sure that your code base will be more scalable, loosely coupled, and better maintainable.If you're developing a product and not just sample app for you own, I'd suggest digging a little deeper into
Domain-Driven Design
,Enterprise Application Architectures
andDesign Patterns
to be able to model your business needs better and implement a better and more robust architecture.Would be glad to talk more about this if you needed more info or specific questions [:
您可以为模型项目中引用的 bll 类创建接口,并使用工厂模式或使用依赖项注入创建具体类。只需准备好为您的项目增加一些复杂性即可。另一种选择是查看 ORM。他们负责许多看起来您正在尝试实现的属性的延迟加载
You could create interfaces for your bll classes that are referenced in your Model project and either create a concrete class using the factory pattern or using dependency injection. Just be prepared to add some complexity to your projects. The alternative could be to take a look at ORMs. They take care of a lot of the lazy loading of properties that it looks like you are trying to achieve