I am currently in an internship and from what I have been learning the controller in MVC should solely be used for traffic, and only for traffic. I have also been told about something called a "service layer", which sounds like the spot where I should be doing any data/business logic for the controller.
I have been searching around for examples and tutorials on this but I am not finding anything that dumbs it down enough for me, as I have only just learned MVC about a month ago. I am wondering if someone would be able to explain and show me how I would transfer the following ActionResult Index
business logic into a "service layer".
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());
}
}
All that this code is doing is returning a DB table, along with some extra logic which pulls an XML file and parses some of its information.
I am wondering how I could turn this specific example into using a service layer (or whatever it is I should use for the logic). Please try to make this as simple as possible, as I am still new to MVC.
A Service Layer defines set of available operations in regards with interfacing client layers i.e. encapsulates the application's business logic. They are just a separate classes in different class library (or namespace) those are independent of MVC framework infrastructure which can be used by ASP.NET Web API or WCF.
I have been searching around for examples and tutorials on this but I am not finding anything that dumbs it down enough for me
Here is good example of very well known music store. This may help you getting through Service Layer with DI Injecting a Controller MSDN
Purpose of service layer is for decoupling and maintainability