在 OOP/MVC 中完全失败
好吧,这是我的错。我从来没有在学校学过编程,这就是为什么我总是以意大利面条代码告终。我一直对不同的模式感到好奇,并试图至少在基本层面上理解它们。
MVC 是我最担心的,我认为我永远无法利用它的优势,因为我不了解它的基础知识。
我的实际问题/问题看起来像:
前端控制器调用一个“核心”类,该类正在执行一些初始化,然后它使用正确的操作/参数调用实际控制器。控制器总是扩展“核心”类,这样我就可以访问它的变量等。它们一起工作得很好,但我真正的问题来了。
在不同的情况下需要某种方法(在大多数情况下获取数据库条目)。 (例如,产品需要它的制造商)
在这种情况下,我有两个(糟糕的)选择:
- 将所需的方法注入到“Core”类中,这样它就会随着时间的推移而变得臃肿
- 将所需的方法注入到实际调用的控制器中,这样我最终会得到一个冗余代码库
我在我的方法中看到很多可能的问题:
- 控制器总是扩展“核心”类
- “核心”控制器保存数据库对象,因此如果没有它,我无法访问我的数据库
- 数据库功能(例如获取产品)位于控制器中,但是我无法访问它们,因为它们总是首先调用“核心”(再次扩展问题)
请告诉我:
我的方法中最大的问题在哪里以及我可以在哪里纠正它?
注意:
请不要将此视为一般性问题,我认为这是一个可以回答的问题。如果您需要一些说明,请提出要求,我会尽力让事情变得轻松。
感谢您宝贵的时间,法布里克
Ok, it's my fault. I've never ever learned programming at a school and that's why i'm always ending up in a spaghetti code. I've always curious about different patterns and tried to understand them at least in a basic level.
MVC is my worst fear and i think i'll be never able to use it's advantages because of i don't understand it's fundamentals.
My actual question/problem looks like:
The front controller calls a 'Core' class which is doing some initialization then it calls the actual controller with the correct action/parameters. The controllers always extending the 'Core' class so i can acces it's variables, etc. They're working nicely together but here comes my real problem.
Some kind of methods (getting a database entry in most of the cases) are required in different cases. (e.g. a product needs it's manufacturer)
In this scenario i have two (bad) choices:
- Inject the required method into the 'Core' class so it's getting bloated over time
- Inject the required method into the actually called controller so i will end up a redundant codebase
I see a lot of possible problems in my approach:
- Controllers are always extending 'Core' class
- 'Core' controller holds the database object so without it i cannot access my Db
- Database functions (e.g. getting a product) are in the controllers but i cannot access them because they're always calling 'Core' first (extending problem again)
Please tell me:
Where is the biggest problem in my approach and where can i correct it?
Note:
Please don't treat this as a general question, i think this is an answerable thing. If you need some clarification, please ask for it and i'll try to lighten up things.
Thanks for your precious time, fabrik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的数据通过模型呈现给您的控制器和视图。该模型可能受存储库支持,但根据您可能希望在模型中提供数据库访问的开销。数据架构应该与此类似:
Your data is represented to your Controller and View through the Model. The Model may be supported by a Repository but depending on the overhead you might want to provide your database access in your Model. The data architecture should be similar to this:
你最大的问题是有“核心”类,尽快摆脱它。
顺便说一句,FrontController 也不是执行 MVC 操作的唯一方法。
你的第二个问题是控制器处理数据库,它不应该处理。我建议您使用一些抽象数据层,仅在模型中使用。控制器应该只处理模型,它不应该关心模型如何保存和获取数据。
Your biggest problem is having the "Core" class, get rid of it asap.
By the way, the FrontController is not the only way to do things MVC things either.
Your second problem is that controller deals with database, it shouldn't. I suggest you use some abstract data layer, which you use only in your models. And controller should deal only with models, it shouldn't care how models get their data persisted and fetched.
考虑使用 DI 框架自动将存储库的实例注入控制器(或者更好的是代理类)。尝试将业务逻辑保留在控制器之外,而是将其反射到帮助程序或代理类中。
我倾向于将我的逻辑分成视图 =>控制器(只是为了后面业务和视图的交互)=>业务逻辑 =>模型(DTO)=>至少进行低级数据访问。
另外,如果您的视图中需要通用的帮助器功能,也许可以创建几个扩展来提供帮助。
Look into using a DI framework to automatically inject an instance of a repository into your controllers (or even better, a proxy class). Try to keep business logic outside of your controllers, but rather, refector it out into a helper or proxy class.
I tend to split my logic up into views => controllers (just for interaction between business later and view) => business logic => models (DTOs) => low-level data access at a minimum.
Also, if you need common helper functionality in your views, perhaps create several extensions to help.