我可以在DDD域服务中称为参考工作单元吗?

时间:2019-07-07 01:53:06

标签: domain-driven-design

在发送电子邮件确认之前必须创建并保留用户的情况下,是否可以在如下所示的Domain服务中实现此行为:

    $request = "SELECT * FROM " . $DB_tname . " ORDER BY score DESC LIMIT 10";
    $result = mysqli_query($connection, $request);
    $numRows = mysqli_num_rows($result);


    <?php 
        require "config.php";
    ?>

    <!DOCTYPE html>
    <html lang="en">

        <head>

            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="description" content="Here you can see the best players of the week.">

            <title><?php echo $winTitle; ?></title>

            <!-- Bootstrap Core CSS -->
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
            <style>
                html,
                body {
                    width: 100%;
                    height: 100%;
                    overflow-x:hidden;
                }

                body {
                    font-family: "Source Sans Pro","Helvetica Neue",Helvetica,Arial,sans-serif;
                }

                /* Header */
                .scrollable
                {
                  position: flex;
                  overflow: auto;
                }
                .header {
                    display: table;
                    position: relative;
                    width: 100%;
                    height: 100%;
                    background: url(<?php echo $Background; ?>) no-repeat center center scroll;
                    -webkit-background-size: cover;
                    -moz-background-size: cover;
                    background-size: cover;
                    -o-background-size: cover;
                }
            </style>


            <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">

            <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
            <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
            <!--[if lt IE 9]>
                <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
                <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
            <![endif]-->


        </head>

        <body>


            <!-- Header -->
            <header id="top" class="header">
                    <div class="row">
                    <br><br><br><br><br>
                        <div class="col-md-8 col-md-offset-2">
                            <div id="topModal" class="modal show scrollable" tabindex="-1" role="dialog" aria-hidden="true">
                                <div style="width: 65%;" class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h1 class="text-center">Top Players</h1>
                                        </div>
                                        <div class="modal-body">
                                            <table class="table table-hover table-bordered">
                                                <tr>
                                                    <th style="width: 5%">Rank</th>
                                                    <th>Name</th>
                                                    <th style="width: 15%">Score</th>
                                                    <th style="width: 10%">Deaths</th>
                                                    <th style="width: 15%">Onlinetime H:M</th>
                                                </tr>
                                                <?php   
                                                    function toStd($sekunden)
                                                    {
                                                        $stunden = floor($sekunden / 3600);
                                                        $minuten = floor(($sekunden - ($stunden * 3600)) / 60);
                                                        $sekunden = round($sekunden - ($stunden * 3600) - ($minuten * 60), 0);

                                                        if ($stunden <= 9) {
                                                            $strStunden = "0" . $stunden;
                                                        } else {
                                                            $strStunden = $stunden;
                                                        }

                                                        if ($minuten <= 9) {
                                                            $strMinuten = "0" . $minuten;
                                                        } else {
                                                            $strMinuten = $minuten;
                                                        }

                                                        return "$strStunden:$strMinuten";
                                                    } 
                                                    $DB_Server = ""; //MySQL IP
                                                    $DB_User = ""; //MySQL user
                                                    $DB_Pw = ""; //MySQL password
                                                    $DB_Db = ""; //Database name

                                                    $DB_tname = "bestplayers"; //Table name (will be created)
                                                    $connection = mysqli_connect($DB_Server, $DB_User, $DB_Pw)
                                                        or die("Connection failed! Check your MySQL details!");

                                                    $request = "SELECT * FROM " . $DB_tname . " ORDER BY score DESC LIMIT 10";
                                                    $result = mysqli_query($connection, $request);

                                                    $numRows = mysqli_num_rows($result);

                                                    $i1 = 0;
                                                    $i2 = 1;

                                                    while ($i1 < $numRows && $i1 < 10) {
                                                        $row = mysqli_fetch_row($connection, $result);

                                                        $username = $row[4];
                                                        if($username == ""){
                                                            $username = "Nobody";
                                                        }

                                                        if($i1 == 0){
                                                            echo '<tr class="success">';
                                                        }else{
                                                            echo '<tr>';
                                                        }

                                                        echo '<td>' . $i2 . '</td>';
                                                        echo '<td>' . $username . '</td>';
                                                        echo '<td>' . $row[1] . '</td>';
                                                        echo '<td>' . $row[7] . '</td>';
                                                        echo '<td>' . toStd($row[5]) . '</td>';
                                                        echo '</tr>';

                                                        $i2++;
                                                        $i1++;
                                                    }

                                                    $request = "SELECT * FROM " . $DB_tname . " WHERE iswp = 1";
                                                    $request_run = mysqli_query($connection, $request);
                                                    var_dump(mysqli_error($connection));

                                                    //$result = mysqli_query($connection, $request);
                                                    $row = mysqli_fetch_row($request_run);

                                                    $history = explode(", ", $row[3]);
                                                    $pthistory = explode(", ", $row[6]);
                                                    $deathhistory = explode(", ", $row[8]);

                                                    if($history[0] == ""){
                                                        $history[0] = 0;
                                                    }
                                                    if($deathhistory[0] == ""){
                                                        $deathhistory[0] = 0;
                                                    }
                                                    if( !isset($row[4])){
                                                        $wpnick = "Nobody";
                                                    }else{
                                                        $wpnick = $row[4];
                                                    }
                                                ?>
                                            </table>
                                            <div class="panel panel-success">
                                                <div class="panel-body">
                                                    <b>Player of the week: </b><a href=<?php echo '"http://steamcommunity.com/profiles/' . $row[0] . '">'; echo $wpnick; ?></a> &nbsp( with <b><?php echo $history[0]; ?></b> score, <b><?php echo $deathhistory[0]; ?></b> deaths and <b><?php echo toStd($pthistory[0]); ?></b> onlinetime )
                                                </div>
                                            </div>

                                            <div class="panel panel-default">
                                                <div class="panel-heading">
                                                    <h4 class="panel-title">
                                                        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#collapse1">Information</a>
                                                    </h4>
                                                </div>
                                                <div id="collapse1" class="panel-collapse collapse">
                                                    <div class="panel-body">
                                                        <?php echo $InfoText; ?>
                                                    </div>
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
            </header>

            <!-- jQuery -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

            <!-- Bootstrap Core JavaScript -->
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>


        </body>

    </html>

1 个答案:

答案 0 :(得分:2)

通常实践的机制是不让域服务与基础结构交互-它们通常不处理事务和工作单元。它们与聚合一起属于域模型层,处理复杂的用例,这些用例通常涉及多个聚合,并且应避免基础结构方面的问题,包括持久性和通知。

应用程序服务应该是从事这项工作的人。他们应该调用“域”模型层(无论是服务还是聚合),收集更改的项,并作为事务处理持久性。

在您的示例中,您可能应该将UserService视为应用程序服务,在factory method中调用User Aggregate来创建一个新的用户对象,然后在repository

现在好了:

  • 我过去使用的一个有趣的模式是将通知(例如发送电子邮件)作为工作单元本身的一部分。 UoW不仅提交所有数据更改,而且还负责调度“已注册”通知。成功提交后,您只需注册/提交通知,然后让UoW进行实际发送。

  • 更好的模式是发布一个具有相关详细信息的事件,并使用订阅者来构造和分发通知。