首页 > 代码库 > MVC项目实践(五)——逻辑操作的实现

MVC项目实践(五)——逻辑操作的实现

接下来是逻辑操作的实现,直接决定了各页面中的各个操作类需要实现何种操作,它明确了各个页面的职责。

由设计文档可以知道我们的第一个页面为选择业务:

 1 @model VolleyballScoring.Models.Team
 2 
 3 @{
 4     ViewBag.Title = "Index";
 5 }
 6 
 7 <h2>Index</h2>
 8 <h3>选择你想要进行的事情</h3>
 9 <div>@Html.ActionLink("查询队伍","Index","Teams")</div>
10 <div>@Html.ActionLink("计分台","Scoring")</div>
11 <div>@Html.ActionLink("观看比赛","Index","Audience")</div>

以下为队伍的控制器操作:

  1 namespace VolleyballScoring.Controllers
  2 {
  3     public class TeamsController : Controller
  4     {
  5         private VolleyballDBContext db = new VolleyballDBContext();
  6 
  7         //
  8         // GET: /Teams/
  9 
 10         public ActionResult Index()
 11         {
 12             return View(db.Teams.ToList());
 13         }
 14 
 15         //
 16         // GET: /Teams/Details/5
 17 
 18         public ActionResult Details(int id = 0)
 19         {
 20             Team team = db.Teams.Find(id);
 21             if (team == null)
 22             {
 23                 return HttpNotFound();
 24             }
 25             return View(team);
 26         }
 27 
 28         //
 29         // GET: /Teams/Create
 30 
 31         public ActionResult Create()
 32         {
 33             return View();
 34         }
 35 
 36         //
 37         // POST: /Teams/Create
 38 
 39         [HttpPost]
 40         public ActionResult Create(Team team)
 41         {
 42             if (ModelState.IsValid)
 43             {
 44                 db.Teams.Add(team);
 45                 db.SaveChanges();
 46                 return RedirectToAction("Index");
 47             }
 48 
 49             return View(team);
 50         }
 51 
 52         //
 53         // GET: /Teams/Edit/5
 54 
 55         public ActionResult Edit(int id = 0)
 56         {
 57             Team team = db.Teams.Find(id);
 58             if (team == null)
 59             {
 60                 return HttpNotFound();
 61             }
 62             return View(team);
 63         }
 64 
 65         //
 66         // POST: /Teams/Edit/5
 67 
 68         [HttpPost]
 69         public ActionResult Edit(Team team)
 70         {
 71             if (ModelState.IsValid)
 72             {
 73                 db.Entry(team).State = EntityState.Modified;
 74                 db.SaveChanges();
 75                 return RedirectToAction("Index");
 76             }
 77             return View(team);
 78         }
 79 
 80         //
 81         // GET: /Teams/Delete/5
 82 
 83         public ActionResult Delete(int id = 0)
 84         {
 85             Team team = db.Teams.Find(id);
 86             if (team == null)
 87             {
 88                 return HttpNotFound();
 89             }
 90             return View(team);
 91         }
 92 
 93         //
 94         // POST: /Teams/Delete/5
 95 
 96         [HttpPost, ActionName("Delete")]
 97         public ActionResult DeleteConfirmed(int id)
 98         {
 99             Team team = db.Teams.Find(id);
100             db.Teams.Remove(team);
101             db.SaveChanges();
102             return RedirectToAction("Index");
103         }
104 
105         protected override void Dispose(bool disposing)
106         {
107             db.Dispose();
108             base.Dispose(disposing);
109         }
110     }
111 }

其他实体类的控制器与之大同小异,就不在此赘述。

下面是操作双方得分的接口:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using NGuestBook.Entity;
 5 
 6 namespace VolleyballScoring.Interface
 7 {
 8     /// <summary>
 9     /// 业务逻辑接口——计分员
10     /// </summary>
11     public interface IAdminBLL
12     {
13         /// <summary>
14         /// 增加计分
15         /// </summary>
16         /// <param name="Section">新小节类</param>
17         /// <returns>是否成功</returns>
18         bool Add(Section sec);
19 
20         /// <summary>
21         /// 删除比分
22         /// </summary>
23         /// <param name="id">欲删除的小节ID</param>
24         /// <returns>是否成功</returns>
25         bool Remove(int id);
26 
27     }
28 }

以下是按照《2015-2016赛季中国排球联赛竞赛规程》的比分变化规则的逻辑表现

  

public  class  Admin:IAminBLL
{
     public void Action(Game game,Section sec)
    {
        if (sec.SNum < 4)//前四局的加分计算和判断。
            {
                if (sec.RouA >= 24 && sec.RouB >= 24)//当两队24分平的情况。
                {
                    if (Math.Abs(sec.RouA - sec.RouB) == 2)
                    {
                        if (sec.RouA > sec.RouB)
                        {
                            game.SscoA += 1;
                            SscoA.Text = game.SscoA.ToString();
                            sec.RouA = 0;
                            ScoreA.Text = sec.RouA.ToString();
                            sec.RouB = 0;
                            ScoreB.Text = sec.RouB.ToString();
                        }
                        else
                        {
                            game.SscoB += 1;
                            SscoB.Text = game.SscoB.ToString();
                            sec.RouA = 0;
                            ScoreA.Text = sec.RouA.ToString();
                            sec.RouB = 0;
                            ScoreB.Text = sec.RouB.ToString();
                        }
                    }
                }
                else if(sec.RouA<24||sec.RouB<24) //当两队没有达到24分平的时候。
                {
                    if (sec.RouA == 25)
                    {
                        game.SscoA += 1;
                        SscoA.Text = game.SscoA.ToString();
                        sec.RouA = 0;
                        ScoreA.Text = sec.RouA.ToString();
                        sec.RouB = 0;
                        ScoreB.Text = sec.RouB.ToString();
                    }
                    else if (sec.RouB == 25)
                    {
                        game.SscoB += 1;
                        SscoB.Text = game.SscoB.ToString();
                        sec.RouA = 0;
                        ScoreA.Text = sec.RouA.ToString();
                        sec.RouB = 0;
                        ScoreB.Text = sec.RouB.ToString();
                    }
                }
            }
            else if (sec.SNum == 4)//第五局的加分计算和判断。
            {
                if (sec.RouA >= 14 && sec.RouB >= 14)//当两队24分平的情况。
                {
                    if (Math.Abs(sec.RouA - sec.RouB) == 2)
                    {
                        if (sec.RouA > sec.RouB)
                        {
                            game.SscoA += 1;
                            SscoA.Text = game.SscoA.ToString();
                        }
                        else
                        {
                            game.SscoB += 1;
                            SscoB.Text = game.SscoB.ToString();
                        }
                    }
                }
                else if (sec.RouA < 14 || sec.RouB < 14) //当两队没有达到24分平的时候。
                {
                    if (sec.RouA == 15)
                    {
                        game.SscoA += 1;
                        SscoA.Text = game.SscoA.ToString();
                    }
                    else if (sec.RouB == 15)
                    {
                        game.SscoB += 1;
                        SscoB.Text = game.SscoB.ToString();
                    }
                }
            }
            else
            {
                Response.Write("<script>alert(‘比赛已结束‘);</script>");
            }

    }  
}    

以上就是主要的逻辑业务表现,下一篇是具体的UI页面的表现

MVC项目实践(五)——逻辑操作的实现