理解和使用"服务层" - .NET MVC 5

Aus*_*tin 5 model-view-controller asp.net-mvc service-layer asp.net-mvc-5

我目前正在实习,从我一直学习的MVC中的控制器应该仅用于流量,仅用于流量.我也被告知了一个叫做"服务层"的东西,这听起来像是我应该为控制器做任何数据/业务逻辑的地方.

我一直在寻找关于这方面的示例和教程,但我找不到任何让我感到愚蠢的东西,因为我刚刚在一个月前刚学过MVC.我想知道是否有人能够解释并告诉我如何将以下ActionResult Index业务逻辑转移到"服务层".

public class LakerLegendsController : Controller
{    
    string pathway1 = HostingEnvironment.MapPath(@"~/App_Data/Announcement1.txt");
    string pathway2 = HostingEnvironment.MapPath(@"~/App_Data/Announcement2.txt");
    string pathway3 = HostingEnvironment.MapPath(@"~/App_Data/Announcement3.txt");
    private MoviesEntities db = new MoviesEntities();

public ActionResult Index()
{
    // Setting some ViewBag texts from announcement files.
    string text1 = System.IO.File.ReadAllText(pathway1);
    ViewBag.TextHTML1 = text1;

    string text2 = System.IO.File.ReadAllText(pathway2);
    ViewBag.TextHTML2 = text2;

    string text3 = System.IO.File.ReadAllText(pathway3);
    ViewBag.TextHTML3 = text3;


    // Following pulls some XML information
    XDocument xmlFile = XDocument.Load(@"http://na.leagueoflegends.com/en/rss.xml");


    var LoLtitles = from service in xmlFile.Descendants("item")
            select (string)service.Element("title");
    var LoLlinks = from service in xmlFile.Descendants("item")
             select (string)service.Element("link");
    var LoLdescriptions = from service in xmlFile.Descendants("item")
             select (string)service.Element("description");
    var LoLDates = from service in xmlFile.Descendants("item")
              select (string)service.Element("pubDate");

    var servicing = LoLdescriptions.ToArray();
    for (int i = 0; i < 4; i++)
    {
        servicing[i] = Regex.Replace(Server.HtmlDecode(servicing[i]), @"<[^>]*>", String.Empty);
    }

    ViewBag.titles = LoLtitles.ToArray();
    ViewBag.links = LoLlinks.ToArray();
    ViewBag.descriptions = servicing;
    ViewBag.dates = LoLDates.ToArray();

    // Pulls the DB Table
    var users = db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1);
    return View(users.ToList());
}
}
Run Code Online (Sandbox Code Playgroud)

这段代码所做的就是返回一个DB表,以及一些额外的逻辑,它可以提取XML文件并解析它的一些信息.

我想知道如何将这个特定的例子变成使用服务层(或者我应该用于逻辑的任何东西).请尝试尽可能简单,因为我还是MVC的新手.

Sma*_*ack 2

服务层定义了与客户端层接口相关的一组可用操作,即封装了应用程序的业务逻辑。它们只是不同类库(或命名空间)中的单独类,独立于 MVC 框架基础结构,可供 ASP.NET Web API 或 WCF 使用。

我一直在寻找这方面的示例和教程,但我没有找到任何可以让我变得足够简单的东西

这是非常著名的音乐商店的一个很好的例子。这可能会帮助您通过 DI注入控制器 MSDN来完成服务层

服务层的目的是为了解耦和可维护性