如何在MVC模型中编写可重用的业务逻辑? [英] How to write reusable business logic in MVC models?

查看:109
本文介绍了如何在MVC模型中编写可重用的业务逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,我们尝试使用MVC(PHP)框架.在讨论了很多之后,认为MVC很好,但是我错过了编写可重用的模型(应用程序)逻辑的可能性.因此,我不确定我们是否具有在MVC框架中实现软件的正确方法.

my problem is that we try to use a MVC (PHP) framework. After discussing a lot think that MVC is very good, but I'm missing the possibility to write reusable model-(application)logic. So, I'm not sure if we have the right approach to implement our software in a MVC framework.

首先,我将介绍我们目前使用的非MVC oo方法.

First I´ll describe the non-MVC, oo-approach which we use at the moment.

例如-我们正在开发一些浏览器游戏(是的,这是我们的职业).想象我们有一个玩家对象.我们经常使用此播放器对象.我们有一些不同的页面,您可以在其中购买思维,因此您需要在玩家的银行帐户"上进行金钱"交易,或者想象您可以与其他玩家进行战斗.我们有几个战斗脚本,这些脚本包含2个或更多玩家对象(取决于战斗类型,即战队战斗,玩家对玩家战斗...).

For example - we are working on some browser games (yes that's our profession). Imagine we have an player object. We use this player object very often. We have some different pages where you can buy thinks, so you need to make "money" transactions on the players "bank-account" or imagine you can do fights against other players. We have several fight-scripts, and these scripts take 2 or more player objects (it depends on the type of battle ie. clan battle, player vs. player battle...).

因此,我们有几个页面(和控制器)具有不同的战斗逻辑.但是每个控制器都使用玩家对象来计算玩家拥有的所有属性和物品,以及玩家将要进行的伤害和防御.

So, we have several pages (and controllers) with different battle-logic. But each of this controllers use the player-object to calculate all the attributes and items a player has and which damage and defence a player will do.

那么,在MVC模型的情况下,我们如何重用播放器对象中的逻辑?在不同的战斗控制器和-models中复制所有必要的逻辑将是很糟糕的.

so, how can we reuse the logic in the player object in case of a MVC Model? it would be bad to duplicate all the necessary logic in the different fight-controllers and -models.

我认为黄金交易"逻辑将是一个为您提供更多详细信息的好例子.您需要在交易中使用交易功能,如果您与其他玩家对战并抢夺了他的一些黄金,那么在购买一些东西的情况下就需要交易功能,而在花费一些黄金的情况下就需要交易功能玩家协会...

I think the "gold-transaction"-logic would be a good example to give you some more detail information. you need the transact-function in case of a fight, if you win against an other player and loot some of his gold, you need the transaction function in case of buying some stuff and you need the transact-function in case of spending some gold to the players guild...

所以,我想说在一个播放器模型中定义所有这些功能将是一个糟糕的方法!我可以说您这些玩家模型会非常庞大​​(实际上,我们的问题是我们的玩家级别非常庞大-它是一个上帝级别)

So, I would say it would be a bad approach to define all these functions in one player model! I can say you these player model would be very big (actually we have the problem that our player-class is really huge - its a god class)

您认为针对此问题有MVC风格的解决方案吗?

do you think there is a MVC-style solution for this problem?

推荐答案

我想说的是,您将代码放在最有意义的地方,而无需在其他地方重复.

I would say you put the code where it makes the most sense, and where you would not need to duplicate it someplace else.

如果某些操作始终需要一个Player对象,但可能在不同的Controller之间使用,则将Player类放在逻辑上.另一方面,如果只需要在某个Controller的上下文中完成一点逻辑,并且可能涉及其他类,则它可能应该在控制器中,或者也应该在其他类中.

If there is some operation that always needs a Player object, but might be used across different Controllers, the Player class would be the logical place to put it. On the other hand, if a bit of logic only needs to be done in the context of a certain Controller, and involves potentially other classes, it perhaps should be in the controller - or perhaps in some other class, too.

如果您无法确定逻辑应该去哪里,那可能是因为您的函数不够精细,并且没有足够的可重用性.当然,MVC的某些方面迫使您对关注点的分离进行更多的思考,并使事情变得比普通"的OOP方法更干燥……因此,您可能最终会破坏单个代码中的当前编码操作现在可以在不同类上将函数集成到多个函数中,以便在正确的位置获取正确的代码.

If you are having trouble figuring out where the logic should go, perhaps it's because your functions are not granular and reusable enough as they are. There are certainly aspects of MVC that force you to think a little bit more about the separation of concerns and keeping things DRY more than a 'plain' OOP approach... so you might end up breaking up currently coded operations that are in a single function now into multiple functions on different classes to get the right code in the right places.

例如-这些都不是特定的建议,而只是一个可能的随机思考过程-可能需要将参与者之间金"的转移过程分解为更细化的过程.玩家类可能会执行更改余额的基本任务,但随后控制器可能会执行过程的特定部分,例如验证向谁/从谁进行黄金转移以及为什么转移黄金.

For example - and these are not at all specific suggestions, but just a random possible thought process - maybe the process of transferring 'gold' between players needs to be broken down into more granular processes. The player class may do the basic task of changing the balance, but then the controller(s) may do specific parts of the process, such as verifying to/from whom gold is being transferred and why.

这篇关于如何在MVC模型中编写可重用的业务逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆