加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 理解和使用“服务层” – .NET MVC 5

发布时间:2020-12-16 03:23:42 所属栏目:asp.Net 来源:网络整理
导读:我目前正在实习,从我一直学习的MVC中的控制器应该仅用于流量,仅用于流量.我也被告知了一个叫做“服务层”的东西,这听起来像是我应该为控制器做任何数据/业务逻辑的地方. 我一直在寻找关于这方面的示例和教程,但我找不到任何让我感到愚蠢的东西,因为我刚刚在
我目前正在实习,从我一直学习的MVC中的控制器应该仅用于流量,仅用于流量.我也被告知了一个叫做“服务层”的东西,这听起来像是我应该为控制器做任何数据/业务逻辑的地方.

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

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());
}
}

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

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

解决方法

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

I have been searching around for examples and tutorials on this but I
am not finding anything that dumbs it down enough for me

这是非常着名的音乐商店的好例子.这可以帮助您通过DI Injecting a Controller MSDN完成服务层

服务层的目的是用于解耦和可维护性

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读