Sono attualmente in uno stage e da quello che sto imparando il controller in MVC deve essere utilizzato esclusivamente per il traffico e solo per il traffico. Mi è stato anche detto qualcosa chiamato "livello di servizio", che suona come il punto in cui dovrei fare qualsiasi logica dati/business per il controller.Comprensione e uso di "Service Layers" - .NET MVC 5
Sono stato a cercare in giro per esempi e tutorial su questo, ma non sto trovando nulla che muta giù abbastanza per me, come ho appena imparato MVC circa un mese fa. Mi chiedo se qualcuno sarebbe in grado di spiegare e mostrarmi come trasferirei la seguente logica aziendale ActionResult Index
in un "livello di servizio".
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());
}
}
Tutto che questo codice sta facendo sta tornando una tabella DB, insieme ad una logica extra che estrae un file XML e analizza alcune delle sue informazioni.
Mi chiedo come sia possibile trasformare questo esempio specifico in un livello di servizio (o qualsiasi altra cosa dovrei usare per la logica). Per favore cerca di renderlo il più semplice possibile, visto che sono ancora nuovo in MVC.