5

현재 인턴쉽 중이며 MVC에서 컨트롤러를 학습 한 내용은 트래픽에만 사용되어야하며 트래픽에 대해서만 사용해야합니다. 나는 또한 "서비스 계층 (service layer)"이라고 불리는 것에 대해서도 들었는데, 나는 컨트롤러 용 데이터/비즈니스 로직을 수행해야만하는 곳과 같다고 들었다."서비스 레이어"이해 및 사용 - .NET MVC 5

나는 이것에 대한 예제와 튜토리얼을 찾고 있었지만, 한 달 전에 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()); 
} 
} 

이 코드가 수행하는 모든 작업은 XML 파일을 가져 와서 일부 정보를 구문 분석하는 몇 가지 추가 논리와 함께 DB 테이블을 반환하는 것입니다.

이 특정 예제를 서비스 계층 (또는 논리에 사용해야하는 것은 무엇이든)으로 변환하는 방법을 궁금합니다. MVC를 처음 접했을 때 가능한 한 간단하게 만들어보십시오.

답변

2

서비스 레이어는 클라이언트 레이어를 인터페이싱하는 것과 관련하여 사용 가능한 작업 집합을 정의합니다. 즉, 응용 프로그램의 비즈니스 논리를 캡슐화합니다. 그것들은 ASP.NET 웹 API 또는 WCF에서 사용할 수있는 MVC 프레임 워크 인프라와는 독립적 인 다른 클래스 라이브러리 (또는 네임 스페이스)의 별도 클래스입니다.

나는이에 대한 예제 및 자습서 주변에 검색되었지만 나는 여기에 매우 잘 알려진 뮤직 스토어의 좋은 예입니다 나를

충분히 그것을 아래로 dumbs 아무것도 발견하고 있지 않다. 이것은 DI로 서비스 레이어를 통과하는 데 도움이 될 수 있습니다. Injecting a Controller MSDN

서비스 레이어의 목적은 디커플링 및 유지 보수성을위한 것입니다.