drpph80800 2010-09-02 12:15
浏览 44
已采纳

在OOP / MVC中完全失败了

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

  • 写回答

3条回答 默认 最新

  • dro59505 2010-09-02 12:33
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条