在Symfony2中将业务逻辑放在哪里? [英] Where to place business logic in Symfony2?

查看:101
本文介绍了在Symfony2中将业务逻辑放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了许多文章和Stack Overflow资源之后,我仍然遇到一些有关将业务逻辑放在哪里?"这一著名问题的问题.阅读 StackOverflow问题

After reading a lot of posts and Stack Overflow resources, I've still got some problems about the famous question about "where to put business logic?" Reading StackOverflow Question and A Blog Post, I believe I've understood the issue of code separation well.

假设我有一个Web表单,您可以在其中添加将添加到数据库的用户.此示例涉及以下概念:

Suppose I have a web form where you can add a user that will be added to a db. This example involves these concepts:

  • 表格
  • 控制器
  • 实体
  • 服务
  • 存储库

如果我不遗漏任何东西,则必须创建一个具有一些属性,getter,setter等的实体,以使其持久化到数据库中.如果要获取或编写该实体,则将使用entityManager,对于非规范"查询,将使用entityRepository(这是适合您的查询语言"查询的位置).

If I didn't miss something, you have to create an entity with some properties, getters, setters and so on in order to make it persist into a db. If you want to fetch or write that entity, you'll use entityManager and, for "non-canonical" query, entityRepository (that is where you can fit your "query language" query).

现在,您必须为所有业务逻辑定义一个服务(这是一个带有惰性"实例的PHP类);这是放置大量"代码的地方.将服务记录到应用程序中后,几乎可以在任何地方使用它,这涉及代码重用等.

Now you have to define a service (that is a PHP class with a "lazy" instance) for all business logic; this is the place to put "heavy" code. Once you've recorded the service into your application, you can use it almost everywhere and that involves code reuse and so on.

呈现和发布表单时,将其与您的实体(当然还有约束)绑定,并使用上面定义的所有概念将它们放在一起.

When you render and post a form, you bind it with your entity (and with constraints of course) and use all the concepts defined above to put all together.

因此,"old-me"将以这种方式编写控制器的操作:

So, "old-me" would write a controller's action in this way:

public function indexAction(Request $request)
    {
        $modified = False;
        if($request->getMethod() == 'POST'){ // submit, so have to modify data
            $em = $this->getDoctrine()->getEntityManager();
            $parameters = $request->request->get('User'); //form retriving
            $id = $parameters['id'];
            $user = $em->getRepository('SestanteUserBundle:User')->find($id);
            $form = $this->createForm(new UserType(), $user);
            $form->bindRequest($request);
            $em->flush();
            $modified = True;
        }

        $users = $this->getDoctrine()->getEntityManager()->getRepository('SestanteUserBundle:User')->findAll();
        return $this->render('SestanteUserBundle:Default:index.html.twig',array('users'=>$users));
    }

"New-me"通过以下方式重构了代码:

"New-me" has refactored code in this way:

   public function indexAction(Request $request)
    {
        $um = $this->get('user_manager');
        $modified = False;
        if($request->getMethod() == 'POST'){ // submit, so have to modify data
            $user = $um->getUserById($request,False);
            $form = $this->createForm(new UserType(), $user);
            $form->bindRequest($request);
            $um->flushAll();
            $modified = True; 
        }
        $users = $um->showAllUser();
        return $this->render('SestanteUserBundle:Default:index.html.twig',array('users'=>$users));
    }

$um是一项自定义服务,其中存储了从#1到#2的所有看不到的代码.

Where $um is a custom service where all code that you can't see from #1 code piece to #2 code piece is stored.

所以,这是我的问题:

  1. 我终于了解了symfony2和{M} VC的精髓吗?
  2. 重构很好吗?如果没有,哪种更好的方法呢?

Post Scriptum :我知道我可以使用FOSUserBundle进行用户存储和身份验证,但这是一个自学使用Symfony的基本示例. 此外,我的服务中还注入了ORM.Doctrine.*以使其正常工作(只是给那些同样困惑我读此问题的人的笔记)

Post Scriptum: I know that I can use the FOSUserBundle for User store and authentication, but this is a basic example for teach myself how to work with Symfony. Moreover, my service was injected with ORM.Doctrine.* in order to work (just a note for who read this question with my same confusion)

推荐答案

关于在何处放置业务逻辑,主要有两种方法:SOA体系结构和域驱动的体系结构.如果您的业务对象(实体)是贫乏的,我的意思是,如果它们没有业务逻辑,而只是获取器和设置器,那么您将更喜欢SOA.但是,如果您在业务对象中构建业务逻辑,那么您将更喜欢另一个.亚当·比恩(Adam Bien)讨论了这些方法:

There are two main approaches regarding on where to put the business logic: the SOA architecture and the domain-driven architecture. If your business objects (entities) are anemic, I mean, if they don’t have business logic, just getters and setters, then you will prefer SOA. However, if you build the business logic inside your business objects, then you will prefer the other. Adam Bien discusses these approaches:

使用Java EE 6进行域驱动的设计:

Domain-driven design with Java EE 6: http://www.javaworld.com/javaworld/jw-05-2009/jw-05-domain-driven-design.html

使用Java EE 6的精益服务体系结构: http://www.javaworld.com/javaworld/jw-04-2009/jw-04-lean-soa-with-javaee6.html

Lean service architectures with Java EE 6: http://www.javaworld.com/javaworld/jw-04-2009/jw-04-lean-soa-with-javaee6.html

它是Java,但您可以理解.

It’s Java, but you can get the idea.

这篇关于在Symfony2中将业务逻辑放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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